这是我的 Controller 代码中的操作:
//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);
}
当我执行测试时,controller中$data的值一直为空。如何从测试中保存 session 中的任何数据并从 Controller 重试?
//config_test.yml
framework:
test: ~
session:
storage_id: session.storage.mock_file
非常感谢
最佳答案
您可以从客户端的容器中获取 session 存储
例如:
$this->client->getContainer()->get('session')->set('foo_bar', 'Bar');
注意:客户端中的容器只用于一次请求($client->request()
)。
关于php - 测试 Symfony2 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23754982/