-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
205 additions
and
40 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Controllers/ActionNotFoundException.php → src/Dispatcher/ActionNotFoundException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?php | ||
|
||
namespace Chevron\Kernel\Controllers; | ||
namespace Chevron\Kernel\Dispatcher; | ||
|
||
class ActionNotFoundException extends \Exception {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
|
||
class MockDi { | ||
function __call($name, $args){ | ||
return null; | ||
} | ||
} | ||
|
||
class MockBaseController extends \Chevron\Kernel\Dispatcher\AbstractDispatchableController { | ||
function init(){} /* noop */ | ||
function index(){ | ||
return "base"; | ||
} | ||
} | ||
|
||
class MockIndexController extends \Chevron\Kernel\Dispatcher\AbstractDispatchableController { | ||
function init(){} /* noop */ | ||
function index(){ | ||
return "index"; | ||
} | ||
} | ||
|
||
class MockErrorController extends \Chevron\Kernel\Dispatcher\AbstractDispatchableController { | ||
function init(){} /* noop */ | ||
function index($method, $args = []){ | ||
list($code, $e) = $args; | ||
return $code; | ||
} | ||
} | ||
|
||
class FrontControllerTest extends PHPUnit_Framework_TestCase { | ||
|
||
function getDiMock(){ | ||
return new MockDi; | ||
} | ||
|
||
function getRouteMock(){ | ||
return new \Chevron\Kernel\Router\Route("MockBaseController", "index"); | ||
} | ||
|
||
function getDispatcherMock(){ | ||
|
||
$di = $this->getDiMock(); | ||
$route = $this->getRouteMock(); | ||
|
||
$mock = $this->getMock("Chevron\\Kernel\\Dispatcher\\DispatcherInterface"); | ||
return $mock; | ||
} | ||
|
||
function getRouterMock(){ | ||
|
||
$route = $this->getRouteMock(); | ||
|
||
$mock = $this->getMock("Chevron\\Kernel\\Router\\RouterInterface"); | ||
$mock->method('match') | ||
->willReturn($route); | ||
return $mock; | ||
} | ||
|
||
function test_normal(){ | ||
|
||
$di = $this->getDiMock(); | ||
$dispatcher = $this->getDispatcherMock(); | ||
$router = $this->getRouterMock(); | ||
$route = $this->getRouteMock(); | ||
|
||
$dispatcher->method('dispatch') | ||
->willReturn(new MockBaseController($di, $route)); | ||
|
||
$app = new Chevron\Kernel\Controllers\FrontController($di, $dispatcher, $router); | ||
$app->setIndexController("MockIndexController"); | ||
$app->setErrorController("MockErrorController"); | ||
|
||
$result = $app("/index.html"); | ||
|
||
$this->assertEquals("base", $result); | ||
|
||
} | ||
|
||
function test_indexController(){ | ||
|
||
$di = $this->getDiMock(); | ||
$dispatcher = $this->getDispatcherMock(); | ||
$router = $this->getRouterMock(); | ||
$route = $this->getRouteMock(); | ||
|
||
$dispatcher->method('dispatch') | ||
->willReturn(new MockIndexController($di, $route)); | ||
|
||
$app = new Chevron\Kernel\Controllers\FrontController($di, $dispatcher, $router); | ||
$app->setIndexController("MockIndexController"); | ||
$app->setErrorController("MockErrorController"); | ||
|
||
$result = $app("/index.html"); | ||
|
||
$this->assertEquals("index", $result); | ||
|
||
} | ||
|
||
function test_ControllerNotFoundException(){ | ||
|
||
$di = $this->getDiMock(); | ||
$dispatcher = $this->getDispatcherMock(); | ||
$router = $this->getRouterMock(); | ||
$route = $this->getRouteMock(); | ||
|
||
$dispatcher->method('dispatch') | ||
->will($this->onConsecutiveCalls( | ||
$this->throwException(new Chevron\Kernel\Dispatcher\ControllerNotFoundException), | ||
new MockErrorController($di, $route) | ||
)); | ||
|
||
$app = new Chevron\Kernel\Controllers\FrontController($di, $dispatcher, $router); | ||
$app->setIndexController("MockIndexController"); | ||
$app->setErrorController("MockErrorController"); | ||
|
||
$result = $app("/foo.html"); | ||
|
||
$this->assertEquals(404, $result); | ||
|
||
} | ||
|
||
function test_ActionNotFoundException(){ | ||
|
||
$di = $this->getDiMock(); | ||
$dispatcher = $this->getDispatcherMock(); | ||
$router = $this->getRouterMock(); | ||
$route = $this->getRouteMock(); | ||
|
||
$dispatcher->method('dispatch') | ||
->will($this->onConsecutiveCalls( | ||
$this->throwException(new Chevron\Kernel\Dispatcher\ActionNotFoundException), | ||
new MockErrorController($di, $route) | ||
)); | ||
|
||
$app = new Chevron\Kernel\Controllers\FrontController($di, $dispatcher, $router); | ||
$app->setIndexController("MockIndexController"); | ||
$app->setErrorController("MockErrorController"); | ||
|
||
$result = $app("/bar.html"); | ||
|
||
$this->assertEquals(404, $result); | ||
|
||
} | ||
|
||
function test_Exception(){ | ||
|
||
$di = $this->getDiMock(); | ||
$dispatcher = $this->getDispatcherMock(); | ||
$router = $this->getRouterMock(); | ||
$route = $this->getRouteMock(); | ||
|
||
$dispatcher->method('dispatch') | ||
->will($this->onConsecutiveCalls( | ||
$this->throwException(new Exception), | ||
new MockErrorController($di, $route) | ||
)); | ||
|
||
$app = new Chevron\Kernel\Controllers\FrontController($di, $dispatcher, $router); | ||
$app->setIndexController("MockIndexController"); | ||
$app->setErrorController("MockErrorController"); | ||
|
||
$result = $app("/bar.html"); | ||
|
||
$this->assertEquals(500, $result); | ||
|
||
} | ||
|
||
} |