Linux, WebDev and other…

Phalcon, PHPUnit and database

I am currently writing a REST API in Phalcon PHP framework. Database is mostly handled by ORM, but I also created few repositories for complex queries in raw SQL. Mocking all of this is very problematic so I decided to run tests on additional, test-only database. This database is populated by initial test data, which is used in tests. Each test must be independent from others, so database state should be same for all tests. Thankfully this is easy to accomplish, because Phalcon supports nested transcations. I am calling rollback after each test and everything works as expected and gives fast execution times.

My PhpUnit configuration is based on unit testing section in Phalcon documentation. Database with nested transactions is initialized in DI in the PhpUnit helper file:

And this is my base test class. setUp is called before each test and tearDown is called after each test:

Notice, that I am not calling direct parent on tearDown method, because I don’t need to reset DI every each test. You can also call direct parent, but then you must initialize DI in setUp.

, , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *