2016-04-23 18 views
2

現在、ユーザーIDを取得するために認証コンポーネントを使用するコントローラをテストしようとしています。私は単体テスト/統合テストにはまったく新しいので、これをどのように動作させるのか分かりません。モック認証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); 
} 

私の質問はどのように私はできている。また、私はこの特定の問題のために見つけることができるすべてのコンテンツが2

コントローラ機能CakePHPのバージョン用に書かれています$ this-> Auth-> User()['id']にデフォルトのIDが1のユーザーを挿入します。しかし、私はstaticExpectsはPHPUnitのバージョン3.8(私は5.2を使用しています)から廃止されていることを読んで

$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]', 
     ])); 

:私は、私はこのようなものを使用する必要がある他の質問で見ました。どのように私はこれを嘲笑すべきですか?

+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); {$ this-> _ controller-> Auth-> setUser(['username' => 'admin'、 'is_admin' => true]);} } ' – azahar

+0

ニース!それは実際に仕事をしました。私はすでにグローバルな状態変数でそれをバイパスしましたが、これははるかに良い解決策です。 – markvdlaan93