2016-04-23 121 views
2

目前我正在嘗試使用驗證組件檢索用戶標識的控制器。由於我對單元/集成測試相當陌生,因此我不知道如何使其工作。此外,所有我能找到的這個特殊問題的內容是爲CakePHP的版本編寫2.模擬驗證cakephp 3

控制器功能:

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' => '[email protected]', 
     ])); 

但是,我讀了staticExpects從PHPUnit的3.8版(我用5.2)棄用:我,我需要用的東西,看起來像這樣的其他問題看見。我應該如何嘲笑這個?

+0

我想你不明白這個問題。問題是我想在測試期間爲$ this-> Auth-> User('id')分配一個固定值(例如user_id始終爲1)。通過這種方式,我可以測試控制器操作是否像應該那樣工作。 – markvdlaan93

回答

1

與感謝橙花:),這爲我工作:

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

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

可以使用$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

你可以找到它在部分

測試需要身份驗證的操作

如果你想使用每個測試相同的會話數據,你可以使用你的集成測試類的設置方法,像這樣:

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

對不起,我想我忘記說我使用無狀態身份驗證,因此沒有使用任何會話。在正常情況下,這將是要走的路,但我無法找到Cakephp文檔中的特定問題。 – markvdlaan93

+1

你有沒有試過controllerSpy功能?像這樣: 'public function controllerSpy($ event) { parent :: controllerSpy($ event);如果(isset($ this - > _ controller)){ } – azahar

+0

不錯!那實際上完成了這項工作。我已經用全局狀態變量繞過它,但這是一個更好的解決方案。 – markvdlaan93