【问题标题】:Symfony 2.8 functional testing - add values to the session arraySymfony 2.8 功能测试 - 向会话数组添加值
【发布时间】:2018-06-27 07:48:28
【问题描述】:

我有一个控制器,它以这种方式从会话数组中读取一个整数:

if ($session->has('administration'))
{
     $id = $session->get('entity_id');
     return $this->getDoctrine()->getRepository('AppBundle:SomeEntity')->find($id);
}

目前我正在为此控制器操作编写测试,但我找不到如何在会话数组中注入这个整数。我尝试了以下方法:

$this->client = $this->makeClient(true);

$this->client->getContainer()->set('session.storage.options.entity_id', 12);

但是,这不起作用,似乎只是忽略了该值。因此,我的问题是如何在功能测试环境中将值存储在会话数组中? (注意:我使用的是 Liip 测试包)

可能会产生影响的额外配置

// config_test.yml
liip_functional_test:
cache_sqlite_db: true
authentication:
    username: "%test.username%"
    password: "%test.password%"

framework:
    test: ~
    session:
       storage_id: session.storage.mock_file
    profiler:
       collect: false

【问题讨论】:

    标签: php symfony session phpunit


    【解决方案1】:

    Symfony 作为测试会话的 Mock,定义为 here in the doc

    use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
    use Symfony\Component\HttpFoundation\Session\Session;
    
    $session = new Session(new MockFileSessionStorage());
    $session->set('entity_id', 12);
    

    【讨论】:

    • 不。我已经尝试过这个$session->set('entity_id', 12);,但它仍然是空的。这可能是因为这个新会话需要以某种方式连接到客户端。
    • 即使之后我尝试$this->client->getContainer()->set('session', $session);,它仍然是空的。
    猜你喜欢
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2021-03-17
    • 2018-01-07
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 2016-03-11
    相关资源
    最近更新 更多