ConsoleErrorHandlerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\Console\Exception\ConsoleException;
  18. use Cake\Controller\Exception\MissingActionException;
  19. use Cake\Log\Log;
  20. use Cake\TestSuite\Stub\ConsoleOutput;
  21. use Cake\TestSuite\TestCase;
  22. use InvalidArgumentException;
  23. /**
  24. * ConsoleErrorHandler Test case.
  25. */
  26. class ConsoleErrorHandlerTest extends TestCase
  27. {
  28. /**
  29. * @var \Cake\Error\ConsoleErrorHandler|\PHPUnit\Framework\MockObject\MockObject
  30. */
  31. protected $Error;
  32. /**
  33. * @var \Cake\TestSuite\Stub\ConsoleOutput
  34. */
  35. protected $stderr;
  36. /**
  37. * setup, create mocks
  38. */
  39. public function setUp(): void
  40. {
  41. parent::setUp();
  42. $this->stderr = new ConsoleOutput();
  43. $this->Error = $this->getMockBuilder('Cake\Error\ConsoleErrorHandler')
  44. ->onlyMethods(['_stop'])
  45. ->setConstructorArgs([['stderr' => $this->stderr]])
  46. ->getMock();
  47. Log::drop('stdout');
  48. Log::drop('stderr');
  49. }
  50. /**
  51. * tearDown
  52. */
  53. public function tearDown(): void
  54. {
  55. unset($this->Error);
  56. parent::tearDown();
  57. }
  58. /**
  59. * test that the console error handler can deal with Exceptions.
  60. */
  61. public function testHandleError(): void
  62. {
  63. $content = "<error>Notice Error:</error> This is a notice error\nIn [/some/file, line 275]\n";
  64. $this->Error->expects($this->never())
  65. ->method('_stop');
  66. $this->Error->handleError(E_NOTICE, 'This is a notice error', '/some/file', 275);
  67. $this->assertSame($content, $this->stderr->messages()[0]);
  68. }
  69. /**
  70. * test that the console error handler can deal with fatal errors.
  71. */
  72. public function testHandleFatalError(): void
  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->assertSame($content, $this->stderr->messages()[0]);
  79. ob_end_clean();
  80. }
  81. /**
  82. * test that the console error handler can deal with CakeExceptions.
  83. */
  84. public function testCakeErrors(): void
  85. {
  86. $exception = new MissingActionException('Missing action');
  87. $message = sprintf("<error>Exception:</error> Missing action\nIn [%s, line %s]\n", $exception->getFile(), $exception->getLine());
  88. $this->Error->expects($this->once())
  89. ->method('_stop')
  90. ->with(1);
  91. $this->Error->handleException($exception);
  92. $this->assertCount(1, $this->stderr->messages());
  93. $this->assertSame($message, $this->stderr->messages()[0]);
  94. }
  95. /**
  96. * test a non Cake Exception exception.
  97. */
  98. public function testNonCakeExceptions(): void
  99. {
  100. $exception = new InvalidArgumentException('Too many parameters.');
  101. $this->Error->expects($this->once())
  102. ->method('_stop')
  103. ->with(1);
  104. $this->Error->handleException($exception);
  105. $this->assertStringContainsString('Too many parameters', $this->stderr->messages()[0]);
  106. }
  107. /**
  108. * Test error code is used as exit code for ConsoleException.
  109. */
  110. public function testConsoleExceptions(): void
  111. {
  112. $exception = new ConsoleException('Test ConsoleException', 2);
  113. $this->Error->expects($this->once())
  114. ->method('_stop')
  115. ->with(2);
  116. $this->Error->handleException($exception);
  117. $this->assertStringContainsString('Test ConsoleException', $this->stderr->messages()[0]);
  118. }
  119. }