Testing a controller that has validation: getVar don't work - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Testing a controller that has validation: getVar don't work (/showthread.php?tid=86939) |
Testing a controller that has validation: getVar don't work - evandroagnes - 02-28-2023 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 If you get the value from the request: PHP Code: var_dump($request->getVar('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 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! RE: Testing a controller that has validation: getVar don't work - kenjis - 03-01-2023 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/CodeIgniter4/incoming/controllers.html#this-validatedata 2. set 'request' in the test. PHP Code: $request->setGlobal('request', [ RE: Testing a controller that has validation: getVar don't work - evandroagnes - 03-01-2023 (03-01-2023, 12:30 AM)kenjis Wrote: I think using getPost() is better when you use POST requests. 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! |