Skip to content

Commit

Permalink
WIP simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
henderjon committed Dec 12, 2014
1 parent e9208ea commit 2f33a12
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 40 deletions.
34 changes: 0 additions & 34 deletions src/Controllers/ErrorController.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Controllers/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function invoke($route){
* if not, have we set an index controller?
*/
if($this->indexController){

$route = new Router\Route($this->indexController, $route->getAction());
}else{
/**
Expand Down Expand Up @@ -116,7 +117,7 @@ function invoke($route){
$controller = $this->dispatcher->dispatch($error);
$view = call_user_func($controller, null, [404, $e]);

}catch(ActionNotFoundException $e){
}catch(Dispatcher\ActionNotFoundException $e){

$controller = $this->dispatcher->dispatch($error);
$view = call_user_func($controller, null, [404, $e]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

namespace Chevron\Kernel\Controllers;
namespace Chevron\Kernel\Dispatcher;

use Psr\Log;
use Chevron\Kernel\Dispatcher\DispatchableInterface;

abstract class BaseController implements DispatchableInterface {
abstract class AbstractDispatchableController implements DispatchableInterface {

use Log\LoggerAwareTrait;

Expand All @@ -21,7 +20,7 @@ abstract function init();
function __invoke(){
$action = $this->route->getAction();
if(method_exists($this, $action)){
return call_user_func([$this, $action]);
return call_user_func_array([$this, $action], func_get_args());
}
$this->logException(new ActionNotFoundException);
}
Expand All @@ -44,4 +43,34 @@ protected function logException(\Exception $e){
throw $e;
}

/****************************
EXAMPLE ERROR HANDLING
****************************/

// protected $response;

// function init(){
// // init something fancy
// $this->response = $this->di->get("response");
// }

/**
* handle errors
*
* 404 -- "404 means \"00PS\" in H@X0R.\n\n"
* 500 -- "OH NOES!! Something very wrong is happening.\n\n"
*/
// function __invoke($code = 404, \Exception $e = null){
// $this->setErrorHeaders(intval($code));
// if($e){ $this->logException($e); }
// return function(){ };
// }

// protected function setErrorHeaders($status){
// if($this->response InstanceOf HeadersInterface){
// $response->setContentType($this->route->getFormat());
// $response->setStatusCode($status);
// }
// }

}
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 {}
169 changes: 169 additions & 0 deletions tests/PHPUnit/Controllers/FrontControllerTest.php
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);

}

}

0 comments on commit 2f33a12

Please sign in to comment.