Derzeit versuche ich einen Controller zu testen, der die Authentifizierungskomponente verwendet, um die Benutzer-ID abzurufen. Da ich zu Unit/Integration Tests ziemlich neu bin, habe ich keine Ahnung, wie dies funktioniert. Darüber hinaus alle Inhalte, die ich für dieses spezielle Problem für CakePHP Version geschrieben finden konnten 2.Mock auth cakephp 3
Reglerfunktion:
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']
]);
}
Test Integration:
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);
}
Meine Frage ist, wie kann ich Fügen Sie einen Benutzer mit der Standard-ID 1 für $ this-> Auth-> User() ['id'] ein. Ich sah in anderen Fragen, die ich brauche etwas zu verwenden, das wie folgt aussieht:
$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]',
]));
Allerdings habe ich gelesen, dass staticExpects von phpunit Version ist veraltet 3.8 (Ich bin mit 5.2). Wie soll ich das verspotten?
Ich denke, Sie haben die Frage nicht verstanden. Das Problem ist, dass ich $ this-> Auth-> User ('id') während des Testens einen festen Wert zuweisen möchte (zum Beispiel user_id immer 1). Auf diese Weise kann ich testen, ob die Controller-Aktionen so funktionieren, wie sie sein sollten. – markvdlaan93