Bootstrapping Symfony2 bundle tests with Composer
September 28, 2012Marek Kalnik1 min read
If you want to create an independent Symfony2 bundle, unit test are a must. Not only are they a good practice but they also help a lot with a day-to-day developpement work. There's one problem though - how do you bootstrap them when the bundle is not in a project?
A bootstrap file is needed to create the test environement. When you are creating basic unit tests, all you really need is an autoloader that will load all your dependencies. You will need to have them defined first, but I trust that you already have a composer.json file in your project.
Actually composer can do everything you need - you can use the composer install command in the bundle itself to download all the needed components and use composer's autoload file as a bootstrap.
Here is a sample phpunit.xml.dist configuration:
<testsuites> <testsuite name="Project Test Suite"> <directory>./Tests</directory> </testsuite> </testsuites> <filter> <whitelist> <directory>./</directory> <exclude> <directory>./Resources</directory> <directory>./Tests</directory> </exclude> </whitelist> </filter>
Sometimes you may need a more refined environement setup. You can easily create a bootstrap file while still using the composer autoloader. Just change the bootstrap in PHPUnit configuration to ./Test/bootstrap.php and add all you need there.
Here is a sample Test/bootstrap.php file: