CHAPTER 18 ■ TESTING WITH PHPUNIT
With those refactorings out the way, my system is more amenable to testing. It’s no accident that my
design has improved at the same time. Now it’s to begin writing tests.
Simple Web Testing
Here’s a test case that performs a very basic test on the WOO system:
class AddVenueTest extends PHPUnit_Framework_TestCase {
function testAddVenueVanilla() {
$this->runCommand("AddVenue", array("venue_name"=>"bob") );
}
function runCommand( $command=null, array $args=null ) {
$request = \woo\base\RequestRegistry::getRequest();
if (! is_null( $args ) ) {
foreach( $args as $key=>$val ) {
$request->setProperty( $key, $val );
}
}
if (! is_null( $command ) ) {
$request->setProperty( 'cmd', $command );
}
woo\controller\Controller::run();
}
}
In fact, it does not so much test anything as prove that the system can be invoked. The real work is
done in the runCommand() method. There is nothing terribly clever here. I get a Request object from the
RequestRegistry, and I populate it with the keys and values provided in the method call. Because the
Controller will go to the same source for its Request object, I know that it will work the values I have set.
Running this test confirms that all is well. I see the output I expect. The problem is that this output is
printed by the view, and is therefore hard to test. I can fix that quite easily by buffering the output:
class AddVenueTest extends PHPUnit_Framework_TestCase {
function testAddVenueVanilla() {
$output = $this->runCommand("AddVenue", array("venue_name"=>"bob") );
self::AssertRegexp( "/added/", $output );
}
function runCommand( $command=null, array $args=null ) {
ob_start();
$request = \woo\base\RequestRegistry::getRequest();
if (! is_null( $args ) ) {
foreach( $args as $key=>$val ) {
$request->setProperty( $key, $val );
}
}
if (! is_null( $command ) ) {
$request->setProperty( 'cmd', $command );
}