Testing

To build better and more reliable applications, you should test your code using
both functional and unit tests. First, read the Testing chapter on the Symfony2 documentation.
It explains everything you need to know to write tests for your Symfony2
application. However, it doesn't explain how to configure Propel, or how to use
it in your test classes.

This recipe introduces Propel in the wonderful testing world.
If you are reading it, you are probably interested in functional tests as
relying on a database means you write functional tests, not unit tests.

Symfony2 provides a
WebTestCase
which provides great features for your functional test classes. This is the
class you need when you want to do black box testing. Then again, this is
explained in the Testing chapter - Functional
tests.
Moreover, Symfony2 comes with multiple environments, like dev, prod but
also test. The Symfony2 Client, detailled in the section Working with the
Test Client
in the Symfony2 documentation, relies on this test environment.

The Test Environment

The config_test.yml file is where you have to put specific configuration for
testing purpose. For example, you can setup a new database for your tests like
yourdatabase_test:

You can also configure a SQLite connection instead of your production database
vendor (MySQL, PostgreSQL, etc.). It's a good idea to use a different
database vendor to ensure your application is not tied to a specific database,
however sometimes it's not possible.

As you may know, Propel uses smart code generation depending on the database
vendor, and so on. You need to build both SQL and PHP code before to run your
tests. It's doable by running a single command line, but it's not optimal.

The Propel WebTestCase

A good idea is to extend the WebTestCase class in your application, and to add
a few methods to run commands for you:

Basically, for each test class, it will build everthing before to execute test
methods. By using this class, you just need to run phpunit in your project to
run all tests, including functional tests that rely on a database. In other
words, it's setup your application in test environment, just like you would do
in production.

Having both WebTestCase and TestCase classes allow you to write Propel aware
tests in your application. You don't need anything else. You can read the PHPUnit
documentation for more information on
assertions.

Travis-CI

Once you have a decent test suite, you may want to use
Travis-CI. Here is a standard configuration for Symfony2
projects: