0

I'm doing cakephp4 controller test with phpUnit but when I call save my id auto_increment disapear.

My table before save(): Image before save

The test:

public function testAdd(): void
   {
       $this->session([
           'Auth' => [
                   'id' => 1,
                   'DNI_CIF' => '22175395Z',
                   'name' => 'Prueba',
                   'lastname' => 'Prueba Prueba',
                   'username' => 'Pruebatesting',
                   'password' => 'prueba',
                   'email' => 'prueba@gmail.com',
                   'phone' => '639087621',
                   'role' => 'admin',
                   'addres_id' => 1
              
           ]
       ]);
       $this->get('animal/add');
 
       $this->assertResponseOk();
 
       $data=[
           'id' => 1,
           'name' => 'AñadirAnimal',
           'image' => '',
           'specie' => 'dog',
           'chip' => 'no',
           'sex' => 'intact_male',
           'race' => 'cat',
           'age' => 1,
           'information' => 'Es un animal.',
           'state' => 'sick',
           'animal_shelter' => [
               'id' => 1,
               'start_date' => '2022-11-03 10:47:38',
               'end_date' => '2022-11-03 10:47:38',
               'user_id' => 1,
               'animal_id' => 1
           ]
       ];
       $this->enableCsrfToken();
 
       $this->post('animal/add',$data);
      $this->assertResponseOk();
   }

The controller:

   public function add()
   {
       $animal = $this->Animal->newEmptyEntity();
       if ($this->request->is('post')) {
           $animal = $this->Animal->patchEntity($animal, $this->request->getData());
 
            if(!$animal->getErrors){
 
               $image = $this->request->getData('image_file');
               if($image !=NULL){
                   $name  = $image->getClientFilename();
               }
 
               if( !is_dir(WWW_ROOT.'img'.DS.'animal-img') ){
                   mkdir(WWW_ROOT.'img'.DS.'animal-img',0775);
                   if($name){
                       $targetPath = WWW_ROOT.'img'.DS.'animal-img'.DS.$name;
 
                       $image->moveTo($targetPath);
                          
                       $animal->image = 'animal-img/'.$name;
                   }
               }
 
               if ($this->Animal->save($animal)) {
                   $this->Flash->success(__('El animal se ha añadido.'));
                   return $this->redirect(['action' => 'index']);
               }
           }
           $this->Flash->error(__('El animal no se ha podido añadir, por favor intentalo de nuevo'));
       }
       $allUsers = $this->getTableLocator()->get('User');
 
       $user = $allUsers->find('list', ['limit' => 200])->all();
       $this->set(compact('animal','user'));
   }

My table after: Image after save The error:


1) App\Test\TestCase\Controller\AnimalControllerTest::testAdd
Possibly related to PDOException: "SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value"
…
Failed asserting that 500 is between 200 and 204.

I don't know why this is happening or how to know the reason. In the app the controller works fine. Data in the app: Data in app

Data in test: Data in test

I hope someone can help me, I don't know what to try anymore or how to know where the problem is...

I tried to look at the data but it doesn't apear to have any errors so I don't know where the error can be.

Iria
  • 9
  • 4

1 Answers1

1

It was that the sql file used in the bootstrap didn't have the autoincrement value.

Iria
  • 9
  • 4