Unit Testing - Autoload Classes
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.
Tags: lazy, php, symfony tip, unit testing


