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>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, 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
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, 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
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
* @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-08-31 02:59:10 +00:00
|
|
|
App::import('Core', array('ErrorHandler', 'Controller', 'Component'));
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-08 01:00:05 +00:00
|
|
|
/**
|
|
|
|
* Short description for class.
|
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-11-08 01:00:05 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
*/
|
|
|
|
class AuthBlueberryUser extends CakeTestModel {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-08 01:00:05 +00:00
|
|
|
/**
|
|
|
|
* name property
|
|
|
|
*
|
|
|
|
* @var string 'AuthBlueberryUser'
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = 'AuthBlueberryUser';
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-08 01:00:05 +00:00
|
|
|
/**
|
|
|
|
* useTable property
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $useTable = false;
|
2008-11-08 01:00:05 +00:00
|
|
|
}
|
2010-09-19 05:43:00 +00:00
|
|
|
|
2010-08-29 05:01:36 +00:00
|
|
|
/**
|
|
|
|
* BlueberryComponent class
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
*/
|
|
|
|
class BlueberryComponent extends Component {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testName property
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public $testName = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* initialize method
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function initialize(&$controller) {
|
|
|
|
$this->testName = 'BlueberryComponent';
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* TestErrorController class
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2010-09-25 14:41:08 +00:00
|
|
|
class TestErrorController extends Controller {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* uses property
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $uses = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-09-25 14:41:08 +00:00
|
|
|
* components property
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-25 14:41:08 +00:00
|
|
|
public $components = array('Blueberry');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-08 01:00:05 +00:00
|
|
|
/**
|
2010-09-25 14:41:08 +00:00
|
|
|
* beforeRender method
|
2008-11-08 01:00:05 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-25 14:41:08 +00:00
|
|
|
function beforeRender() {
|
|
|
|
echo $this->Blueberry->testName;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-08 01:00:05 +00:00
|
|
|
/**
|
2010-09-25 14:41:08 +00:00
|
|
|
* index method
|
2008-11-08 01:00:05 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-09-25 14:41:08 +00:00
|
|
|
function index() {
|
|
|
|
$this->autoRender = false;
|
|
|
|
return 'what up';
|
|
|
|
}
|
2008-11-08 01:00:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-10-09 04:13:08 +00:00
|
|
|
/**
|
|
|
|
* MyCustomErrorHandler class
|
|
|
|
*
|
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
*/
|
|
|
|
class MyCustomErrorHandler extends ErrorHandler {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* custom error message type.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2009-10-09 04:13:08 +00:00
|
|
|
function missingWidgetThing() {
|
|
|
|
echo 'widget thing is missing';
|
|
|
|
}
|
|
|
|
}
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
2010-08-29 17:49:10 +00:00
|
|
|
* Exception class for testing app error handlers and custom errors.
|
2008-06-11 09:02:54 +00:00
|
|
|
*
|
2010-08-29 17:49:10 +00:00
|
|
|
* @package cake.test.cases.libs
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2010-09-05 06:05:31 +00:00
|
|
|
class MissingWidgetThingException extends NotFoundException { }
|
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
|
|
|
*
|
2009-03-18 17:55:58 +00:00
|
|
|
* @package cake
|
2008-10-30 17:30:26 +00:00
|
|
|
* @subpackage cake.tests.cases.libs
|
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
|
|
|
|
2010-11-15 01:00:27 +00:00
|
|
|
var $_restoreError = false;
|
2010-05-29 04:21:02 +00:00
|
|
|
/**
|
|
|
|
* setup create a request object to get out of router later.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function setUp() {
|
2010-09-25 14:41:08 +00:00
|
|
|
App::build(array(
|
|
|
|
'views' => array(
|
|
|
|
TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS,
|
|
|
|
TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS
|
|
|
|
)
|
|
|
|
), 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
|
|
|
|
*/
|
2010-08-29 02:50:29 +00:00
|
|
|
function teardown() {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function testHandleErrorDebugOn() {
|
|
|
|
set_error_handler('ErrorHandler::handleError');
|
|
|
|
$this->_restoreError = true;
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$wrong .= '';
|
|
|
|
$result = ob_get_clean();
|
|
|
|
|
|
|
|
$this->assertPattern('/<pre class="cake-debug">/', $result);
|
|
|
|
$this->assertPattern('/<b>Notice<\/b>/', $result);
|
|
|
|
$this->assertPattern('/variable:\s+wrong/', $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that errors go into CakeLog when debug = 0.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testHandleErrorDebugOff() {
|
|
|
|
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');
|
|
|
|
$this->assertEqual(count($result), 1);
|
|
|
|
$this->assertPattern(
|
|
|
|
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} Notice: Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
|
|
|
|
$result[0]
|
|
|
|
);
|
|
|
|
@unlink(LOGS . 'debug.log');
|
|
|
|
}
|
|
|
|
|
2010-11-15 03:06:18 +00:00
|
|
|
/**
|
|
|
|
* Test that errors going into CakeLog include traces.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testHandleErrorLoggingTrace() {
|
|
|
|
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');
|
|
|
|
$this->assertPattern(
|
|
|
|
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} Notice: Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
|
|
|
|
$result[0]
|
|
|
|
);
|
|
|
|
$this->assertPattern('/^Trace:/', $result[1]);
|
|
|
|
$this->assertPattern('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
|
|
|
|
@unlink(LOGS . 'debug.log');
|
|
|
|
}
|
|
|
|
|
2010-08-28 05:39:02 +00:00
|
|
|
/**
|
|
|
|
* test handleException generating a page.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testHandleException() {
|
2010-09-03 19:35:11 +00:00
|
|
|
if ($this->skipIf(file_exists(APP . 'app_error.php'), 'App error exists cannot run.')) {
|
|
|
|
return;
|
|
|
|
}
|
2010-09-19 05:43:00 +00:00
|
|
|
if ($this->skipIf(PHP_SAPI == 'cli', 'This integration test can not be run in cli.')) {
|
|
|
|
return;
|
|
|
|
}
|
2010-09-05 06:05:31 +00:00
|
|
|
$error = new NotFoundException('Kaboom!');
|
2010-08-28 05:39:02 +00:00
|
|
|
ob_start();
|
|
|
|
ErrorHandler::handleException($error);
|
|
|
|
$result = ob_get_clean();
|
2010-08-29 03:32:14 +00:00
|
|
|
$this->assertPattern('/Kaboom!/', $result, 'message missing.');
|
2010-08-28 05:39:02 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|