Archive for May, 2008

Unit Testing - Autoload Classes

By Stanley | Monday, May 12th, 2008

I like test driven development. I don’t like adding required files to test script manually because it’s tedious and I am lazy. I really like Symfony’s autoloading feature. Unfortunately, this feature is not available for running unit tests. Running symfony test-unit without including any of the required files, tests will not run.

Fortunately, there is an elegant solution. The idea is based on the Symfony CLI environment. At the top of the test script, add the following lines (or put them in a file and include that file):

<?php
require_once ‘PHPUnit/Framework.php’;

define(’SF_ROOT_DIR’,      realpath(dirname(__file__).’/../..’));
define(’SF_APP’,           ‘frontend’);
define(’SF_ENVIRONMENT’,   ‘cli’);
define(’SF_DEBUG’,          false);

require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.’apps’.
DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.’config’.
DIRECTORY_SEPARATOR.’config.php’);
?>

These lines should be familiar if you have worked with Symfony CLI. Basically, they are boilerplate code for any Symfony CLI app. With any luck, Symfony should load classes for you automagically.

My Laziness and sfPropelMigrationsLightPlugin

By Stanley | Friday, May 2nd, 2008

I have a confession to make: I am lazy. My colleagues know this. One of the things I hate doing when starting on an existing project is setting up the database. Sure, database schema and update scripts should be somewhere in the repository. Chances are good that they are not kept up-to-date with all database changes. Even if they are, I just can’t apply all the update scripts at once because of dependencies or changes have already been applied. 99.9% of the time, I ended up dumping the staging or development database and importing it into mine.

I was going to write a tool that will solve this problem, but my laziness would have none of it. Instead, I searched the Internet and found this cool little tool: sfPropelMigrationsLightPlugin, a Symfony plug-in. It works like this. Whenever there is a database change, you would initialize a migration and the plug-in creates a PHP file for you to add the database changes. To update the database, just run “symfony migrate <app name>” and the tool will automagically apply all missing updates to the database. This feature is particularly useful if there is more than one person working on the project at the same time. Team members just run “symfony migrate <app name>” and viola, their databases are synchronized with yours. No fuss. It couldn’t have been more simple.

Checkout the plugin at http://trac.symfony-project.com/wiki/sfPropelMigrationsLightPlugin.