You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.4 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-11-1
  8. *
  9. * Unit tests for ErrorAction class
  10. */
  11. require_once dirname(__FILE__) . '/Action_TestCase.php';
  12. require_once dirname(__FILE__) . '/../../app/ErrorAction.php';
  13. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  14. require_once dirname(__FILE__) . '/../../exception/ErrorHTTPException.php';
  15. require_once dirname(__FILE__) . '/../../exception/Error404Exception.php';
  16. class ErrorActionTest extends Action_TestCase
  17. {
  18. private $log;
  19. public function setUp()
  20. {
  21. parent::setUp();
  22. $this->log = ini_get('error_log');
  23. ini_set('error_log', '/dev/null');
  24. set_exit_overload(function () {
  25. return false;
  26. });
  27. }
  28. /**
  29. * @runInSeparateProcess
  30. */
  31. public function testErrorExceptionNotice()
  32. {
  33. $this->setConstants(false);
  34. $exception = $this->getMock('ErrorException', array(), array('', 0, E_NOTICE));
  35. $action = new ErrorAction($exception);
  36. $this->assertSame($exception, $action->exception);
  37. }
  38. /**
  39. * @runInSeparateProcess
  40. */
  41. public function testErrorExceptionWarning()
  42. {
  43. $this->setConstants(false);
  44. $exception = $this->getMock('ErrorException', array(), array('', 0, E_WARNING));
  45. $action = new ErrorAction($exception);
  46. $this->assertSame($exception, $action->exception);
  47. }
  48. /**
  49. * @runInSeparateProcess
  50. */
  51. public function testErrorExceptionError()
  52. {
  53. $this->setConstants(false);
  54. $exception = $this->getMock('ErrorException', array(), array('', 0, E_ERROR));
  55. $action = new ErrorAction($exception);
  56. $this->assertSame($exception, $action->exception);
  57. }
  58. /**
  59. * @runInSeparateProcess
  60. */
  61. public function testErrorExceptionCustom()
  62. {
  63. $this->setConstants(false);
  64. $exception = $this->getMock('ErrorException', array(), array('', 0, 211));
  65. $action = new ErrorAction($exception);
  66. $this->assertSame($exception, $action->exception);
  67. }
  68. /**
  69. * @runInSeparateProcess
  70. */
  71. public function testFetchNoTemplate()
  72. {
  73. Config::set('DEBUG', false);
  74. $exception = $this->getMock('ErrorException');
  75. $controller = FrontController::getInstance();
  76. $controller->setView('SomeView');
  77. $action = new ErrorAction($exception);
  78. $result = $action->fetch();
  79. $this->assertSame('/actions/500', $result['template']);
  80. }
  81. /**
  82. * @runInSeparateProcess
  83. */
  84. public function testError404WithAjax()
  85. {
  86. $this->setConstants(false);
  87. $exception = new Error404Exception('Some message');
  88. $controller = FrontController::getInstance();
  89. $controller->setView('SomeView');
  90. $action = new ErrorAction($exception);
  91. $action->setAjaxError();
  92. $this->assertSame($exception, $action->exception);
  93. $result = $action->fetch();
  94. $this->assertSame('Some message', $result);
  95. }
  96. /**
  97. * @runInSeparateProcess
  98. */
  99. public function testError404NoAjax()
  100. {
  101. $this->setConstants(false);
  102. $exception = new Error404Exception('Some message');
  103. $controller = FrontController::getInstance();
  104. $controller->setView('SomeView');
  105. $action = new ErrorAction($exception);
  106. $this->assertSame($exception, $action->exception);
  107. $result = $action->fetch();
  108. $this->assertSame('/actions/404', $result['template']);
  109. }
  110. /**
  111. * @runInSeparateProcess
  112. */
  113. public function testErrorHTTP()
  114. {
  115. $this->setConstants(false);
  116. $exception = new ErrorHTTPException('Some message', 410);
  117. $controller = FrontController::getInstance();
  118. $controller->setView('SomeView');
  119. $action = new ErrorAction($exception);
  120. $this->assertSame($exception, $action->exception);
  121. $result = $action->fetch();
  122. $this->assertSame('/actions/HTTP', $result['template']);
  123. }
  124. private function setConstants($val = false)
  125. {
  126. Config::set('DEBUG', $val);
  127. }
  128. private function header()
  129. {
  130. ob_end_clean();
  131. flush();
  132. }
  133. public function tearDown()
  134. {
  135. parent::tearDown();
  136. ini_set('error_log', $this->log);
  137. unset_exit_overload();
  138. }
  139. }