ConsoleErrorHandlerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Console;
  16. use Cake\Console\ConsoleErrorHandler;
  17. use Cake\Controller\Error\MissingActionException;
  18. use Cake\Error;
  19. use Cake\Log\Log;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * ConsoleErrorHandler Test case.
  23. */
  24. class ConsoleErrorHandlerTest extends TestCase {
  25. /**
  26. * setup, create mocks
  27. *
  28. * @return Mock object
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $this->stderr = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  33. $this->Error = $this->getMock('Cake\Console\ConsoleErrorHandler', ['_stop'], [['stderr' => $this->stderr]]);
  34. Log::drop('stderr');
  35. }
  36. /**
  37. * tearDown
  38. *
  39. * @return void
  40. */
  41. public function tearDown() {
  42. unset($this->Error);
  43. parent::tearDown();
  44. }
  45. /**
  46. * test that the console error handler can deal with Exceptions.
  47. *
  48. * @return void
  49. */
  50. public function testHandleError() {
  51. $content = "<error>Notice Error:</error> This is a notice error in [/some/file, line 275]\n";
  52. $this->stderr->expects($this->once())->method('write')
  53. ->with($content);
  54. $this->Error->expects($this->never())
  55. ->method('_stop');
  56. $this->Error->handleError(E_NOTICE, 'This is a notice error', '/some/file', 275);
  57. }
  58. /**
  59. * test that the console error handler can deal with fatal errors.
  60. *
  61. * @return void
  62. */
  63. public function testHandleFatalError() {
  64. ob_start();
  65. $content = "<error>Fatal Error:</error> This is a fatal error in [/some/file, line 275]";
  66. $this->stderr->expects($this->once())->method('write')
  67. ->with($this->stringContains($content));
  68. $this->Error->handleError(E_USER_ERROR, 'This is a fatal error', '/some/file', 275);
  69. }
  70. /**
  71. * test that the console error handler can deal with CakeExceptions.
  72. *
  73. * @return void
  74. */
  75. public function testCakeErrors() {
  76. $exception = new MissingActionException('Missing action');
  77. $message = sprintf('Missing action in [%s, line %s]', $exception->getFile(), $exception->getLine());
  78. $this->stderr->expects($this->once())->method('write')
  79. ->with($this->stringContains($message));
  80. $this->Error->expects($this->once())
  81. ->method('_stop');
  82. $this->Error->handleException($exception);
  83. }
  84. /**
  85. * test a non Cake Exception exception.
  86. *
  87. * @return void
  88. */
  89. public function testNonCakeExceptions() {
  90. $exception = new \InvalidArgumentException('Too many parameters.');
  91. $this->stderr->expects($this->once())->method('write')
  92. ->with($this->stringContains('Too many parameters.'));
  93. $this->Error->handleException($exception);
  94. }
  95. /**
  96. * test a Error404 exception.
  97. *
  98. * @return void
  99. */
  100. public function testError404Exception() {
  101. $exception = new Error\NotFoundException('dont use me in cli.');
  102. $this->stderr->expects($this->once())->method('write')
  103. ->with($this->stringContains('dont use me in cli.'));
  104. $this->Error->handleException($exception);
  105. }
  106. /**
  107. * test a Error500 exception.
  108. *
  109. * @return void
  110. */
  111. public function testError500Exception() {
  112. $exception = new Error\InternalErrorException('dont use me in cli.');
  113. $this->stderr->expects($this->once())->method('write')
  114. ->with($this->stringContains('dont use me in cli.'));
  115. $this->Error->handleException($exception);
  116. }
  117. /**
  118. * test a exception with non-integer code
  119. *
  120. * @return void
  121. */
  122. public function testNonIntegerExceptionCode() {
  123. if (PHP_VERSION_ID < 50300) {
  124. $this->markTestSkipped('ReflectionProperty::setAccessible() is available since 5.3');
  125. }
  126. $exception = new Exception('Non-integer exception code');
  127. $class = new ReflectionClass('Exception');
  128. $property = $class->getProperty('code');
  129. $property->setAccessible(true);
  130. $property->setValue($exception, '42S22');
  131. ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
  132. ->with($this->stringContains('Non-integer exception code'));
  133. $this->Error->expects($this->once())
  134. ->method('_stop')
  135. ->with(1);
  136. $this->Error->handleException($exception);
  137. }
  138. }