Help for simple PhpUnit Action Test

Hello!

I wrote a small CMS in Slim, with authentication and so on. And it works fine :slight_smile:
But I’ haven’t done many PHPUnit tests so far. I’m a beginner in testing. My entities are tested, but it’s really simple.

My simplest action is this:

namespace App\Actions\Pages;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Views\ViewInterface;

class PagesAction
{
    private ViewInterface $view;

    public function __construct(ViewInterface $view)
    {
        $this->view = $view;
    }

    public function __invoke(Request $request, Response $response, array $args = []): Response
    {
        $page = $args['page'];

        $response = $this->view->render($response, $page, []);

        return $response;
    }
}

How can I test the action as easy as possible for me? I would like to understand it. Where is the best place to start?

I use my own slim skeleton, here, and not the official one. Was my other fun project.

Who can help me?

Thanks in advance

You could try to implement an HTTP test with Phpunit as follows

  • Create a Slim app instance
  • Create a Request object for the route you want to test
  • Invoke the “handle” method of the App instance and pass that request object.
  • Fetch the Response object and use the Phpunit assert methods to check the expected result.