ConsoleErrorHandlerTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 2.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Error;
  17. use Cake\Controller\Exception\MissingActionException;
  18. use Cake\Core\Exception\Exception;
  19. use Cake\Http\Exception\InternalErrorException;
  20. use Cake\Http\Exception\NotFoundException;
  21. use Cake\Log\Log;
  22. use Cake\TestSuite\Stub\ConsoleOutput;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * ConsoleErrorHandler Test case.
  26. */
  27. class ConsoleErrorHandlerTest extends TestCase
  28. {
  29. /**
  30. * setup, create mocks
  31. *
  32. * @return void
  33. */
  34. public function setUp(): void
  35. {
  36. parent::setUp();
  37. $this->stderr = new ConsoleOutput();
  38. $this->Error = $this->getMockBuilder('Cake\Error\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(): void
  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\nIn [/some/file, line 275]\n";
  62. $this->Error->expects($this->never())
  63. ->method('_stop');
  64. $this->Error->handleError(E_NOTICE, 'This is a notice error', '/some/file', 275);
  65. $this->assertEquals($content, $this->stderr->messages()[0]);
  66. }
  67. /**
  68. * test that the console error handler can deal with fatal errors.
  69. *
  70. * @return void
  71. */
  72. public function testHandleFatalError()
  73. {
  74. ob_start();
  75. $content = "<error>Fatal Error:</error> This is a fatal error\nIn [/some/file, line 275]\n";
  76. $this->Error->handleError(E_USER_ERROR, 'This is a fatal error', '/some/file', 275);
  77. $this->assertCount(1, $this->stderr->messages());
  78. $this->assertEquals($content, $this->stderr->messages()[0]);
  79. ob_end_clean();
  80. }
  81. /**
  82. * test that the console error handler can deal with CakeExceptions.
  83. *
  84. * @return void
  85. */
  86. public function testCakeErrors()
  87. {
  88. $exception = new MissingActionException('Missing action');
  89. $message = sprintf("<error>Exception:</error> Missing action\nIn [%s, line %s]\n", $exception->getFile(), $exception->getLine());
  90. $this->Error->expects($this->once())
  91. ->method('_stop');
  92. $this->Error->handleException($exception);
  93. $this->assertCount(1, $this->stderr->messages());
  94. $this->assertEquals($message, $this->stderr->messages()[0]);
  95. }
  96. /**
  97. * test a non Cake Exception exception.
  98. *
  99. * @return void
  100. */
  101. public function testNonCakeExceptions()
  102. {
  103. $exception = new \InvalidArgumentException('Too many parameters.');
  104. $this->Error->handleException($exception);
  105. $this->assertStringContainsString('Too many parameters', $this->stderr->messages()[0]);
  106. }
  107. /**
  108. * test a Error404 exception.
  109. *
  110. * @return void
  111. */
  112. public function testError404Exception()
  113. {
  114. $exception = new NotFoundException("don't use me in cli.");
  115. $this->Error->handleException($exception);
  116. $this->assertStringContainsString("don't use me in cli", $this->stderr->messages()[0]);
  117. }
  118. /**
  119. * test a Error500 exception.
  120. *
  121. * @return void
  122. */
  123. public function testError500Exception()
  124. {
  125. $exception = new InternalErrorException("don't use me in cli.");
  126. $this->Error->handleException($exception);
  127. $this->assertStringContainsString("don't use me in cli", $this->stderr->messages()[0]);
  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->Error->expects($this->once())
  142. ->method('_stop')
  143. ->with(1);
  144. $this->Error->handleException($exception);
  145. $this->assertStringContainsString('Non-integer exception code', $this->stderr->messages()[0]);
  146. }
  147. }