With your permission I allow myself the following oratory of shell spells:
First, we need to install PHPUnit (currently for me it is a version 3.4)
pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit
(Prepend it with sudo if it makes sense for you, i.e. if you are under linux, etc.)
You should be able to run phpunit executable, if everything has gone well.
Next on our list is Zend (current 1.10.6). There is no official pear support by the project, but this should not stop us. IMHO Packaging is not just another kind of luxury. It is so essential, that a decent form of packaging should be almost everywhere welcome. Since pear is exactly such form, we will use an unofficial google-code hosted project, maintained exactly for luxury seeking appreciators.
pear channel-discover zend.googlecode.com/svn
pear install zend/zend
Now, we will assume that you are about to start your test-driven application. Let us outline the folders' structure as having at least two folders inside project's root with names src/php and test/php
Basically, we are trying to achieve a mirroring concept, that is, each class under src/php has a corresponding test scenario under test/php. It would be better to have something more concrete to play with. We will define a "Hello world" application.
src/php/MyApp/MyModule/HelloWorld.php
namespace MyApp\MyModule;
/**
* Hello world class
*
*/
class HelloWorld
{
/**
* Default message
*
* @var string
*/
const MESSAGE = 'Hello world!';
/**
* Talk method
*
* @return string message
*/
public function talk()
{
return self::MESSAGE;
}
}
To test it we extend PHPUnit_Framework_TestCase
test/php/MyApp/MyModule/testHelloWorld.php
namespace MyApp\MyModule;
/**
* 'Hello world' testcase
*/
class TestHelloWorld extends \PHPUnit_Framework_TestCase
{
/**
* Test hello world talk functionality
*
* @throws Exception
* @return void
*/
public function testTalk()
{
$helloWorld = new HelloWorld();
$observedValue = $helloWorld->talk();
$expectedValue = 'Hello world!';
self::assertEquals($expectedValue, $observedValue);
}
}
Please note that line 07 contains '\PHPUnit_Framework_TestCase' absolute namespace reference to PHPUnit_* legacy namespace. It is because class identifier follows the previously declared namespace keyword, which basically tells PHP to append each non-absolutely referred class name with itself.
So, as you have notice in this example we are mixing both legacy and new convention for PHP namespaces. This technique is quite interesting by itself, because it is not limited to this situation with PHPUnit. A perfect time to explain what this technique actually is. Once again nothing special, we just intend to use Zend loader to spare implementation time of the legacy namespaces support and to separate it from the new notation. This test.php file is placed in the root.
test.php
if (!defined('PS')) {
define('PS', PATH_SEPARATOR);
}
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('LS')) {
define('LS', PHP_EOL);
}
$extended_path = get_include_path() . PS . '.' . DS .'src/php';
$extended_path .= PS . '.' . DS .'test/php';
set_include_path($extended_path);
require_once 'PHPUnit/Framework.php';
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance()->registerNamespace('Zend_');
Zend_Loader_Autoloader::getInstance()->registerNamespace('PHPUnit_');
Zend_Loader_Autoloader::getInstance()->registerNamespace('MyApp\\');
// Run a single test by method name
//$test = new \MyApp\MyModule\TestHelloWorld('testTalk');
// Run all tests by reflection class
$test = new ReflectionClass(new MyApp\MyModule\TestHelloWorld);
PHPUnit_TextUI_TestRunner::run($test);
Lines 1-14 define constants and extend 'include_path'. Lines 15-21 are very important. They allow to specify for which namespaces Zend_Loader_Autoloader will use its internal loader (usually it is Zend_Loader). Please note that we don't want to use Zend_Loader as a general fallback loader, we want to use autoloading only for the specific list of namespace prefixes. Another thing to note here is line 20, where the namespace for our application is registered, i.e. 'MyApp'. It is very important to prepend it with '\\' since we a using non-legacy notation for our application, and it should be matched accordingly by the autoloader.
Probably the last remark here is that PHPUnit as well as Zend do not contain any namespace keyword and are functioning perfectly, keeping things the as they were.
Links for further reading
1. zend autoloader
2. psr-0-final-proposal
3. Full source code of the example
4. PHP namespaces