php - Testing Symfony2 session
980
Here is the action in my controller code:
//DefaultController.php
public function resultsAction(Request $request)
{
$session = $request->getSession();
$data = $session->get('custom_data');
var_dump($data);
//...
}
Here is my test code:
//DefaultControllerTest.php
public function setUp()
{
$this->client = static::createClient(array(), array(
'HTTP_USER_AGENT' => 'MySuperBrowser/1.0',
));
$this->client->insulate();
$this->client->followRedirects(true);
$sessionMock = new MockFileSessionStorage();
$this->session = new Session($sessionMock);
}
public function testAdslRisultati()
{
$data = array(
'key1' => 'val1',
'key2' => 'val2' //etc...,
);
$this->session->set('custom_data', $data);
$crawler = $this->client->request('GET', '/results');
$this->assertTrue($crawler->filter('html:contains("text...")')->count() > 0);
}
When I execute the test, the value of $data in the controller is always empty. How can I save any data in session from test and retry it from controller?
//config_test.yml
framework:
test: ~
session:
storage_id: session.storage.mock_file
Thank you very much
Answer
Solution:
Your can get the session storage from container in client
As example:
Attention: The container in client used only for one request (
$client->request()
).