updating ErrorHandler with test, fixes #4102

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6462 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-02-15 16:49:17 +00:00
parent 0073683a54
commit e9c15fe33e
2 changed files with 59 additions and 49 deletions

View file

@ -50,33 +50,13 @@ class ErrorHandler extends Object{
* @param array $messages Error messages * @param array $messages Error messages
*/ */
function __construct($method, $messages) { function __construct($method, $messages) {
parent::__construct();
static $__previousError = null;
if ($__previousError != array($method, $messages)) {
$__previousError = array($method, $messages);
if (!class_exists('dispatcher')) {
require CAKE . 'dispatcher.php';
}
$this->__dispatch =& new Dispatcher();
$this->__dispatch->base = $this->__dispatch->baseUrl();
if (!class_exists('appcontroller')) {
App::import('Controller', 'App'); App::import('Controller', 'App');
}
$this->controller =& new AppController(); $this->controller =& new AppController();
$this->controller->base = $this->__dispatch->base; $this->controller->_set(Router::getPaths());
$this->controller->webroot = $this->__dispatch->webroot;
$this->controller->params = Router::getParams(); $this->controller->params = Router::getParams();
$this->controller->here = $this->controller->params['url']['url'];
$this->controller->viewPath = 'errors';
$this->controller->cacheAction = false;
$this->controller->constructClasses(); $this->controller->constructClasses();
$this->__dispatch->start($this->controller); $this->controller->cacheAction = false;
} $this->controller->viewPath = 'errors';
$allow = array('.', '/', '_', ' ', '-', '~'); $allow = array('.', '/', '_', ' ', '-', '~');
if (substr(PHP_OS,0,3) == "WIN") { if (substr(PHP_OS,0,3) == "WIN") {
@ -98,10 +78,17 @@ class ErrorHandler extends Object{
$method = 'error'; $method = 'error';
} }
if (Configure::read() > 0 || $method == 'error') { if ($method == 'error') {
call_user_func_array(array(&$this, $method), $messages); $this->dispatchMethod($method, $messages);
exit();
} elseif (Configure::read() == 0) {
$this->dispatchMethod('error404', $messages);
exit();
} else { } else {
call_user_func_array(array(&$this, 'error404'), $messages); $this->dispatchMethod($method, $messages);
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
exit();
}
} }
} }
/** /**
@ -117,7 +104,6 @@ class ErrorHandler extends Object{
'message' => $message, 'message' => $message,
'title' => $code . ' ' . $name)); 'title' => $code . ' ' . $name));
$this->controller->render('error404'); $this->controller->render('error404');
exit();
} }
/** /**
* Convenience method to display a 404 page. * Convenience method to display a 404 page.
@ -133,11 +119,11 @@ class ErrorHandler extends Object{
} }
$url = Router::normalize($url); $url = Router::normalize($url);
header("HTTP/1.0 404 Not Found"); header("HTTP/1.0 404 Not Found");
$this->error(array('code' => '404', $this->controller->set(array('code' => '404',
'name' => __('Not Found', true), 'name' => __('Not Found', true),
'message' => sprintf(__("The requested address %s was not found on this server.", true), "<strong>'{$url}'</strong>"), 'message' => sprintf(__("The requested address %s was not found on this server.", true), "<strong>'{$url}'</strong>"),
'base' => $this->controller->base)); 'base' => $this->controller->base));
exit(); $this->controller->render('error404');
} }
/** /**
* Renders the Missing Controller web page. * Renders the Missing Controller web page.
@ -153,7 +139,6 @@ class ErrorHandler extends Object{
'controllerName' => $controllerName, 'controllerName' => $controllerName,
'title' => __('Missing Controller', true))); 'title' => __('Missing Controller', true)));
$this->controller->render('missingController'); $this->controller->render('missingController');
exit();
} }
/** /**
* Renders the Missing Action web page. * Renders the Missing Action web page.
@ -170,7 +155,6 @@ class ErrorHandler extends Object{
'action' => $action, 'action' => $action,
'title' => __('Missing Method in Controller', true))); 'title' => __('Missing Method in Controller', true)));
$this->controller->render('missingAction'); $this->controller->render('missingAction');
exit();
} }
/** /**
* Renders the Private Action web page. * Renders the Private Action web page.
@ -185,7 +169,6 @@ class ErrorHandler extends Object{
'action' => $action, 'action' => $action,
'title' => __('Trying to access private method in class', true))); 'title' => __('Trying to access private method in class', true)));
$this->controller->render('privateAction'); $this->controller->render('privateAction');
exit();
} }
/** /**
* Renders the Missing Table web page. * Renders the Missing Table web page.
@ -200,7 +183,6 @@ class ErrorHandler extends Object{
'table' => $table, 'table' => $table,
'title' => __('Missing Database Table', true))); 'title' => __('Missing Database Table', true)));
$this->controller->render('missingTable'); $this->controller->render('missingTable');
exit();
} }
/** /**
* Renders the Missing Database web page. * Renders the Missing Database web page.
@ -213,7 +195,6 @@ class ErrorHandler extends Object{
$this->controller->set(array('title' => __('Scaffold Missing Database Connection', true))); $this->controller->set(array('title' => __('Scaffold Missing Database Connection', true)));
$this->controller->render('missingScaffolddb'); $this->controller->render('missingScaffolddb');
exit();
} }
/** /**
* Renders the Missing View web page. * Renders the Missing View web page.
@ -229,7 +210,6 @@ class ErrorHandler extends Object{
'file' => $file, 'file' => $file,
'title' => __('Missing View', true))); 'title' => __('Missing View', true)));
$this->controller->render('missingView'); $this->controller->render('missingView');
exit();
} }
/** /**
* Renders the Missing Layout web page. * Renders the Missing Layout web page.
@ -244,7 +224,6 @@ class ErrorHandler extends Object{
$this->controller->set(array('file' => $file, $this->controller->set(array('file' => $file,
'title' => __('Missing Layout', true))); 'title' => __('Missing Layout', true)));
$this->controller->render('missingLayout'); $this->controller->render('missingLayout');
exit();
} }
/** /**
* Renders the Database Connection web page. * Renders the Database Connection web page.
@ -258,7 +237,6 @@ class ErrorHandler extends Object{
$this->controller->set(array('model' => $className, $this->controller->set(array('model' => $className,
'title' => __('Missing Database Connection', true))); 'title' => __('Missing Database Connection', true)));
$this->controller->render('missingConnection'); $this->controller->render('missingConnection');
exit();
} }
/** /**
* Renders the Missing Helper file web page. * Renders the Missing Helper file web page.
@ -273,7 +251,6 @@ class ErrorHandler extends Object{
'file' => $file, 'file' => $file,
'title' => __('Missing Helper File', true))); 'title' => __('Missing Helper File', true)));
$this->controller->render('missingHelperFile'); $this->controller->render('missingHelperFile');
exit();
} }
/** /**
* Renders the Missing Helper class web page. * Renders the Missing Helper class web page.
@ -288,7 +265,6 @@ class ErrorHandler extends Object{
'file' => $file, 'file' => $file,
'title' => __('Missing Helper Class', true))); 'title' => __('Missing Helper Class', true)));
$this->controller->render('missingHelperClass'); $this->controller->render('missingHelperClass');
exit();
} }
/** /**
* Renders the Missing Component file web page. * Renders the Missing Component file web page.
@ -304,7 +280,6 @@ class ErrorHandler extends Object{
'file' => $file, 'file' => $file,
'title' => __('Missing Component File', true))); 'title' => __('Missing Component File', true)));
$this->controller->render('missingComponentFile'); $this->controller->render('missingComponentFile');
exit();
} }
/** /**
* Renders the Missing Component class web page. * Renders the Missing Component class web page.
@ -320,7 +295,6 @@ class ErrorHandler extends Object{
'file' => $file, 'file' => $file,
'title' => __('Missing Component Class', true))); 'title' => __('Missing Component Class', true)));
$this->controller->render('missingComponentClass'); $this->controller->render('missingComponentClass');
exit();
} }
/** /**
* Renders the Missing Model class web page. * Renders the Missing Model class web page.
@ -334,7 +308,6 @@ class ErrorHandler extends Object{
$this->controller->set(array('model' => $className, $this->controller->set(array('model' => $className,
'title' => __('Missing Model', true))); 'title' => __('Missing Model', true)));
$this->controller->render('missingModel'); $this->controller->render('missingModel');
exit();
} }
} }
?> ?>

View file

@ -26,7 +26,27 @@
* @lastmodified $Date$ * @lastmodified $Date$
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/ */
uses('error'); if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
}
uses('error', 'controller' . DS .'controller');
if (!class_exists('TestAppController')) {
class TestAppController extends Controller {
function beforeFilter() {
$this->cakeError('error404', array('oops' => 'Nothing to see here'));
}
}
}
class TestErrorController extends TestAppController {
var $uses = array();
function index() {
$this->autoRender = false;
return 'what up';
}
}
/** /**
* Short description for class. * Short description for class.
* *
@ -36,7 +56,24 @@ uses('error');
class ErrorHandlerTest extends UnitTestCase { class ErrorHandlerTest extends UnitTestCase {
function skip() { function skip() {
$this->skipif (true, 'ErrorHandlerTest not implemented'); $this->skipif (false, 'ErrorHandlerTest not implemented');
}
function testFromBeforeFilter() {
$Test = new TestErrorController();
if (!class_exists('dispatcher')) {
require CAKE . 'dispatcher.php';
}
$Dispatcher =& new Dispatcher();
restore_error_handler();
ob_start();
$controller = $Dispatcher->dispatch('/test_error', array('return'=> 1));
$expected = ob_get_clean();
set_error_handler('simpleTestErrorHandler');
$this->assertPattern("/<h2>Not Found<\/h2>/", $expected);
$this->assertPattern("/<strong>'\/test_error'<\/strong>/", $expected);
} }
function testError() { function testError() {