ConsoleErrorHandlerTest.php 4.9 KB

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