无法从CakePHP Phpunit测试中读取cookie数据



我在测试框架中使用了CakePHP的构建来测试我的控制器。我有一个注销功能,该功能会在用户使用该站点时创建的各种cookie到期。我正在尝试阅读所述cookie,以确定测试是否应通过,即测试cookie是否正确过期。我已经确保正确实例化了cookie组件,但是我无法从应存在的cookie中读取任何值。这是组成我正在运行的测试的代码:

public function testLogout() {
    // setup the cookie component
    $collection = new ComponentCollection();
    $this->Cookie = new CookieComponent($collection);
    $result = $this->testAction('/users/logout');
    $cookie_name = Configure::read('tech_cookie_name');
    $cookie_data = $this->Cookie->read($cookie_name);
    debug($cookie_name);
    // cookie data is returning as NULL but I'm expecting some type of value.
    debug($cookie_data);
    debug($result);
    exit;
} 

我意识到出口正在尽早杀死测试,但是我正在使用它来查看是否从cookie寄回了任何东西。我不确定为什么我无法从我所知道的cookie中读取任何数据。有人知道为什么会这样,还是有一个解决方案,以便如何在单位测试中正确阅读cookie。

在某些情况下,您无法从Routes.php configure :: read()读取,这不是一个好习惯。它将在Localhost中起作用,但在Live中不起作用。尝试正确配置您的会话。通过从AppController和您的当前控制器(UserController)调用您的会话,您应该可以在测试操作中看到它。

public $components = array('Session', 'RequestHandler', 'Cookie', ...);

如果您这样写会话:

$this->Session->write('Test.tech_cookie_name', 'tech_cookie_name');

那么您应该能够这样阅读:

$this->Session->read('Test.tech_cookie_name');

最新更新