ConsoleErrorHandlerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\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. }
  72. /**
  73. * test that the console error handler can deal with CakeExceptions.
  74. *
  75. * @return void
  76. */
  77. public function testCakeErrors() {
  78. $exception = new MissingActionException('Missing action');
  79. $message = sprintf('Missing action in [%s, line %s]', $exception->getFile(), $exception->getLine());
  80. $this->stderr->expects($this->once())->method('write')
  81. ->with($this->stringContains($message));
  82. $this->Error->expects($this->once())
  83. ->method('_stop');
  84. $this->Error->handleException($exception);
  85. }
  86. /**
  87. * test a non Cake Exception exception.
  88. *
  89. * @return void
  90. */
  91. public function testNonCakeExceptions() {
  92. $exception = new \InvalidArgumentException('Too many parameters.');
  93. $this->stderr->expects($this->once())->method('write')
  94. ->with($this->stringContains('Too many parameters.'));
  95. $this->Error->handleException($exception);
  96. }
  97. /**
  98. * test a Error404 exception.
  99. *
  100. * @return void
  101. */
  102. public function testError404Exception() {
  103. $exception = new NotFoundException('dont use me in cli.');
  104. $this->stderr->expects($this->once())->method('write')
  105. ->with($this->stringContains('dont use me in cli.'));
  106. $this->Error->handleException($exception);
  107. }
  108. /**
  109. * test a Error500 exception.
  110. *
  111. * @return void
  112. */
  113. public function testError500Exception() {
  114. $exception = new InternalErrorException('dont use me in cli.');
  115. $this->stderr->expects($this->once())->method('write')
  116. ->with($this->stringContains('dont use me in cli.'));
  117. $this->Error->handleException($exception);
  118. }
  119. /**
  120. * test a exception with non-integer code
  121. *
  122. * @return void
  123. */
  124. public function testNonIntegerExceptionCode() {
  125. $exception = new Exception('Non-integer exception code');
  126. $class = new \ReflectionClass('Exception');
  127. $property = $class->getProperty('code');
  128. $property->setAccessible(true);
  129. $property->setValue($exception, '42S22');
  130. $this->stderr->expects($this->once())->method('write')
  131. ->with($this->stringContains('Non-integer exception code'));
  132. $this->Error->expects($this->once())
  133. ->method('_stop')
  134. ->with(1);
  135. $this->Error->handleException($exception);
  136. }
  137. }