Controller unit test in slim3

Viewed 4864

At the outset, I would like to say - I'm new in unit testing in PHP (phpunit). In my new project (slim3 framework) I would like to test my controllers for example LoginController.

My idea is (in unit test method)

  • Create instance of LoginController
  • Mock some services in controller (DI)
  • Execute method which is response for request (in my controllers method __invoke)

My problem is about parameters for __invoke method. In Slim3 callable method for request has two first params:

RequestInterface $request and ResponseInterface $response

How can I create this parameters in my unit test class? I was searching for some examples for this issue but without success.

Any suggestions?

I've found some code in Slim3 tests to mock request:

protected function requestFactory()
{
    $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123');
    $headers = new Headers();
    $cookies = array(
        'user' => 'john',
        'id' => '123',
    );
    $env = Slim\Http\Environment::mock();
    $serverParams = $env->all();
    $body = new Body(fopen('php://temp', 'r+'));
    $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);

    return $request;
}

But I'm not sure that is good way.

Thanks for any help

1 Answers
Related