We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to integrate of PHPUnit and phalcon framework?

Such as the title.



6.4k

I struggled a bit trying to set up a simple "set uri to /, do the same as the normal application would do and check if the response equal" application, because I just set the $_SERVER['REQUEST_URI'] and not $_SERVER['REQUEST_METHOD'] - may be worth mentioning :)

Plaputta, could you post some code? I'm a bit in trouble in testing my MVC phalcon based app...

If in my action i don't return nothing leaving phalcon choosing and rendering the View, how could i test the response in PhpUnit?

I've go the idea of create a new Application in each test method and execute $application->handle("controller/action") to get the response object and make assertion on it... and it works great but it works only once... I mean that if i have multiple test method that do the same thing, i get unpredictable result as it seems that the php response object get cached for a controller. Example, if i have a controller with action1 and have 3 test method (test1 test2 and test3) that try to execute action1, test1 run correctly, but test2 has unexpected results because the response he get from action1 contains the same data returned previously to test1...

Is there any best practice on consistently unit-test Phalcon MVC apps with phpunit (without using selenium)?

Thanks

I use codeception. UnitTesting is so simple. You only must prepare autoloader for you code in bootstrap You can also use Phalcon module to codeception I personaly use acceptance test with selenium. Very easy to write from codeception.

Thanks for the advice! I already promised to give a look to codeception/selenium when i had to make acceptance tests of the frontend part of my app. Actually i'm using an MVC phalcon app to make Web API so i thought i could use simply unit-test on that Actions.

Reading your post pushed me to give a look to codeception in advance, and its documentation taken me again on the earh as also codeception says that 'functional tests' may cause a lot of trouble with service that require shared services or sessions/cookies/... etc. because all test methods are called in the same context... so there is no real enviroinment reset for each test call. The most quick solution maybe to use acceptance tests for Web API too (it's an MVC app afterall)... The other not too beautiful solution may be to check in my controllers if it is running in test mode, and if it is, get a special DI container with all services mocked.

I think i'll go with the first. Any other suggestion appreciated.