模拟身份验证 cakephp 3

Mock auth cakephp 3

目前我正在尝试测试使用身份验证组件检索用户 ID 的控制器。由于我是 unit/integration 测试的新手,所以我不知道如何让它工作。此外,我能找到的针对这个特定问题的所有内容都是为 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',
        ]));

但是,我了解到 staticExpects 从 phpunit 版本 3.8 开始被弃用(我使用的是 5.2)。我应该如何模拟这个?

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

// 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

您可以在

部分找到它

Testing Actions That Require Authentication

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

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

感谢 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
        ]);
    }
}