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.

179 lines
5.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-10-11
  8. *
  9. * Unit tests for ErrorHandler class
  10. */
  11. require_once dirname(__FILE__) . '/../../classes/Env.class.php';
  12. require_once dirname(__FILE__) . '/../../session/Session.php';
  13. require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
  14. class ErrorHandlerTest extends PHPUnit_Framework_TestCase
  15. {
  16. public $old_eh = array('PHPUnit_Util_ErrorHandler', 'handleError');
  17. protected $log_file_name = 'error_log_file';
  18. public function setUp()
  19. {
  20. set_error_handler(array('ErrorHandler', 'error_handler'));
  21. ob_start();
  22. }
  23. public function testErrorHandlerInit()
  24. {
  25. $my_eh = array('ErrorHandler', 'error_handler');
  26. ErrorHandler::init();
  27. $eh = set_error_handler($my_eh);
  28. $this->assertInternalType('array', $eh);
  29. $this->assertSame($eh, $my_eh);
  30. }
  31. public function testHandleError()
  32. {
  33. $this->setExpectedException('ErrorException', 'test error');
  34. trigger_error("test error", E_USER_ERROR);
  35. }
  36. public function testHandleAt()
  37. {
  38. $my_eh = array('ErrorHandler', 'error_handler');
  39. ErrorHandler::init();
  40. set_error_handler($my_eh);
  41. $var = '';
  42. $ok = @$var['some'];
  43. $this->assertSame('', $var);
  44. ob_start();
  45. $this->setExpectedException('ErrorException');
  46. $ex = $var['some'];
  47. }
  48. /**
  49. * @TODO: ErrorHandler->wrapTrace() not used
  50. */
  51. public function testShowDebug()
  52. {
  53. try {
  54. throw new ErrorException("test error", E_USER_ERROR);
  55. }
  56. catch (ErrorException $e) {
  57. $_SESSION['some'] = 'value';
  58. $result = ErrorHandler::showDebug($e);
  59. $this->assertNotEmpty($result);
  60. $this->assertStringStartsWith('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', $result);
  61. $this->assertStringEndsWith('</html>', $result);
  62. }
  63. }
  64. /**
  65. * @TODO: ErrorHandler::wrapTrace not used
  66. * @TODO: nl2br() adds html <br /> but leaves original linebreak line \n
  67. */
  68. public function testWrapTrace()
  69. {
  70. $class = new ReflectionClass('ErrorHandler');
  71. $method = $class->getMethod('WrapTrace');
  72. $method->setAccessible(true);
  73. $result = $method->invoke(null, "first line\nsecond line");
  74. $this->assertSame("<code>first line<br />\nsecond line</code>", $result);
  75. $result = $method->invoke(null, "first line\r\nsecond line");
  76. $this->assertSame("<code>first line<br />\r\nsecond line</code>", $result);
  77. $result = $method->invoke(null, "first line\r\n\r\nsecond line");
  78. $this->assertSame("<code>first line<br />\r\n<br />\r\nsecond line</code>", $result);
  79. }
  80. /**
  81. * @runInSeparateProcess
  82. */
  83. public function testLogErrorDefaultException()
  84. {
  85. $ex = new Exception('message', 123);
  86. ini_set('error_log', $this->log_file_name);
  87. ErrorHandler::logError($ex);
  88. $log = file_get_contents($this->log_file_name);
  89. $this->assertContains('PHP Unknown Error: message in ', $log);
  90. }
  91. /**
  92. * @runInSeparateProcess
  93. */
  94. public function testLogErrorDefaultExceptionWithHTTP()
  95. {
  96. $_SERVER['REQUEST_METHOD'] = 'GET';
  97. $_SERVER['REQUEST_URI'] = '/somelongurl';
  98. $_SERVER['HTTP_REFERER'] = 'http://referrer/url';
  99. $this->assertEquals('GET', ENV::Server('REQUEST_METHOD'));
  100. $this->assertEquals('/somelongurl', ENV::Server('REQUEST_URI'));
  101. $this->assertEquals('http://referrer/url', ENV::Server('HTTP_REFERER'));
  102. $ex = new Exception('message', 123);
  103. ini_set('error_log', $this->log_file_name);
  104. ErrorHandler::logError($ex);
  105. $log = file_get_contents($this->log_file_name);
  106. $this->assertContains('PHP Unknown Error: message in ', $log);
  107. $this->assertContains('URL: GET /somelongurl, referrer: http://referrer/url', $log);
  108. }
  109. /**
  110. * @runInSeparateProcess
  111. */
  112. public function testLogErrorCustomException()
  113. {
  114. $ex = new LogicException('Logic', 333);
  115. ini_set('error_log', $this->log_file_name);
  116. ErrorHandler::logError($ex);
  117. $log = file_get_contents($this->log_file_name);
  118. $this->assertContains('PHP Unknown Error: Logic in ', $log);
  119. }
  120. /**
  121. * @runInSeparateProcess
  122. */
  123. public function testLogErrorErrorExceptionNotice()
  124. {
  125. $ex = new ErrorException('message', 321, E_NOTICE);
  126. ini_set('error_log', $this->log_file_name);
  127. ErrorHandler::logError($ex);
  128. $log = file_get_contents($this->log_file_name);
  129. $this->assertContains('PHP Notice: message in ', $log);
  130. }
  131. /**
  132. * @runInSeparateProcess
  133. */
  134. public function testLogErrorErrorExceptionWarning()
  135. {
  136. $ex = new ErrorException('message', 321, E_WARNING);
  137. ini_set('error_log', $this->log_file_name);
  138. ErrorHandler::logError($ex);
  139. $log = file_get_contents($this->log_file_name);
  140. $this->assertContains('PHP Warning: message in ', $log);
  141. }
  142. /**
  143. * @runInSeparateProcess
  144. */
  145. public function testLogErrorErrorExceptionFatal()
  146. {
  147. $ex = new ErrorException('message', 321, E_ERROR);
  148. ini_set('error_log', $this->log_file_name);
  149. ErrorHandler::logError($ex);
  150. $log = file_get_contents($this->log_file_name);
  151. $this->assertContains('PHP Fatal Error: message in ', $log);
  152. }
  153. public function tearDown()
  154. {
  155. if (file_exists($this->log_file_name) && is_writable($this->log_file_name)) {
  156. unlink($this->log_file_name);
  157. }
  158. ini_set('error_log', 'php://stderr');
  159. set_error_handler($this->old_eh);
  160. }
  161. }