2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* ErrorHandlerTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Error
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5432
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-09 05:55:24 +00:00
|
|
|
App::uses('ErrorHandler', 'Error');
|
|
|
|
App::uses('Controller', 'Controller');
|
|
|
|
App::uses('Router', 'Routing');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* ErrorHandlerTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Error
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-03-18 17:55:58 +00:00
|
|
|
class ErrorHandlerTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-10-10 21:18:48 +00:00
|
|
|
public $_restoreError = false;
|
2010-05-29 04:21:02 +00:00
|
|
|
/**
|
|
|
|
* setup create a request object to get out of router later.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2010-09-25 14:41:08 +00:00
|
|
|
App::build(array(
|
2011-04-22 13:17:28 +00:00
|
|
|
'View' => array(
|
2011-04-17 10:35:21 +00:00
|
|
|
CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS
|
2010-09-25 14:41:08 +00:00
|
|
|
)
|
|
|
|
), true);
|
|
|
|
Router::reload();
|
|
|
|
|
2010-05-29 04:21:02 +00:00
|
|
|
$request = new CakeRequest(null, false);
|
|
|
|
$request->base = '';
|
|
|
|
Router::setRequestInfo($request);
|
2010-08-29 02:50:29 +00:00
|
|
|
$this->_debug = Configure::read('debug');
|
2010-11-15 03:06:18 +00:00
|
|
|
$this->_error = Configure::read('Error');
|
|
|
|
Configure::write('debug', 2);
|
2010-05-29 04:21:02 +00:00
|
|
|
}
|
|
|
|
|
2010-09-25 14:41:08 +00:00
|
|
|
/**
|
|
|
|
* teardown
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function teardown() {
|
2010-08-29 02:50:29 +00:00
|
|
|
Configure::write('debug', $this->_debug);
|
2010-11-15 03:06:18 +00:00
|
|
|
Configure::write('Error', $this->_error);
|
2010-09-25 14:41:08 +00:00
|
|
|
App::build();
|
2010-11-15 01:00:27 +00:00
|
|
|
if ($this->_restoreError) {
|
|
|
|
restore_error_handler();
|
|
|
|
}
|
2010-09-19 05:43:00 +00:00
|
|
|
}
|
|
|
|
|
2010-11-15 01:00:27 +00:00
|
|
|
/**
|
|
|
|
* test error handling when debug is on, an error should be printed from Debugger.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHandleErrorDebugOn() {
|
2010-11-15 01:00:27 +00:00
|
|
|
set_error_handler('ErrorHandler::handleError');
|
|
|
|
$this->_restoreError = true;
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$wrong .= '';
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/<pre class="cake-error">/', $result);
|
|
|
|
$this->assertRegExp('/<b>Notice<\/b>/', $result);
|
|
|
|
$this->assertRegExp('/variable:\s+wrong/', $result);
|
2010-11-15 01:00:27 +00:00
|
|
|
}
|
|
|
|
|
2010-11-22 04:02:02 +00:00
|
|
|
/**
|
|
|
|
* provides errors for mapping tests.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function errorProvider() {
|
|
|
|
return array(
|
|
|
|
array(E_USER_NOTICE, 'Notice'),
|
|
|
|
array(E_USER_WARNING, 'Warning'),
|
|
|
|
array(E_USER_ERROR, 'Fatal Error'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test error mappings
|
|
|
|
*
|
|
|
|
* @dataProvider errorProvider
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testErrorMapping($error, $expected) {
|
2010-11-22 04:02:02 +00:00
|
|
|
set_error_handler('ErrorHandler::handleError');
|
|
|
|
$this->_restoreError = true;
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
trigger_error('Test error', $error);
|
|
|
|
|
|
|
|
$result = ob_get_clean();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/<b>' . $expected . '<\/b>/', $result);
|
2010-11-22 04:02:02 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 03:44:31 +00:00
|
|
|
/**
|
|
|
|
* test error prepended by @
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testErrorSuppressed() {
|
2010-12-02 03:44:31 +00:00
|
|
|
set_error_handler('ErrorHandler::handleError');
|
|
|
|
$this->_restoreError = true;
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
@include 'invalid.file';
|
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertTrue(empty($result));
|
|
|
|
}
|
|
|
|
|
2010-11-15 01:00:27 +00:00
|
|
|
/**
|
|
|
|
* Test that errors go into CakeLog when debug = 0.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHandleErrorDebugOff() {
|
2010-11-15 01:00:27 +00:00
|
|
|
Configure::write('debug', 0);
|
2010-11-15 03:06:18 +00:00
|
|
|
Configure::write('Error.trace', false);
|
|
|
|
if (file_exists(LOGS . 'debug.log')) {
|
|
|
|
@unlink(LOGS . 'debug.log');
|
|
|
|
}
|
2010-11-15 01:00:27 +00:00
|
|
|
|
|
|
|
set_error_handler('ErrorHandler::handleError');
|
|
|
|
$this->_restoreError = true;
|
|
|
|
|
|
|
|
$out .= '';
|
|
|
|
|
|
|
|
$result = file(LOGS . 'debug.log');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals(count($result), 1);
|
|
|
|
$this->assertRegExp(
|
2011-04-25 03:31:44 +00:00
|
|
|
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
|
2010-11-15 01:00:27 +00:00
|
|
|
$result[0]
|
|
|
|
);
|
|
|
|
@unlink(LOGS . 'debug.log');
|
|
|
|
}
|
|
|
|
|
2010-11-15 03:06:18 +00:00
|
|
|
/**
|
|
|
|
* Test that errors going into CakeLog include traces.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHandleErrorLoggingTrace() {
|
2010-11-15 03:06:18 +00:00
|
|
|
Configure::write('debug', 0);
|
|
|
|
Configure::write('Error.trace', true);
|
|
|
|
if (file_exists(LOGS . 'debug.log')) {
|
|
|
|
@unlink(LOGS . 'debug.log');
|
|
|
|
}
|
|
|
|
|
|
|
|
set_error_handler('ErrorHandler::handleError');
|
|
|
|
$this->_restoreError = true;
|
|
|
|
|
|
|
|
$out .= '';
|
|
|
|
|
|
|
|
$result = file(LOGS . 'debug.log');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp(
|
2011-04-25 03:31:44 +00:00
|
|
|
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
|
2010-11-15 03:06:18 +00:00
|
|
|
$result[0]
|
|
|
|
);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/^Trace:/', $result[1]);
|
|
|
|
$this->assertRegExp('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
|
2010-11-15 03:06:18 +00:00
|
|
|
@unlink(LOGS . 'debug.log');
|
|
|
|
}
|
|
|
|
|
2010-08-28 05:39:02 +00:00
|
|
|
/**
|
|
|
|
* test handleException generating a page.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHandleException() {
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
|
2010-11-15 04:33:46 +00:00
|
|
|
|
|
|
|
$error = new NotFoundException('Kaboom!');
|
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleException($error);
|
|
|
|
$result = ob_get_clean();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
|
2010-11-15 04:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test handleException generating a page.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHandleExceptionLog() {
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.');
|
|
|
|
|
2010-11-15 04:33:46 +00:00
|
|
|
if (file_exists(LOGS . 'error.log')) {
|
|
|
|
unlink(LOGS . 'error.log');
|
|
|
|
}
|
|
|
|
Configure::write('Exception.log', true);
|
2010-09-05 06:05:31 +00:00
|
|
|
$error = new NotFoundException('Kaboom!');
|
2010-11-15 04:33:46 +00:00
|
|
|
|
2010-08-28 05:39:02 +00:00
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleException($error);
|
|
|
|
$result = ob_get_clean();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
|
2010-11-15 04:33:46 +00:00
|
|
|
|
|
|
|
$log = file(LOGS . 'error.log');
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertRegExp('/\[NotFoundException\] Kaboom!/', $log[0], 'message missing.');
|
|
|
|
$this->assertRegExp('/\#0.*ErrorHandlerTest->testHandleExceptionLog/', $log[1], 'Stack trace missing.');
|
2010-08-28 05:39:02 +00:00
|
|
|
}
|
|
|
|
|
2011-04-24 05:26:48 +00:00
|
|
|
/**
|
|
|
|
* tests it is possible to load a plugin exception renderer
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLoadPluginHanlder() {
|
2011-04-24 05:26:48 +00:00
|
|
|
App::build(array(
|
|
|
|
'plugins' => array(
|
2011-04-17 10:35:21 +00:00
|
|
|
CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
|
2011-04-24 05:26:48 +00:00
|
|
|
)
|
|
|
|
), true);
|
2011-05-10 04:17:29 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
2011-04-24 05:26:48 +00:00
|
|
|
Configure::write('Exception.renderer', 'TestPlugin.TestPluginExceptionRenderer');
|
|
|
|
$error = new NotFoundException('Kaboom!');
|
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleException($error);
|
|
|
|
$result = ob_get_clean();
|
|
|
|
$this->assertEquals($result, 'Rendered by test plugin');
|
2011-05-10 04:17:29 +00:00
|
|
|
CakePlugin::unload();
|
2011-04-24 05:26:48 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|