Setting a Symfony2 project up for testing can be tricky, however in this short guide I will show you how you are able to setup your project so you can spend more time coding and less time trying to fix issues you run into while setting up a testing environment.
One of the first steps you will need to do is setup your test
environment in
symfony2. The way we are going to setup the environment is to use Doctrine ORM
and configure it to use SQLite for the database.
Start by adding a few lines to some of the configuration files.
This tells Doctrine to use SQLite and where it can find the database file.
You shouldn’t need to add anything else to the config_test.yml
file since it
will import other configuration files which will be used to configure the rest
of doctrine. You can check your configuration by running app/console
debug:config doctrine --env=test
and making sure everything looks as expected.
At this point running phpunit -c app
will fail. There are just a few more
steps we need to complete before everything will work together.
Now we need to create a testing bootstrap file that PHPUnit will use, and then
update the phpunit.xml.dist
file.
But what if you have some fixtures you want to load? If you are using the bundle for doctrine fixtures or any other bundle to load fixtures, you just need to add some more code after you create and setup the database.
Next just edit app/phpunit.xml.dist
and modify or add the bootstrap
attribute to use test.bootstrap.php
instead of bootstrap.php.cache
and now
you can run phpunit -c app/
and have a complete database setup with fixtures
and all!