Welcome Guest, Not a member yet? Register   Sign In
Testing a controller that has validation: getVar don't work
#1

(This post was last modified: 02-28-2023, 02:27 AM by evandroagnes.)

Hi everyone,
I'm trying to code a unit test for a controller that has validation but it seems that the method $request->getVar don't work when you set de request in the test:
Code:
<?php

namespace App\Controllers;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\ControllerTestTrait;
use CodeIgniter\Test\DatabaseTestTrait;
use Config\Services;

class TaskModelTest extends CIUnitTestCase
{
    use DatabaseTestTrait;
    use ControllerTestTrait;

    public function testStore()
    {
        $request = Services::request();
        $request = $request->withMethod('post');
        $request->setGlobal('post', [
            'user_id' => '1',
            'type'  => 'test',
            'description' => 'This is a test!',
        ]);

        $testResponse = $this->withRequest($request)
            ->controller(Task::class)
            ->execute('store');

        $url = $testResponse->getRedirectUrl();
        $this->assertEquals(site_url('tasks-list'), $url);
    }
}

If you get the value from the request:
PHP Code:
        var_dump($request->getVar('description'));
        var_dump($request->getPost('description')); 

The first one results: NULL and the second: string(15) "This is a test!", that is corret.
The problem is that the validation uses the method getVar internally, resulting in a bad behavior in the test.

Here the store function in the controller:
PHP Code:
    // insert data
    public function store()
    {
        helper(['form']);

        $data = [
            'user_id' => $this->request->getVar('user_id'),
            'type'  => $this->request->getVar('type'),
            'description'  => $this->request->getVar('description'),
            'status'  => 1// open
        ];

        if ($this->validate($this->getValidationRules())) {
            $taskModel = new TaskModel();
            $taskModel->insert($data);

            return redirect()->to('tasks-list');
        } else {
            $data['validation'] = $this->validator;

            return $this->create($data);
        }
    

As I said above, if I use getPost I can get the values, but the validation fails because uses getVar.

I'm using the codeigniter version 4.2.11 and php 7.4.

Thanks in advance!
Reply
#2

I think using getPost() is better when you use POST requests.
Why do you need to use getVar()?

Ah, $this->validate() uses getVar()!

1. use $this->validateData() https://codeigniter4.github.io/CodeIgnit...lidatedata
2. set 'request' in the test.
PHP Code:
        $request->setGlobal('request', [
            'user_id' => '1',
            'type'  => 'test',
            'description' => 'This is a test!',
        ]); 
Reply
#3

(03-01-2023, 12:30 AM)kenjis Wrote: I think using getPost() is better when you use POST requests.
Why do you need to use getVar()?

Ah, $this->validate() uses getVar()!

1. use $this->validateData() https://codeigniter4.github.io/CodeIgnit...lidatedata
2. set 'request' in the test.
PHP Code:
        $request->setGlobal('request', [
            'user_id' => '1',
            'type'  => 'test',
            'description' => 'This is a test!',
        ]); 

For now I setted 'request' in the test and it works! But I'll change all validations to use 'validateData' because seem to me the right way to solve this.

Thanks!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB