Hi folks,
newbie here. I am trying to write some tests, but I keep banging my head to a metaphorical and a literal wall with it.
I am trying to test add user function, here’s the test:
/**
* Test add method
*
* @return void
*/
public function testAdd(): void
{
$this->get('/users/add');
$this->assertResponseOk();
$data = [
'firstname' => 'Rio',
'lastname' => 'Grande',
//'username' => 'riogrande@gmail.com',
//'email' => 'riogrande@gmail.com',
//'password' => 'riorio'
];
$this->post('/users/add', $data);
//$this->assertResponseContains('The user has been saved.');
$expected = [
[
'firstname' => 'Rio',
'lastname' => 'Grande',
// 'username' => 'riogrande@gmail.com',
'email' => 'riogrande@gmail.com',
// 'password' => 'riorio'
]
];
$users = TableRegistry::get('users');
$query = $users->find('all', [
'fields' => ['Users.firstname', 'Users.lastname', 'Users.username', 'Users.email', 'Users.password'],
'conditions' => ['Users.email' => 'riogrande@gmail.com'],
]);
$result = $query->enableHydration(false)->toArray();
$this->assertEquals($expected, $result);
}
Here is the add function in UsersController:
/**
* Add method
*
* @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
*/
public function add()
{
$user = $this->Users->newEmptyEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->getData());
$user->user_id = $this->Auth->user('id');
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.'));
}
}
And the problem while running phpunit test: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘Users.firstname’ in ‘field list’
The app is functioning otherwise and I can add new users and data goes to database, but since testing with Cake is something new for me it would be nice to start with kind of easy test and get it to work.
Please: explain to me in plain English, why the test can’t find my Users.firstname (I tried with Users.email and Users.password and got the same error). Do I have to select the table/database somewhere? What can I do to continue with this?
All the code is stolen from other cake users (thanks and sorry) and it’s probably from Cake version 3 or prior, does it affect?
I really appreciate all input. Have a nice weekend!