現在、ユーザー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]',
]));
:私は、私はこのようなものを使用する必要がある他の質問で見ました。どのように私はこれを嘲笑すべきですか?
あなたはその質問を理解していないと思います。問題は、テスト中に$ this-> Auth-> User( 'id')に固定値を割り当てたいということです(たとえばuser_idは常に1)。このようにして、コントローラーの動作が正常に動作するかどうかをテストできます。 – markvdlaan93