However I think I made a mistake in making the tests too specific (even with all the mocking....)
I think that I should change the three first tests to use generic handlers not named handlers. The tests should be
"First Pre Handler claims and aborts further processing"
"Later Pre-Handler claims and aborts further processing"
"No pre-handler claimed and controller invoked"
"Fall Through/Legacy handling"
instead of the explicit use of the CentralDatabaseDownHandler, InvalidCompHandler, etc. I was hoping to use these tests to drive the awareness of those two handlers -- but that should have come from the tests of the ModuleFactor that creates the handler chain, right?
I should be able to pull alot of the common code into setUp's and tearDown's I know, as well.
Here's the test code (kidna long/repetitive):
Code: Select all
<?php
require_once(COMPINABOX.'include/classes/controllers/FrontDispatcher.inc');
require_once(COMPINABOX.'include/classes/controllers/ModuleFactory.inc');
require_once(COMPINABOX.'include/classes/handlers/CentralDatabaseDownHandler.inc');
require_once(COMPINABOX.'include/classes/handlers/InvalidCompHandler.inc');
require_once(COMPINABOX.'include/classes/controllers/SlidingDoorsAdminController.inc');
require_once(COMPINABOX.'include/classes/requests/SlidingDoorsAdminRequest.inc');
require_once(COMPINABOX.'include/classes/contexts/SlidingDoorsAdminContext.inc');
Mock::generate("ModuleFactory");
Mock::generate("CentralDatabaseDownHandler");
Mock::generate("InvalidCompHandler");
Mock::generate("SlidingDoorsAdminController");
Mock::generate("SlidingDoorsAdminRequest");
Mock::generate("SlidingDoorsAdminContext");
Mock::generatePartial("FrontDispatcher","FDTestVersion",
array("_findModule"));
class TestFrontDispatcher extends UnitTestCase {
var $savedState;
function TestFrontDispatcher() {
$this->UnitTestCase('TestFrontDispatcher');
}
function setUp() {
$this->savedState=array("Get"=>$_GET,
"Post"=>$_POST,
"Cookie"=>$_COOKIE,
"Server"=>$_SERVER);
$_GET=array();
$_POST=array();
$_SERVER=array();
$_COOKIE=array();
}
function tearDown() {
$_GET=$this->savedState["Get"];
$_POST=$this->savedState["Post"];
$_COOKIE=$this->savedState["Cookie"];
$_SERVER=$this->savedState["Server"];
}
function testCentralDBDown() {
$_SERVER["REQUEST_URI"]="/register/foo/Admin/bar";
$viewName="CentralDatabaseDown";
$sdaRequest =& new MockSlidingDoorsAdminRequest($this);
$sdaContext =& new MockSlidingDoorsAdminContext($this);
$dbHandler =& new MockCentralDatabaseDownHandler($this);
$dbHandler->expectOnce("canHandle",array($sdaContext,$sdaRequest));
$dbHandler->setReturnValue("canHandle",TRUE);
$dbHandler->expectOnce("execute",array($sdaContext,$sdaRequest));
$dbHandler->setReturnValue("execute",$viewName);
$compHandler =& new MockInvalidCompHandler($this);
$compHandler->expectNever("canHandle");
$compHandler->expectNever("execute");
$mc =& new MockSlidingDoorsAdminController($this);
$mc->expectNever("invoke");
$sdaFactory =& new MockModuleFactory($this);
$sdaFactory->setReturnReference("createRequest",$sdaRequest);
$sdaFactory->setReturnReference("createContext",$sdaContext);
$sdaFactory->setReturnReference("createController",$mc);
$handlers = array(); $handlers[0]=&$dbHandler; $handlers[1]=&$compHandler;
$sdaFactory->setReturnReference("createPreHandlers",$handlers);
$dispatcher =& new FDTestVersion($this);
$dispatcher->setReturnReference("_findModule",$sdaFactory);
$dispatcher->FrontDispatcher();
$this->assertEqual($viewName, $dispatcher->dispatch());
$mc->tally();
$dbHandler->tally();
$compHandler->tally();
}
function testInvalidCompName() {
$_SERVER["REQUEST_URI"]="/register/foo/Admin/bar";
$viewName="InvalidCompName";
$sdaRequest =& new MockSlidingDoorsAdminRequest($this);
$sdaContext =& new MockSlidingDoorsAdminContext($this);
$dbHandler =& new MockCentralDatabaseDownHandler($this);
$dbHandler->expectOnce("canHandle",array($sdaContext,$sdaRequest));
$dbHandler->setReturnValue("canHandle",FALSE);
$dbHandler->expectNever("execute");
$compHandler =& new MockInvalidCompHandler($this);
$compHandler->expectOnce("canHandle",array($sdaContext,$sdaRequest));
$compHandler->setReturnValue("canHandle",TRUE);
$compHandler->expectOnce("execute",array($sdaContext,$sdaRequest));
$compHandler->setReturnValue("execute",$viewName);
$mc =& new MockSlidingDoorsAdminController($this);
$mc->expectNever("invoke");
$sdaFactory =& new MockModuleFactory($this);
$sdaFactory->setReturnReference("createRequest",$sdaRequest);
$sdaFactory->setReturnReference("createContext",$sdaContext);
$sdaFactory->setReturnReference("createController",$mc);
$handlers = array(); $handlers[0]=&$dbHandler; $handlers[1]=&$compHandler;
$sdaFactory->setReturnReference("createPreHandlers",$handlers);
$dispatcher =& new FDTestVersion($this);
$dispatcher->setReturnReference("_findModule",$sdaFactory);
$dispatcher->FrontDispatcher();
$this->assertEqual($viewName, $dispatcher->dispatch());
$mc->tally();
$dbHandler->tally();
$compHandler->tally();
}
function testInvokeModuleController() {
$_SERVER["REQUEST_URI"]="/register/foo/Admin/bar";
$viewName="SomeView";
$sdaRequest =& new MockSlidingDoorsAdminRequest($this);
$sdaContext =& new MockSlidingDoorsAdminContext($this);
$dbHandler =& new MockCentralDatabaseDownHandler($this);
$dbHandler->expectOnce("canHandle",array($sdaContext,$sdaRequest));
$dbHandler->setReturnValue("canHandle",FALSE);
$dbHandler->expectNever("execute");
$compHandler =& new MockInvalidCompHandler($this);
$compHandler->expectOnce("canHandle",array($sdaContext,$sdaRequest));
$compHandler->setReturnValue("canHandle",FALSE);
$compHandler->expectNever("execute");
$mc =& new MockSlidingDoorsAdminController($this);
$mc->expectOnce("invoke",array($sdaRequest,$sdaContext));
$mc->setReturnValue("invoke",$viewName);
$sdaFactory =& new MockModuleFactory($this);
$sdaFactory->setReturnReference("createRequest",$sdaRequest);
$sdaFactory->setReturnReference("createContext",$sdaContext);
$sdaFactory->setReturnReference("createController",$mc);
$handlers = array(); $handlers[0]=&$dbHandler; $handlers[1]=&$compHandler;
$sdaFactory->setReturnReference("createPreHandlers",$handlers);
$dispatcher =& new FDTestVersion($this);
$dispatcher->setReturnReference("_findModule",$sdaFactory);
$dispatcher->FrontDispatcher();
$this->assertEqual($viewName, $dispatcher->dispatch());
$mc->tally();
$dbHandler->tally();
$compHandler->tally();
}
function testFallThrough() {
$_SERVER["REQUEST_URI"]="/bar";
$dispatcher =& new FrontDispatcher();
$this->assertEqual(0, $dispatcher->dispatch());
}
}
?>