php - 测试 Symfony2 session

标签 php session symfony testing functional-testing

这是我的 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/

相关文章:

php - session 变量与 Mysql 表

javascript - 如何使用 Ajax 调用特定 Controller 操作?

php - Symfony 3 形式 : how to set default value for a textarea widget

php - 在数据库表中搜索大量关键字

php - 在 php 和 mysql 中重新打开页面时在下拉列表中显示保存的数据

scala - play 框架中的服务器端 session

PHP $_SESSION 数据返回空

forms - Symfony2 表单 : Changing the default prefix in Form Labels

php - jw播放器: Error loading player: No playable sources found

来自sql结果的Php数组