ConsoleErrorHandlerTest.php 5.0 KB

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