1

who knows how to create a symfony2.1.x session for phpunit tests ?

i found this, but it`s not working:

$account = $this->em->getRepository('BaseBundle:Account');
$user = $account->findOneById('835d278dfb30');        
$container = $this->client->getContainer();

$token = new UsernamePasswordToken($user, null, array('IS_AUTHENTICATED_FULLY', 'IS_AUTHENTICATED_ANONYMOUS'));
$this->client->getContainer()->get('security.context')->setToken($token);
$session = $this->client->getContainer()->get('session');
$session->set('_security_secured_area', serialize($token));
$session->save();

after this i tried to make a request to the account url, which is protected through symfony´s security area and it should me not redirect to the login page :(

Please help me, i tried a lot of stuff out there but nothing works.

3
  • 1
    I understood you're trying to write a functional test. You need to send a cookie with your request so that your application can recognise the user. Commented Oct 29, 2012 at 21:40
  • yes, that´s exatly what i want... can you give me an example of a working cookie ? Commented Oct 30, 2012 at 6:47
  • 1
    stackoverflow.com/questions/10859321/… and symfony.com/doc/2.0/book/… Commented Oct 30, 2012 at 9:57

1 Answer 1

1

you can use Symfony session mock objects.

Link to documentation: http://symfony.com/doc/current/components/http_foundation/session_testing.html

Sign up to request clarification or add additional context in comments.

2 Comments

I’m sorry, but you call that documentation?
Olivier, check out my reply below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.