How to set up Doctrine2 fixtures when testing with PHPUnit? How to set up Doctrine2 fixtures when testing with PHPUnit? symfony symfony

How to set up Doctrine2 fixtures when testing with PHPUnit?


Yes DoctrineFixtures are a good choice.

To test model: you don't really need to load fixtures in the database, you should create objects with the data you want (by injecting it with setters).

To test controller: load doctrine fixtures and use doctrine transactions so the state of your database is the same before each testcase, begin transaction in setUp() and rollback in tearDow(). (If your controller use transactions too i haven't found a good solution yet).

For fixtures error, if you don't have any error and your fixtures aren't loaded maybe you have missed a naming convention. Can you show us some code ?


Have a look at this solution. I don't think using transactions is the best idea, since chances are that you'll use transactions in your code. This solution suggests to load fixtures manually in each of your test.


There is a very handy LiipFunctionalTestBundle that simplifies working with a fixtures in test. The basic idea is to create a database each time you run the tests and then load fixtures. Now you can save the models, delete, every test they will be the same.