cakephp 3 undefined属性cookie组件单元测试

时间:2017-10-19 08:42:54

标签: unit-testing cakephp cookies phpunit cakephp-3.0

我试图通过单元测试来测试我的组件功能。 我的组件功能

public function userRole() { 

    $loginId = $this->Cookie->read('Admin.login_id');
    $name = $this->Cookie->read('Admin.name'); 
    $role = $this->Cookie->read('Admin.role'); 
    if (empty($loginId) || empty($name)){ 
        return false; 
    }

    $adminsORM = TableRegistry::get('Admins');
    $admin = $adminsORM->find('all', [
        'conditions' => ['login_id' => $loginId, 'name' => $name, 'disable' => 0]
    ])->first(); 



    return empty($admin)? false : $admin->role; 
}

我的组件测试功能

public $Acl;

public function setUp()
{
    parent::setUp();
    $registry = new ComponentRegistry();
    $this ->Acl = new AclComponent($registry);
}

public function testUserRole()
{
    // Test our adjust method with different parameter settings       
        $this->Cookie->write('Admin.login_id', 'demo12');
        $this->Cookie->write('Admin.role', 1);        
        $this->Cookie->write('Admin.name', 'demo 12');   
    $output = $this->Acl->userRole();
    $this->assertResponseOk();

}

作曲家测试代码

vendor/bin/phpunit --filter testUserRole /d/xampp/htdocs/admin/admin/tests/TestCase/Controller/Component/AclComponentTest.php

错误

  

注意错误:未定义的属性:[D:\ xampp \ htdocs \ admin \ admin \ tests \ TestCase \ Controller \ Component \ AclComponentTest.php,line中的App \ Test \ TestCase \ Controller \ Component \ AclComponentTest :: $ Cookie 31]

2 个答案:

答案 0 :(得分:2)

如错误所示,单元测试中没有$this->Cookie属性。我只能假设组件中的$this->Cookie引用了Cookie组件(从CakePHP 3.5开始不推荐使用它)。

如果您需要准备常规单元测试的cookie,而不是控制器/集成测试(您可以使用IntegrationTestCase::cookie()IntegrationTestCase::cookieEncrypted()IntegrationTestCase::assertResponseOk()方法),那么您必须将cookie直接写入请求对象,并确保将其提供给组件。

查看Cookbook中关于如何测试组件的示例,它应该如下所示:

namespace App\Test\TestCase\Controller\Component;

use App\Controller\Component\MyComponent;
use Cake\Controller\Controller;
use Cake\Controller\ComponentRegistry;
use Cake\Http\ServerRequest;
use Cake\Http\Response;
use Cake\TestSuite\TestCase;

class MyComponentTest extends TestCase
{
    public $component = null;
    public $controller = null;

    public function setUp()
    {
        parent::setUp();

        $request = new ServerRequest();
        $response = new Response();

        $this->controller = $this->getMockBuilder('Cake\Controller\Controller')
            ->setConstructorArgs([$request, $response])
            ->setMethods(null)
            ->getMock();

        $registry = new ComponentRegistry($this->controller);
        $this->component = new MyComponent($registry);
    }

    // ...
}

然后,您可以在setUp()方法中定义cookie,以便它们在所有测试中都可用,或者您可以在每个测试中单独定义它们。另请注意,如果您正在使用加密Cookie,则应使用CookieCryptTrait::_encrypt()加密Cookie数据。

// ...
use Cake\Utility\CookieCryptTrait;
use Cake\Utility\Security;

protected function _getCookieEncryptionKey()
{
    // the cookie component uses the salt by default
    return Security::getSalt();
}

public function testUserRole()
{
    $data = [
        'login_id' => 'demo12',
        'role' => 1,
        'name' => 'demo 12'
    ];
    // the cookie component uses `aes` by default
    $cookie = $this->_encrypt($data, 'aes');

    $request = new ServerRequest([
        'cookies' => [
            'Admin' => $cookie
        ]
    ]);
    $this->controller->request = $request;

    $output = $this->Acl->userRole();
    $this->assertEquals('expected value', $output);
}

另见

答案 1 :(得分:1)

根据testing documentation,为了在测试用例期间设置Cookie,您需要使用$this->cookieEncrypted('my_cookie', 'Some secret values')函数:

$this->cookieEncrypted('Admin.login_id', 'demo12');
$this->cookieEncrypted('Admin.role', 1);        
$this->cookieEncrypted('Admin.name', 'demo 12');