cakephp 4 需要身份验证的测试操作
cakephp 4 Testing Actions That Require Authentication
需要身份验证的测试操作与描述的不一样cakephp manual。
我找到了这个解决方案,它是正确的吗?
use App\Model\Entity\User;
...
public function testLogout()
{
$user=new User();
$user->id=1;
$this->session(['Auth' => $user]);
$this->get ( '/Users/logout' );
$this->assertRedirect(['controller' => 'Users', 'action' => 'login']);
}
解决方案是(感谢 ndm):
use Cake\ORM\TableRegistry;
...
protected function login($userId = 1)
{
$users = TableRegistry::getTableLocator()->get('Users');
$user = $users->get($userId);
$this->session(['Auth' => $user]);
}
...
$this->login(1);
$this->enableRetainFlashMessages();
$this->get ( '/Users/logout' );
....
需要身份验证的测试操作与描述的不一样cakephp manual。
我找到了这个解决方案,它是正确的吗?
use App\Model\Entity\User;
...
public function testLogout()
{
$user=new User();
$user->id=1;
$this->session(['Auth' => $user]);
$this->get ( '/Users/logout' );
$this->assertRedirect(['controller' => 'Users', 'action' => 'login']);
}
解决方案是(感谢 ndm):
use Cake\ORM\TableRegistry;
...
protected function login($userId = 1)
{
$users = TableRegistry::getTableLocator()->get('Users');
$user = $users->get($userId);
$this->session(['Auth' => $user]);
}
...
$this->login(1);
$this->enableRetainFlashMessages();
$this->get ( '/Users/logout' );
....