【发布时间】:2020-10-15 12:18:08
【问题描述】:
我用 cakephp 4.x 编写了一个 webaplication。在我的控制器(AppController.php)中,我实现了以下内容:
...
public function beforeRender(\Cake\Event\EventInterface $event)
{
#Load User_ID
if($this->Authentication->getIdentity()){
$identity = $this->Authentication->getIdentity();
$user_id = $this->Authentication->getIdentity()->getIdentifier();
}
}
...
现在我编写了以下 phpunit 测试:
...
private function loginAsAdmin() {
$this->Users = TableRegistry::getTableLocator()->get('Users');
$user = $this->Users->get(1);
$this->session(['Auth.User'=> $user->toArray()]);
}
/**
* Test loginAsUser method
*
* @return void
*/
public function testLoginAsAdmin(): void
{
$this->loginAsAdmin();
$this->get('/admin/dashboard');
$this->assertSession(1, 'Auth.User.id'); //check if the user is logged in
}
...
但我的身份对象总是空的。
对象实际上应该是这样的:
我没有更多的想法如何实现它。有人能帮我吗? :)
【问题讨论】:
标签: php authentication cakephp phpunit cakephp-4.x