ConsoleErrorHandlerTest.php 4.9 KB

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