• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting request method and params when testing with ControllerTester

#1
Hello,

I'm using CI 4.0.4 and writing a test using the ControllerTester.
I think it helps to add a convenient way to set request method and parameters there.

Here's my experience so far.
First, I wrote a test case sending a get request, which is the default when running a controller as described on the doc, and it worked fine.
It simply specified my controller and executed it, like:
PHP Code:
class MyTestCase extends CIDatabaseTestCase
{
    use ControllerTester;
    public function testGetIndex()
    {
        $result $this->controller(\App\Controllers\MyController::class)->execute('index');
        // assert this and that.
    }

Next, I wanted to write a test case sending a post request similarly, but it wasn't clear to me how.
I looked at CodeIgniter4/tests/system/Test/ControllerTesterTest.php to see if there's any example sending a post request, but there's not. I searched around and found that setting $_SERVER['REQUEST_METHOD'] = 'post' would work to set request method, and $_POST[$key] = $value to set request parameters. With my controller and my test case, it worked for setting the request method, but didn't seem to be working for setting the request parameters.
I also tried FeatureTestTrait because it's easy to specify a request method and params, but there's a complication with my filters and I couldn't make it work (a RedirectResponse was returned from somewhere?).
I came back to ControllerTester and chased it down, and found that my controller has validation code and the validator takes request parameters through $request->getVar() where the getVar() gets parameters from $_REQUEST.
So, I needed to set the same parameters to both $_POST and $_REQUEST, and it finally worked that way.
I haven't tried it, but an alternative would be to create an IncomingRequest and set things up. However, it still seems a bit tricky to me, one reason is that it may not be immediately clear (at least for me) from the method name setGlobal() that one can set request parameters through the method, and another reason is that one need to call the method twice, like 'post' and 'request', in some cases for non-obvious reason.

It looks like the FeatureTestTrait takes care of those things (as well as session) here,
while ControllerTester leaves them users' responsibility now.
I think it's common to test controllers with different request methods and parameters, so testing would be easier by adding a few methods to specify request method and parameters.

A quick idea is to add ControllerTester.withMethod(), withParameters(), and maybe withSession() as well,
or withMethodAndParams() to pass them together, but there might be some other ideas that work better with the CI and its implementation.

Thank you
Reply

#2
I haven’t actually used Controller tests but Feature Tests would definitely cover what you’re trying to do.
Reply

#3
Thank you for your reply.
Yes, that's one way, but my point is that the ControllerTester probably should cover it too because I guess it's a common use case, and that's why I posted this as a feature request. What do you think?
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.