ConsoleErrorHandlerTest.php 3.8 KB

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