【问题标题】:How to set some data in session in functional test?如何在功能测试的会话中设置一些数据?
【发布时间】:2018-10-03 20:57:20
【问题描述】:

我使用"liip/functional-test-bundle": "~1.8","symfony/symfony": "~3.4",,对于我的功能测试,我想在会话中设置一些数据,但是在使用相同的键设置数据后,我得到了 null。 Mytest 类扩展自extends WebTestCase

这就是我在测试方法中设置数据的方式

    $session = $this->container->get('session');
    $session->set('test', 2);

    $this->getClient()->request(
        Request::METHOD_GET,
        $routing
    );

config_test.yml

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

在行动中

public function getEntityAction()
{
    $test = $this->get('session')->get('test');

变量$test等于null 如何在功能测试的会话中设置一些数据?

调试时,请求前

session = {Symfony\Component\HttpFoundation\Session\Session} [5]
 storage = {Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage} [8]
  savePath = "/tmp"
  id = "d5d7233a843594e94c42b6a0e5a14a86c2518fb5f0a6478732841547b3e93fd6"
  name = "MOCKSESSID"
  started = true
  closed = false
  data = {array} [3]
   _sf2_attributes = {array} [1]
    test = 2

在行动中

$y = {Symfony\Component\HttpFoundation\Session\Session} [5]
    storage ={Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage} [8]
    savePath = "/home/ivan/hosts/fix/var/cache/test/sessions"
    id = "5ade2581b9764"
    name = "MOCKSESSID"
    started = true
    closed = false
    data = {array} [3]
        _sf2_attributes = {array} [1]
           _security_main = "authuser data"

我不明白为什么 id 和 savePath 不同?

【问题讨论】:

    标签: php symfony liipfunctionaltestbundle


    【解决方案1】:

    here所写:

    我认为这里的主要错误是忘记$this->container$client->getContainer() 不同:客户端旋转一个完全分离的新容器,并且根据您使用会话的方式,您可能找不到相同的东西在他们两个中。

    因此,当您致电 $this->container->get('session')->set('test', 2) 时,您不会更改测试的实际会话。


    所以,如果你调用容器的客户端,它可能会起作用:

    $client = $this->makeClient();
    $client->getContainer()->set('test', 2);
    $crawler = $client->request('GET', '/contact');
    

    或者,为了规避这个问题,您可以编写一个不依赖于直接设置会话的测试,而是执行与用户相同的操作以填充会话并调用使用它的控制器.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多