I\'m writing a functional test for an action that uses Symfony2\'s session service to fetch data. In my test class\'s setUp
method, I call $this->get(\
Firstly try using your client's container (I'm assuming you're using WebTestCase):
$client = static::createClient();
$container = $client->getContainer();
If it still doesn't work try saving the session:
$session = $container->get('session');
$session->set('foo', 'bar');
$session->save();
I didn't try it in functional tests but that's how it works in Behat steps.
You can retrieve the "session" service. With this service, you can :
The code can be the following :
use Symfony\Component\BrowserKit\Cookie;
....
....
public function testARequestWithSession()
{
$client = static::createClient();
$session = $client->getContainer()->get('session');
$session->start(); // optional because the ->set() method do the start
$session->set('foo', 'bar'); // the session is started here if you do not use the ->start() method
$session->save(); // important if you want to persist the params
$client->getCookieJar()->set(new Cookie($session->getName(), $session->getId())); // important if you want that the request retrieve the session
$client->request( .... ...
The Cookie with $session->getId() has to be created after the start of the session
See Documentation http://symfony.com/doc/current/testing/http_authentication.html#creating-the-authentication-token