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