ConsoleErrorHandlerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.tests.cases.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.tests.cases.console
  24. */
  25. class ConsoleErrorHandlerTest extends CakeTestCase {
  26. /**
  27. * setup, create mocks
  28. *
  29. * @return Mock object
  30. */
  31. function setUp() {
  32. parent::setUp();
  33. ConsoleErrorHandler::$stderr = $this->getMock('ConsoleOutput', array(), array(), '', false);
  34. }
  35. /**
  36. * teardown
  37. *
  38. * @return void
  39. */
  40. function tearDown() {
  41. parent::tearDown();
  42. }
  43. /**
  44. * test that the console error handler can deal with CakeExceptions.
  45. *
  46. * @return void
  47. */
  48. function testHandleError() {
  49. $content = "<error>Notice Error:</error> This is a notice error in [/some/file, line 275]\n";
  50. ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
  51. ->with($content);
  52. ConsoleErrorHandler::handleError(E_NOTICE, 'This is a notice error', '/some/file', 275);
  53. }
  54. /**
  55. * test that the console error handler can deal with CakeExceptions.
  56. *
  57. * @return void
  58. */
  59. function testCakeErrors() {
  60. $exception = new MissingActionException('Missing action');
  61. ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
  62. ->with($this->stringContains('Missing action'));
  63. ConsoleErrorHandler::handleException($exception);
  64. }
  65. /**
  66. * test a non CakeException exception.
  67. *
  68. * @return void
  69. */
  70. function testNonCakeExceptions() {
  71. $exception = new InvalidArgumentException('Too many parameters.');
  72. ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
  73. ->with($this->stringContains('Too many parameters.'));
  74. ConsoleErrorHandler::handleException($exception);
  75. }
  76. /**
  77. * test a Error404 exception.
  78. *
  79. * @return void
  80. */
  81. function testError404Exception() {
  82. $exception = new NotFoundException('dont use me in cli.');
  83. ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
  84. ->with($this->stringContains('dont use me in cli.'));
  85. ConsoleErrorHandler::handleException($exception);
  86. }
  87. /**
  88. * test a Error500 exception.
  89. *
  90. * @return void
  91. */
  92. function testError500Exception() {
  93. $exception = new InternalErrorException('dont use me in cli.');
  94. ConsoleErrorHandler::$stderr->expects($this->once())->method('write')
  95. ->with($this->stringContains('dont use me in cli.'));
  96. ConsoleErrorHandler::handleException($exception);
  97. }
  98. }