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.

142 lines
3.8 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. class ErrorActionTest extends Action_TestCase
  14. {
  15. private $log;
  16. public function setUp()
  17. {
  18. parent::setUp();
  19. $this->log = ini_get('error_log');
  20. ini_set('error_log', '/dev/null');
  21. set_exit_overload(function()
  22. {
  23. return false;
  24. });
  25. }
  26. /**
  27. * @runInSeparateProcess
  28. */
  29. public function testErrorExceptionNotice()
  30. {
  31. $this->setConstants(false);
  32. $exception = $this->getMock('ErrorException', array(), array('', 0, E_NOTICE));
  33. $action = new ErrorAction($exception);
  34. $this->assertSame($exception, $action->exception);
  35. }
  36. /**
  37. * @runInSeparateProcess
  38. */
  39. public function testErrorExceptionWarning()
  40. {
  41. $this->setConstants(false);
  42. $exception = $this->getMock('ErrorException', array(), array('', 0, E_WARNING));
  43. $action = new ErrorAction($exception);
  44. $this->assertSame($exception, $action->exception);
  45. }
  46. /**
  47. * @runInSeparateProcess
  48. */
  49. public function testErrorExceptionError()
  50. {
  51. $this->setConstants(false);
  52. $exception = $this->getMock('ErrorException', array(), array('', 0, E_ERROR));
  53. $action = new ErrorAction($exception);
  54. $this->assertSame($exception, $action->exception);
  55. }
  56. /**
  57. * @runInSeparateProcess
  58. */
  59. public function testErrorExceptionCustom()
  60. {
  61. $this->setConstants(false);
  62. $exception = $this->getMock('ErrorException', array(), array('', 0, 211));
  63. $action = new ErrorAction($exception);
  64. $this->assertSame($exception, $action->exception);
  65. }
  66. /**
  67. * @runInSeparateProcess
  68. */
  69. public function testFetchNoTemplate()
  70. {
  71. if (!defined('DEBUG')) {
  72. define('DEBUG', false);
  73. }
  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 = $this->getMock('Error404Exception', array('getMessage', 'getFile', 'getLine', 'getTrace'));
  88. $exception->expects($this->once())
  89. ->method('getTrace')
  90. ->will($this->returnValue(array('one' => array('class' => 'AjaxAction'))));
  91. $action = new ErrorAction($exception);
  92. $this->assertSame($exception, $action->exception);
  93. }
  94. /**
  95. * @runInSeparateProcess
  96. */
  97. public function testError404NoAjax()
  98. {
  99. $this->setConstants(false);
  100. $exception = $this->getMock('Error404Exception', array('getMessage', 'getFile', 'getLine', 'getTrace'));
  101. $exception->expects($this->once())
  102. ->method('getTrace')
  103. ->will($this->returnValue(array('one' => array('class' => 'Action'))));
  104. $action = new ErrorAction($exception);
  105. $this->assertSame($exception, $action->exception);
  106. }
  107. private function setConstants($val = false)
  108. {
  109. if (!defined('DEBUG')) {
  110. define('DEBUG', $val);
  111. }
  112. }
  113. private function header()
  114. {
  115. ob_end_clean();
  116. flush();
  117. }
  118. public function tearDown()
  119. {
  120. parent::tearDown();
  121. ini_set('error_log', $this->log);
  122. unset_exit_overload();
  123. }
  124. }