这是我的控制器代码中的动作:
//DefaultController.php
public function resultsAction(Request $request)
{
$session = $request->getSession();
$data = $session->get('custom_data');
var_dump($data);
//...
}
下面是我的测试代码:
//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);
}
当我执行测试时,控制器中的$data的值总是空的。我如何保存会话中的任何数据从测试和重试它从控制器?
//config_test.yml
framework:
test: ~
session:
storage_id: session.storage.mock_file
非常感谢
您可以从客户端容器中获取会话存储
为例:
$this->client->getContainer()->get('session')->set('foo_bar', 'Bar');
注意:客户端容器只用于一个请求($client->request()
)。