ConsoleErrorHandlerTest.php 4.3 KB

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