How to test a controller?

I wanna test a controller but I still don’t find a way to do it smoothly. For example, I wanna try a GET method with no parameters.

class HomeController extends BaseController
{ 
    public function getHome(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
    {
        $page = $request->getQueryParam('page', 1);
        $polls = \App\Models\Poll::where('user_id', $_SESSION['user']['id'])->paginate(5, ['*'], 'page', $page);
        $pages = $polls->links(new \Illuminate\Pagination\BootstrapFourPresenter($polls));
        $data  = ['polls' => $polls,
                    'pages' => $pages]; 
        
        return $this->c->get('view')->render($response, 'home.twig', $data);
    }
...
}

Nothing too complex going on (a page retrieves a collection of objects from the database based on a query) but I don’t get how to create a test for this that doesn’t involve creating a little new Slim app recreating everything from scratch. There’s very little information on how to test and the only thing I found is a case that’s too simple to fit this case.

Well in the end I got around the issue using the BaseTestCase of the skeleton which I hadn’t read very well. I just had to add a few more things to its container to make it work. Sorry.

If you haven’t come across it already, you may find this Testing Slim Framework actions article helpful.

1 Like