PHPUnit 测试添加操作 - 测试失败
PHPUnit testing the add action - Test failure
CakePHP 版本:3.5.17
PHPUnit:6.5.8
示例代码:
用户控制器添加操作。 (代码错误。)
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
// Initialise the client id.
if ($this->clientId() === false) {
//$errorLocation = 'Users Controller - Line ' . __LINE__;
//if ($this->recordError($errorLocation) === false) {
//throw new UnauthorizedException();
//}
//throw new UnauthorizedException();
}
else {
$clientID = $this->clientId();
}
$user = $this->Users->patchEntity($user, $this->request->getData());
// Declare the client id for save.
$user->cid_1 = $clientID;
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
客户端 id 函数。
public function clientId()
{
$session = $this->request->session();
if ($session->check('Cid.one')) {
$clientID = $session->read('Cid.one');
if (!is_string($clientID) || is_numeric($clientID) || (strlen($clientID) !== 40)) {
return false;
}
return $clientID;
}
return false;
}
进程。
当用户登录时,我 select $clientID 并将其存储在会话中,并在应用程序中的许多 select 语句中使用它。
错误.
未定义的变量 clientID - 例如:未从保存时出错的函数中检索客户端 ID。
总结。
这对我来说很有意义,因为我可以在不登录的情况下 运行 进行单元测试,并且在登录时检索客户端 ID。 EG: 测试的时候client id怎么会有啊!
我的解决方案。
我没有使用会话,而是使用如下所示的查找器。
用户控制器添加操作。 (通过的代码。)
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
// Declare the id from auth component.
$id = $this->Auth->user('id');
// Select the client id.
$query = $this->Users->find('cid', [
'id' => $id
]);
if ($query->isEmpty()) {
$errorLocation = 'Users Controller - Line ' . __LINE__;
if ($this->recordError($errorLocation) === false) {
throw new NotFoundException();
}
throw new NotFoundException();
}
// Initialise the variables and retrieve the data.
$clientID = '';
foreach ($query as $row):
$clientID = $row->cid_1;
endforeach;
$user = $this->Users->patchEntity($user, $this->request->getData());
// Declare the client id for save.
$user->cid_1 = $clientID;
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
我的问题:
有没有办法从测试会话中模拟 $clientID?
我想知道是否有类似于使用的东西:$this->session(['Auth.User.id' => 1400]);在我的模拟测试中
经过身份验证的用户,但对于其他会话数据,例如客户端 ID?
为什么我问。
这与性能有关。据我所知,从会话中声明一个值比从数据库中声明一个值 select 更快。
谢谢 Z.
Testing Actions That Require Authentication 上的手册部分展示了如何在会话中设置变量。但它不仅限于 Auth 部分;在您的测试代码中,您可以设置任何所需的会话变量。你的情况:
$this->session(['Cid.one' => XXX]);
CakePHP 版本:3.5.17
PHPUnit:6.5.8
示例代码:
用户控制器添加操作。 (代码错误。)
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
// Initialise the client id.
if ($this->clientId() === false) {
//$errorLocation = 'Users Controller - Line ' . __LINE__;
//if ($this->recordError($errorLocation) === false) {
//throw new UnauthorizedException();
//}
//throw new UnauthorizedException();
}
else {
$clientID = $this->clientId();
}
$user = $this->Users->patchEntity($user, $this->request->getData());
// Declare the client id for save.
$user->cid_1 = $clientID;
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
客户端 id 函数。
public function clientId()
{
$session = $this->request->session();
if ($session->check('Cid.one')) {
$clientID = $session->read('Cid.one');
if (!is_string($clientID) || is_numeric($clientID) || (strlen($clientID) !== 40)) {
return false;
}
return $clientID;
}
return false;
}
进程。
当用户登录时,我 select $clientID 并将其存储在会话中,并在应用程序中的许多 select 语句中使用它。
错误.
未定义的变量 clientID - 例如:未从保存时出错的函数中检索客户端 ID。
总结。
这对我来说很有意义,因为我可以在不登录的情况下 运行 进行单元测试,并且在登录时检索客户端 ID。 EG: 测试的时候client id怎么会有啊!
我的解决方案。
我没有使用会话,而是使用如下所示的查找器。
用户控制器添加操作。 (通过的代码。)
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
// Declare the id from auth component.
$id = $this->Auth->user('id');
// Select the client id.
$query = $this->Users->find('cid', [
'id' => $id
]);
if ($query->isEmpty()) {
$errorLocation = 'Users Controller - Line ' . __LINE__;
if ($this->recordError($errorLocation) === false) {
throw new NotFoundException();
}
throw new NotFoundException();
}
// Initialise the variables and retrieve the data.
$clientID = '';
foreach ($query as $row):
$clientID = $row->cid_1;
endforeach;
$user = $this->Users->patchEntity($user, $this->request->getData());
// Declare the client id for save.
$user->cid_1 = $clientID;
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
我的问题:
有没有办法从测试会话中模拟 $clientID?
我想知道是否有类似于使用的东西:$this->session(['Auth.User.id' => 1400]);在我的模拟测试中 经过身份验证的用户,但对于其他会话数据,例如客户端 ID?
为什么我问。
这与性能有关。据我所知,从会话中声明一个值比从数据库中声明一个值 select 更快。
谢谢 Z.
Testing Actions That Require Authentication 上的手册部分展示了如何在会话中设置变量。但它不仅限于 Auth 部分;在您的测试代码中,您可以设置任何所需的会话变量。你的情况:
$this->session(['Cid.one' => XXX]);