模拟auth cakephp 3

时间:2016-04-23 16:50:46

标签: cakephp phpunit integration-testing cakephp-3.0

目前,我正在尝试测试使用身份验证组件检索用户ID的控制器。由于我对单元/集成测试相当新,我不知道如何使其正常工作。此外,我为这个特定问题找到的所有内容都是为Cakephp版本2编写的。

控制器功能:

 public function favorite(){
    // Particular line where the problem is occurring:
    $userId = $this->Auth->User()['id'];
    // Get all favorite pictures of the user
    $query = $this->UsersPictures->getFavoritePictures($userId);

    $results = $query->all();
    // Replace the link in the result set by a presigned url of Amazon
    foreach($results as $result){
        $result->picture->link = $this->Aws->getTokenizedItem($result->picture->link);
    }
    $this->set([
        'success' => true,
        'data' => [
            'pictures' => $results
        ],
        '_serialize' => ['success', 'data']
    ]);
}

集成测试:

public function testFavoriteShouldPass(){
    $this->configRequest([
        'headers' => [
            'Accept' => 'application/json'
        ]
    ]);
    $this->get('api/pictures/favorite/1.json');

    $expected = [
        'success' => true,
        'data' => [
                [
                'user_id' => 1,
                'picture_id' => 1,
                'created' => '2016-04-03T20:35:40+0000',
                'modified' => '2016-04-03T20:35:40+0000',
                'picture' => [
                    'id' => 1,
                    'album_id' => 1,
                    'description' => 'Lorem ipsum dolor sit amet',
                    'link' => 'test',
                    'favorite' => true,
                    'created' => null,
                    'modified' => null,
                    'cover_photo' => true
                ]
            ]
        ]
    ];
    $this->assertEquals($expected, $response);
}

我的问题是如何为$ this-> Auth-> User()[' id']插入默认ID为1的用户。我在其他问题中看到我需要使用看起来像这样的东西:

   $this->_controller->Auth
        ->staticExpects($this->any())
        ->method('user')
        ->will($this->returnValue([
            'id' => 1,
            'username' => 'admin',
            'created' => '2013-05-08 00:00:00',
            'modified' => '2013-05-08 00:00:00',
            'email' => 'me@me.com',
        ]));

但是,我读到从phpunit版本3.8(我使用5.2)不推荐使用staticExpects。我该如何嘲笑这个?

2 个答案:

答案 0 :(得分:1)

感谢azahar :),这对我有用:

public function controllerSpy($event)
{
    parent::controllerSpy($event);

    if (isset($this->_controller)) {
        $this->_controller->Auth->setUser([
            'id' => 1,
            'username' => 'testtesttesttest',
            'email' => 'john@doe.com',
            'first_name' => 'John',
            'last_name' => 'Doe',
            'uuid' => 'wepoewoweo-ew-ewewpoeopw',
            'sign_in_count' => 1,
            'current_sign_in_ip' => '127.0.0.1',
            'active' => true
        ]);
    }
}

答案 1 :(得分:0)

您可以使用$this->session()在集成测试中设置会话数据,例如(取自cakephp book):

// Set session data
$this->session([
    'Auth' => [
        'User' => [
            'id' => 1,
            'username' => 'testing',
            // other keys.
        ]
    ]
]);

您实际上可以在他们的文档中找到它:http://book.cakephp.org/3.0/en/development/testing.html#controller-integration-testing

您可以在

部分找到它
  

测试需要身份验证的操作

如果要在每个测试中使用相同的会话数据,可以使用集成测试类的setUp方法,如下所示:

public function setUp()
{
    parent::setUp();
    // Set session data
    $this->session([
    'Auth' => [
        'User' => [
              'id' => 1,
              'username' => 'testing',
              // other keys.
            ]
        ]
    ]);
}