ConsoleErrorHandlerTest.php 4.3 KB

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