A symfony tip: Unit test your Propel classes

Fabien Potencier

October 03, 2008

Unit testing Propel classes in symfony 1.0 was a bit tricky. As of symfony 1.1, it is much more easier.

To test a Propel object, you need to enable the autoloading of Propel classes, provide a valid database connection to Propel, and feed the database with some test data.

Thankfully, it is quite easy as symfony already provides everything you need:

  • To get autoloading, you need to initialize a configuration object:

    ProjectConfiguration::getApplicationConfiguration('frontend', 'test', true)

  • To get a database connection, you need to initialize the sfDatabaseManager class:

    new sfDatabaseManager($configuration);

  • To load some test data, you can use the sfPropelData class:

    $loader = new sfPropelData();

The following example shows you a very simple test file for a User Propel model:


$t = new lime_test(1, new lime_output_color());

// begin testing your model class
$user = UserPeer::retrieveByUsername('fabien');
$t->is($user->getLastName(), 'Potencier', '->retrieveByUsername() returns the User for the given username');