ConsoleErrorHandlerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * ConsoleErrorHandler Test case
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 2.0
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Console;
  18. use Cake\Console\ConsoleErrorHandler;
  19. use Cake\Error;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * ConsoleErrorHandler Test case.
  23. */
  24. class ConsoleErrorHandlerTest extends TestCase {
  25. /**
  26. * setup, create mocks
  27. *
  28. * @return Mock object
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. $this->stderr = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  33. $this->Error = $this->getMock('Cake\Console\ConsoleErrorHandler', ['_stop'], [['stderr' => $this->stderr]]);
  34. }
  35. /**
  36. * tearDown
  37. *
  38. * @return void
  39. */
  40. public function tearDown() {
  41. unset($this->Error);
  42. parent::tearDown();
  43. }
  44. /**
  45. * test that the console error handler can deal with Exceptions.
  46. *
  47. * @return void
  48. */
  49. public function testHandleError() {
  50. $content = "<error>Notice Error:</error> This is a notice error in [/some/file, line 275]\n";
  51. $this->stderr->expects($this->once())->method('write')
  52. ->with($content);
  53. $this->Error->expects($this->never())
  54. ->method('_stop');
  55. $this->Error->handleError(E_NOTICE, 'This is a notice error', '/some/file', 275);
  56. }
  57. /**
  58. * test that the console error handler can deal with fatal errors.
  59. *
  60. * @return void
  61. */
  62. public function testHandleFatalError() {
  63. ob_start();
  64. $content = "<error>Fatal Error:</error> This is a fatal error in [/some/file, line 275]";
  65. $this->stderr->expects($this->once())->method('write')
  66. ->with($this->stringContains($content));
  67. $this->Error->handleError(E_USER_ERROR, 'This is a fatal error', '/some/file', 275);
  68. }
  69. /**
  70. * test that the console error handler can deal with CakeExceptions.
  71. *
  72. * @return void
  73. */
  74. public function testCakeErrors() {
  75. $exception = new Error\MissingActionException('Missing action');
  76. $message = sprintf('Missing action in [%s, line %s]', $exception->getFile(), $exception->getLine());
  77. $this->stderr->expects($this->once())->method('write')
  78. ->with($this->stringContains($message));
  79. $this->Error->expects($this->once())
  80. ->method('_stop');
  81. $this->Error->handleException($exception);
  82. }
  83. /**
  84. * test a non Cake Exception exception.
  85. *
  86. * @return void
  87. */
  88. public function testNonCakeExceptions() {
  89. $exception = new \InvalidArgumentException('Too many parameters.');
  90. $this->stderr->expects($this->once())->method('write')
  91. ->with($this->stringContains('Too many parameters.'));
  92. $this->Error->handleException($exception);
  93. }
  94. /**
  95. * test a Error404 exception.
  96. *
  97. * @return void
  98. */
  99. public function testError404Exception() {
  100. $exception = new Error\NotFoundException('dont use me in cli.');
  101. $this->stderr->expects($this->once())->method('write')
  102. ->with($this->stringContains('dont use me in cli.'));
  103. $this->Error->handleException($exception);
  104. }
  105. /**
  106. * test a Error500 exception.
  107. *
  108. * @return void
  109. */
  110. public function testError500Exception() {
  111. $exception = new Error\InternalErrorException('dont use me in cli.');
  112. $this->stderr->expects($this->once())->method('write')
  113. ->with($this->stringContains('dont use me in cli.'));
  114. $this->Error->handleException($exception);
  115. }
  116. }