From d59a2c8de159b70758f191504e2bcb8472762ecc Mon Sep 17 00:00:00 2001 From: mark_story Date: Fri, 9 Oct 2009 00:13:08 -0400 Subject: [PATCH] Making subclassing of ErrorHandler much easier. Test cases added. --- cake/libs/error.php | 7 +++- cake/tests/cases/libs/error.test.php | 57 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/cake/libs/error.php b/cake/libs/error.php index ebfd86387..26ef72a52 100644 --- a/cake/libs/error.php +++ b/cake/libs/error.php @@ -110,8 +110,11 @@ class ErrorHandler extends Object { } 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'; } diff --git a/cake/tests/cases/libs/error.test.php b/cake/tests/cases/libs/error.test.php index f5b38772e..634999449 100644 --- a/cake/tests/cases/libs/error.test.php +++ b/cake/tests/cases/libs/error.test.php @@ -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 * @@ -244,6 +272,35 @@ class ErrorHandlerTest extends CakeTestCase { $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 *