PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 18 ■ TESTING WITH PHPUNIT

list( $key, $val )=explode( "=", $arg );
$this->setProperty( $key, $val );
}
}
}


The init() method detects whether it is running in a server context, and populates the $properties
array accordingly (either directly or via setProperty()). This works fine for command line invocation. It
means I can run something like:


$ php runner.php cmd=AddVenue venue_name=bob


and get this response:




Add a Space for venue bob


Add a Space for Venue 'bob'







'bob' added (5)
please add name for the space

[add space]








Although this works for the command line, it remains a little tricky to pass in arguments via a
method call. One inelegant solution would be to manually set the $argv array before calling the
controller’s run() method. I don’t much like this, though. Playing directly with magic arrays feels plain
wrong, and the string manipulation involved at each end would compound the sin. Looking at the
controller class more closely, I see an opportunity to improve both design and testability. Here’s an
extract from the handleRequest() method:


// \woo\controller\Controller
function handleRequest() {


$request = new Request();


$app_c = \woo\base\ApplicationRegistry::appController();
while( $cmd = $app_c->getCommand( $request ) ) {
$cmd->execute( $request );
}

Free download pdf