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.

140 lines
3.7 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. Config::set('DEBUG', false);
  72. $exception = $this->getMock('ErrorException');
  73. $controller = FrontController::getInstance();
  74. $controller->setView('SomeView');
  75. $action = new ErrorAction($exception);
  76. $result = $action->fetch();
  77. $this->assertSame('/actions/500', $result['template']);
  78. }
  79. /**
  80. * @runInSeparateProcess
  81. */
  82. public function testError404WithAjax()
  83. {
  84. $this->setConstants(false);
  85. $exception = $this->getMock('Error404Exception', array('getMessage', 'getFile', 'getLine', 'getTrace'));
  86. $exception->expects($this->once())
  87. ->method('getTrace')
  88. ->will($this->returnValue(array('one' => array('class' => 'AjaxAction'))));
  89. $action = new ErrorAction($exception);
  90. $this->assertSame($exception, $action->exception);
  91. }
  92. /**
  93. * @runInSeparateProcess
  94. */
  95. public function testError404NoAjax()
  96. {
  97. $this->setConstants(false);
  98. $exception = $this->getMock('Error404Exception', array('getMessage', 'getFile', 'getLine', 'getTrace'));
  99. $exception->expects($this->once())
  100. ->method('getTrace')
  101. ->will($this->returnValue(array('one' => array('class' => 'Action'))));
  102. $action = new ErrorAction($exception);
  103. $this->assertSame($exception, $action->exception);
  104. }
  105. private function setConstants($val = false)
  106. {
  107. Config::set('DEBUG', $val);
  108. }
  109. private function header()
  110. {
  111. ob_end_clean();
  112. flush();
  113. }
  114. public function tearDown()
  115. {
  116. parent::tearDown();
  117. ini_set('error_log', $this->log);
  118. unset_exit_overload();
  119. }
  120. }