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.

92 lines
2.9 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. public function setUp()
  18. {
  19. set_error_handler(array('ErrorHandler', 'error_handler'));
  20. ob_start();
  21. }
  22. public function testErrorHandlerInit()
  23. {
  24. $my_eh = array('ErrorHandler', 'error_handler');
  25. ErrorHandler::init();
  26. $eh = set_error_handler($my_eh);
  27. $this->assertInternalType('array', $eh);
  28. $this->assertSame($eh, $my_eh);
  29. }
  30. public function testHandleError()
  31. {
  32. $this->setExpectedException('ErrorException', 'test error');
  33. trigger_error("test error", E_USER_ERROR);
  34. }
  35. public function testHandleAt()
  36. {
  37. $my_eh = array('ErrorHandler', 'error_handler');
  38. ErrorHandler::init();
  39. set_error_handler($my_eh);
  40. $var = '';
  41. $ok = @$var['some'];
  42. $this->assertSame('', $var);
  43. ob_start();
  44. $this->setExpectedException('ErrorException');
  45. $ex = $var['some'];
  46. }
  47. /**
  48. * @TODO: ErrorHandler->wrapTrace() not used
  49. */
  50. public function testShowDebug()
  51. {
  52. try {
  53. throw new ErrorException("test error", E_USER_ERROR);
  54. } catch (ErrorException $e) {
  55. $_SESSION['some'] = 'value';
  56. $result = ErrorHandler::showDebug($e);
  57. $this->assertNotEmpty($result);
  58. $this->assertStringStartsWith('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', $result);
  59. $this->assertStringEndsWith('</html>', $result);
  60. }
  61. }
  62. /**
  63. * @TODO: ErrorHandler::wrapTrace not used
  64. * @TODO: nl2br() adds html <br /> but leaves original linebreak line \n
  65. */
  66. public function testWrapTrace()
  67. {
  68. $class = new ReflectionClass('ErrorHandler');
  69. $method = $class->getMethod('WrapTrace');
  70. $method->setAccessible(true);
  71. $result = $method->invoke(null, "first line\nsecond line");
  72. $this->assertSame("<code>first line<br />\nsecond line</code>", $result);
  73. $result = $method->invoke(null, "first line\r\nsecond line");
  74. $this->assertSame("<code>first line<br />\r\nsecond line</code>", $result);
  75. $result = $method->invoke(null, "first line\r\n\r\nsecond line");
  76. $this->assertSame("<code>first line<br />\r\n<br />\r\nsecond line</code>", $result);
  77. }
  78. public function tearDown()
  79. {
  80. set_error_handler($this->old_eh);
  81. }
  82. }