2

I am quite lost with how I should test the constructor. I have basic knowledge of how test are suppose to work but in this case, I do not know how I should proceed.

I have a constructor that handles rest request.

    public function getStuffAction(Request $request) {
        $searchString = $request->query->get('q', '');

        if ($searchString === '')
            $stuff = $this->getManager('stuff')->findAll();
        else
            $stuff = $this->getManager('stuff')->findBySearchString($searchString);

        $view = View::create();
        $view->setData($stuff)->setStatusCode(200);
        return $view;
    }

    public function getManager($entityName) {
        return $this->get('app.' . $entityName . '.manager');
    }

I understand that I need to test the behaviour of my function. I see that I should test when the query is empty, when it is not and probably if it return something.

Be aware that I am new to Symfony.

From what I read, I should inject the manager as a parameter in getStuffAction so it can then be mocked but I don't know how.

  • Have you check this http://stackoverflow.com/questions/10427282/symfony-2-entitymanager-injection-in-service – Maxime Roussin-Bélanger Mar 24 '16 at 18:13
  • I have but it's not what I'm looking for. I'm not calling a service. This is a REST call. When a call is made to the API, via routing rules, it is redirected to this function directly. – Stéphane Pelletier-Lamothe Mar 24 '16 at 18:17
  • You can use the injection like in the link to inject the manager into your controller constructor no? – Maxime Roussin-Bélanger Mar 24 '16 at 18:22
  • From what I understand, no. But I might be wrong. – Stéphane Pelletier-Lamothe Mar 24 '16 at 18:27
  • Your best bet for controller actions is to create functional (as opposed to unit) tests. http://symfony.com/doc/current/book/testing.html#functional-tests To do a true unit test (which is usually a waste of time for controllers) you would need to mock up your manager and the request and possible a few other things. You will also want to refactor the action and inject a view instead of creating it with a static function. Statics can be hard to test. – Cerad Mar 24 '16 at 19:06
  • I though about mocking the manager and searched on how to inject it but I did not find anything. Could you elaborate on how I can inject the View inside my controller / function. Since the request parameter come directly from the request made to the API, I do not know how I can inject something that is not coming from the REST request. – Stéphane Pelletier-Lamothe Mar 24 '16 at 19:16
  • All I can suggest is reading the phpunit docs on mocking and perhaps reading up on defining controllers as listeners or maybe using kernel event listeners for adding the view to the request. The basic problem is that you are trying to do something quite complicated without understanding the basics. And of course a functional test will bypass much of the complexity. – Cerad Mar 24 '16 at 19:20
  • Yeah, I've been given the task to do the test for the ones who coded the function since I'm the more test enthusiast of the bunch. I need to learn symfony at the same time. Nonetheless, I thank you for your answer and your time. I will read these documents. – Stéphane Pelletier-Lamothe Mar 24 '16 at 19:30

0 Answers0