Skip to content

Commit

Permalink
Making subclassing of ErrorHandler much easier.
Browse files Browse the repository at this point in the history
Test cases added.
  • Loading branch information
markstory committed Oct 9, 2009
1 parent 955bd33 commit d59a2c8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cake/libs/error.php
Expand Up @@ -110,8 +110,11 @@ function __construct($method, $messages) {
}

if ($method !== 'error') {
if (Configure::read() == 0) {
$method = 'error404';
if (Configure::read('debug') == 0) {
$parentMethods = get_class_methods(get_parent_class($this));
if (in_array($method, $parentMethods)) {
$method = 'error404';
}
if (isset($code) && $code == 500) {
$method = 'error500';
}
Expand Down
57 changes: 57 additions & 0 deletions cake/tests/cases/libs/error.test.php
Expand Up @@ -207,6 +207,34 @@ class BlueberryController extends AppController {
var $components = array('Auth');
}

/**
* MyCustomErrorHandler class
*
* @package cake
* @subpackage cake.tests.cases.libs
*/
class MyCustomErrorHandler extends ErrorHandler {

/**
* custom error message type.
*
* @return void
**/
function missingWidgetThing() {
echo 'widget thing is missing';
}

/**
* stop method
*
* @access public
* @return void
*/
function _stop() {
return;
}
}

/**
* TestErrorHandler class
*
Expand Down Expand Up @@ -244,6 +272,35 @@ function skip() {
$this->skipIf(PHP_SAPI === 'cli', '%s Cannot be run from console');
}

/**
* test that methods declared in an ErrorHandler subclass are not converted
* into error404 when debug == 0
*
* @return void
**/
function testSubclassMethodsNotBeingConvertedToError() {
$back = Configure::read('debug');
Configure::write('debug', 2);
ob_start();
$ErrorHandler = new MyCustomErrorHandler('missingWidgetThing', array('message' => 'doh!'));
$result = ob_get_clean();
$this->assertEqual($result, 'widget thing is missing');

Configure::write('debug', 0);
ob_start();
$ErrorHandler = new MyCustomErrorHandler('missingWidgetThing', array('message' => 'doh!'));
$result = ob_get_clean();
$this->assertEqual($result, 'widget thing is missing', 'Method declared in subclass converted to error404. %s');

Configure::write('debug', 0);
ob_start();
$ErrorHandler = new MyCustomErrorHandler('missingController', array('message' => 'Page not found'));
$result = ob_get_clean();
$this->assertPattern('/Not Found/', $result, 'Method declared in error handler not converted to error404. %s');

Configure::write('debug', $back);
}

/**
* testError method
*
Expand Down

0 comments on commit d59a2c8

Please sign in to comment.