The most important thing you should provide with your re-usable bundles is unit tests set. Lately I solved two major cases which Symfony2 hasn’t got out of the box: testing services, defined in Dependency Injection Container and running model tests with fixtures in fully isolated environment.

Testing services defined in DIC

Since DIC in Symfony2 is one of the most awesome solutions, you probably (so do I) want to reflect creating services in tests in the same manner as in tested code. So you probably wondering how to use container in the tests. For this purpose I created BaseTestCase, which all of my tests cases extends.

Now you can simply retrieve services as $this->get(“”) and invoke them (for testing purposes) in the same way, as you do it in production code (for example as in controlers)

Fully isolated model tests

Often we deal with testing code which works with database. For this purpose we should use Doctrine Fixtures Bundle and create some fixture classes. But we need to load those fixtures before every test, to make sure, that our tests are running in isolated environement. We can use ModelTestCase, like in the listing below.

Last thing we need to do, is to setup sqlite in memory database in doctrine config (app/config/config_tes.yml):

Now before every single test, in test case which extends ModelTestCase, whole database structure will be created into the sqlite memory databse, and all fixtures will be loaded.
This approach allows you to create test suites which can be run not only locally, but also in Continous Integration servers, like Bamboo or Hudson.

Some of concepts used in this post were borrowed from this blog post:

If you are using PHP 5.4 you will be probably interested in similar idea in Testing in isolation with Symfony2 and WebTestCase