ConsoleErrorHandlerTest.php 3.8 KB

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