Zend, Doctrine and PHPUnit a great threesome

I’m a sucker for executing PHPUnit tests using my konsole.

Using rake I have set up a build script that executes my Doctrine migrations and runs my PHPUnit test suite.Here is my rake file:

namespace(:phpunit) do
 task :run do
 # system "cd application/models/ && rm *.php"
 # system "cd application/models/generated/ && rm *.php"
 system "php ./db_build.php"
 system "phpunit --colors --verbose tests/TestSuite"
 end
end

I have commented the clean up tasks out since I’m using fat models and slim controllers aproach – I have business logic stuffed into my models.

This rake task executes the following steps each time it’s run:

  1. execute “php ./db_build.php” which is my Doctrine migration script
  2. execute “phpunit tests/TestSuite” which is my test suite

The result is great and gives me a lot of flexibility. The db_build.php file dumps my tables and rebuilds them:

require_once('application/library/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));
$manager = Doctrine_Manager::getInstance();
$manager->openConnection('mysql://user:pass@localhost/database');

// Doctrine::generateModelsFromDb('application/models', array('doctrine'), array('generateTableClasses' => true));
Doctrine::dropDatabases();
Doctrine::createDatabases();
Doctrine::generateModelsFromYaml("./schema.yml", "application/models");
Doctrine::createTablesFromModels('application/models');
// Manager
$manager = new Manager();
$manager->username = 'admin';
$manager->password = 'secret';
$manager->acl = 'Manager';
$manager->save();

// Default photographer
$photographer = new Photographer;
$photographer->email = 'email@example.com';
$photographer->password = '12345';
$photographer->save();

Using Doctrine::generateModelsFromYaml I can edit my database schema file “on the fly” adding and removing fields and relations – removing classes doesn’t run yet. Once the entire database has been dropped and rebuild, I setup some default test data just to save me some time, this could also be done in a unit test. But it doesn’t get more agile that this! I can alter my schema, drop and rebuild the database each time I run a unit test, and I will get any error prompted rigth away if I have changed something vital that one of my classes is relying on.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: