We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Unit test controllers

How do you unit test controllers in Phalcon?

I have some trouble adding headers to a request, is it only possible to add these through the super global or is there some other way ?



300
Accepted
answer

i think you can do it through superglobal or through DI, something like:

// test case
$response = new \Phalcon\Http\Response();
$response->setHeader('Content-Type', 'application/json');
$this->getDI()->set('response', $response);

// dispatch
$this->dispatch('index/index');

Yes that is possible in the response but not in the request :(

in that case you could create a mock object of request object and assign it to DI with key 'request' ?

Yes offcourse :D