PHPUnit测试添加操作-测试失败
CakePHP版本:3.5.17PHPUnit测试添加操作-测试失败,phpunit,cakephp-3.0,Phpunit,Cakephp 3.0,CakePHP版本:3.5.17 PHPUnit:6.5.8 示例代码: 用户控制器添加操作。(编码哪些错误。) 客户端id功能。 public function add() { $user = $this->Users->newEntity(); if ($this->request->is('post')) { // Initialise the client id. if ($this->clientId(
PHPUnit:6.5.8 示例代码: 用户控制器添加操作。(编码哪些错误。) 客户端id功能。
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.'));
}
}
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;
}
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.'));
}
}
过程。
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.'));
}
}
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;
}
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并将其存储在会话中,并在应用程序中的许多select语句中使用它
错误
未定义变量clientID-例如:未从函数中检索客户端id,该函数在保存时出错
摘要。
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.'));
}
}
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;
}
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.'));
}
}
这对我来说很有意义,因为我可以在不登录的情况下运行单元测试,并且在登录时检索客户端id。测试时,客户id怎么可能在那里
我的解决方案。
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.'));
}
}
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;
}
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
我为什么问。
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.'));
}
}
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;
}
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.'));
}
}
它与性能有关。据我所知,从会话中声明值比从数据库中选择值更快
感谢Z.上的手册部分介绍了如何在会话中设置变量。但它不仅限于Auth部分;在测试代码中,可以设置所需的任何会话变量。就你而言:
$this->session(['Cid.one' => XXX]);
在显示的代码中不可能有“undefined variable”错误,逻辑要么抛出异常,要么定义变量。请确保您发布了正确的代码。@ndm-很抱歉,我在调试时注释掉了异常以显示未定义的变量错误,但在发布时忘记注释“输入输出”。我现在已经编辑了我的文章,所以应该读对了。你是否尝试过使用
$this->session(['Cid.one'=>XXX])代码>?似乎您知道使用$this->session
编写身份验证数据;在会话中写入您可能需要的任何内容也应该是有效的。@Greg Schmidt-我确实尝试过,但仍然无法通过测试,但我刚刚又尝试了一次,效果很好,所以我不确定上次做错了什么。而且,即使它成功了,我仍然不相信我正确地使用了这个框架,因此得到确认是一个很大的帮助。如果你把这个作为一个答案,我会打勾作为正确的,非常感谢你的帮助。