2010-09-02 22:02:58 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ConsoleErrorHandler Test case
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-09-02 22:02:58 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-09-02 22:02:58 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Console
|
2010-09-02 22:02:58 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2010-09-02 22:02:58 +00:00
|
|
|
*/
|
2011-03-08 19:16:21 +00:00
|
|
|
|
|
|
|
App::uses('ConsoleErrorHandler', 'Console');
|
2010-09-02 22:02:58 +00:00
|
|
|
|
|
|
|
/**
|
2010-09-05 15:22:39 +00:00
|
|
|
* ConsoleErrorHandler Test case.
|
2010-09-02 22:02:58 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Console
|
2010-09-02 22:02:58 +00:00
|
|
|
*/
|
2010-09-05 15:22:39 +00:00
|
|
|
class ConsoleErrorHandlerTest extends CakeTestCase {
|
2010-09-02 22:02:58 +00:00
|
|
|
|
|
|
|
/**
|
2010-11-15 03:47:35 +00:00
|
|
|
* setup, create mocks
|
2010-09-02 22:02:58 +00:00
|
|
|
*
|
2010-09-05 15:22:39 +00:00
|
|
|
* @return Mock object
|
2010-09-02 22:02:58 +00:00
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function setUp() : void {
|
2010-11-15 03:47:35 +00:00
|
|
|
parent::setUp();
|
2011-10-03 03:16:41 +00:00
|
|
|
$this->Error = $this->getMock('ConsoleErrorHandler', array('_stop'));
|
2010-11-15 03:47:35 +00:00
|
|
|
ConsoleErrorHandler::$stderr = $this->getMock('ConsoleOutput', array(), array(), '', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* tearDown
|
2010-11-15 03:47:35 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function tearDown() : void {
|
2011-10-03 03:16:41 +00:00
|
|
|
unset($this->Error);
|
2010-11-15 03:47:35 +00:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that the console error handler can deal with CakeExceptions.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testHandleError() {
|
2010-12-04 22:49:09 +00:00
|
|
|
$content = "<error>Notice Error:</error> This is a notice error in [/some/file, line 275]\n";
|
2010-11-15 03:47:35 +00:00
|
|
|
ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
|
|
|
|
->with($content);
|
|
|
|
|
2011-10-03 03:16:41 +00:00
|
|
|
$this->Error->handleError(E_NOTICE, 'This is a notice error', '/some/file', 275);
|
2010-09-05 15:22:39 +00:00
|
|
|
}
|
2010-09-02 22:02:58 +00:00
|
|
|
|
2012-09-14 12:17:07 +00:00
|
|
|
/**
|
|
|
|
* test that the console error handler can deal with fatal errors.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testHandleFatalError() {
|
|
|
|
$content = "<error>Fatal Error Error:</error> This is a fatal error in [/some/file, line 275]\n";
|
|
|
|
ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
|
|
|
|
->with($content);
|
|
|
|
|
|
|
|
$this->Error->expects($this->once())
|
|
|
|
->method('_stop')
|
|
|
|
->with(1);
|
|
|
|
|
|
|
|
$this->Error->handleError(E_USER_ERROR, 'This is a fatal error', '/some/file', 275);
|
|
|
|
}
|
|
|
|
|
2010-09-02 22:02:58 +00:00
|
|
|
/**
|
|
|
|
* test that the console error handler can deal with CakeExceptions.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCakeErrors() {
|
2010-09-02 22:02:58 +00:00
|
|
|
$exception = new MissingActionException('Missing action');
|
2010-11-15 03:47:35 +00:00
|
|
|
ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
|
2010-09-09 03:38:51 +00:00
|
|
|
->with($this->stringContains('Missing action'));
|
2010-09-02 22:02:58 +00:00
|
|
|
|
2011-10-03 03:16:41 +00:00
|
|
|
$this->Error->expects($this->once())
|
|
|
|
->method('_stop')
|
|
|
|
->with(404);
|
|
|
|
|
|
|
|
$this->Error->handleException($exception);
|
2010-09-02 22:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test a non CakeException exception.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testNonCakeExceptions() {
|
2010-09-02 22:02:58 +00:00
|
|
|
$exception = new InvalidArgumentException('Too many parameters.');
|
|
|
|
|
2010-11-15 03:47:35 +00:00
|
|
|
ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
|
2010-09-09 03:38:51 +00:00
|
|
|
->with($this->stringContains('Too many parameters.'));
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2011-10-03 03:16:41 +00:00
|
|
|
$this->Error->expects($this->once())
|
|
|
|
->method('_stop')
|
|
|
|
->with(1);
|
|
|
|
|
|
|
|
$this->Error->handleException($exception);
|
2010-09-02 22:02:58 +00:00
|
|
|
}
|
2010-09-02 22:04:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test a Error404 exception.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testError404Exception() {
|
2010-09-05 15:10:48 +00:00
|
|
|
$exception = new NotFoundException('dont use me in cli.');
|
2011-10-28 05:01:17 +00:00
|
|
|
|
2010-11-15 03:47:35 +00:00
|
|
|
ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
|
2010-09-09 03:38:51 +00:00
|
|
|
->with($this->stringContains('dont use me in cli.'));
|
2010-09-05 15:22:39 +00:00
|
|
|
|
2011-10-03 03:16:41 +00:00
|
|
|
$this->Error->expects($this->once())
|
|
|
|
->method('_stop')
|
|
|
|
->with(404);
|
|
|
|
|
|
|
|
$this->Error->handleException($exception);
|
2010-09-02 22:04:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test a Error500 exception.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testError500Exception() {
|
2010-09-05 15:10:48 +00:00
|
|
|
$exception = new InternalErrorException('dont use me in cli.');
|
2010-09-02 22:04:50 +00:00
|
|
|
|
2010-11-15 03:47:35 +00:00
|
|
|
ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
|
2010-09-09 03:38:51 +00:00
|
|
|
->with($this->stringContains('dont use me in cli.'));
|
2010-09-05 15:22:39 +00:00
|
|
|
|
2011-10-03 03:16:41 +00:00
|
|
|
$this->Error->expects($this->once())
|
|
|
|
->method('_stop')
|
|
|
|
->with(500);
|
|
|
|
|
|
|
|
$this->Error->handleException($exception);
|
2010-09-02 22:04:50 +00:00
|
|
|
}
|
2010-09-04 14:46:04 +00:00
|
|
|
|
2014-06-26 11:12:28 +00:00
|
|
|
/**
|
|
|
|
* test a exception with non-integer code
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNonIntegerExceptionCode() {
|
|
|
|
if (PHP_VERSION_ID < 50300) {
|
|
|
|
$this->markTestSkipped('ReflectionProperty::setAccessible() is available since 5.3');
|
|
|
|
}
|
|
|
|
|
|
|
|
$exception = new Exception('Non-integer exception code');
|
|
|
|
|
|
|
|
$class = new ReflectionClass('Exception');
|
|
|
|
$property = $class->getProperty('code');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
$property->setValue($exception, '42S22');
|
|
|
|
|
|
|
|
ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
|
|
|
|
->with($this->stringContains('Non-integer exception code'));
|
|
|
|
|
|
|
|
$this->Error->expects($this->once())
|
|
|
|
->method('_stop')
|
|
|
|
->with(1);
|
|
|
|
|
|
|
|
$this->Error->handleException($exception);
|
|
|
|
}
|
|
|
|
|
2011-10-03 03:16:41 +00:00
|
|
|
}
|