3 Feb 2012

Reset database before phpunit tests in Symfony 2

Here's a way you can reset the database before running your phpunit tests.

In app/phpunit.xml change the bootstrap file we're going to use:

   bootstrap                   = "my_phpunit_bootstrap.php" >   

In my_phpunit_bootstrap.php we import the old bootstrap and add our code to recreate the database and load the fixtures (you could also do this inside your tests):

I also set the test database as pdo_sqlite for faster tests.

Here are some other links I found useful before settling into this solution:

Fully isolated tests in symfony 2

What is the best way to create relationships with functional tests

Using doctrine entity manager in symfony2 unit tests