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.

82 lines
2.6 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->assertEquals($eh, $my_eh);
  29. }
  30. /**
  31. * @expectedException ErrorException
  32. * @expectedExceptionMessage test error
  33. */
  34. public function testHandleError()
  35. {
  36. trigger_error("test error", E_USER_ERROR);
  37. }
  38. /**
  39. * @TODO: ErrorHandler->wrapTrace() not used
  40. */
  41. public function testShowDebug()
  42. {
  43. try {
  44. throw new ErrorException("test error", E_USER_ERROR);
  45. } catch (ErrorException $e) {
  46. $_SESSION['some'] = 'value';
  47. $result = ErrorHandler::showDebug($e);
  48. $this->assertNotEmpty($result);
  49. $this->assertStringStartsWith('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', $result);
  50. $this->assertStringEndsWith('</html>', $result);
  51. }
  52. }
  53. /**
  54. * @TODO: ErrorHandler::wrapTrace not used
  55. * @TODO: nl2br() adds html <br /> but leaves original linebreak line \n
  56. */
  57. public function testWrapTrace()
  58. {
  59. $class = new ReflectionClass('ErrorHandler');
  60. $method = $class->getMethod('WrapTrace');
  61. $method->setAccessible(true);
  62. $result = $method->invoke(null, "first line\nsecond line");
  63. $this->assertEquals("<code>first line<br />\nsecond line</code>", $result);
  64. $result = $method->invoke(null, "first line\r\nsecond line");
  65. $this->assertEquals("<code>first line<br />\r\nsecond line</code>", $result);
  66. $result = $method->invoke(null, "first line\r\n\r\nsecond line");
  67. $this->assertEquals("<code>first line<br />\r\n<br />\r\nsecond line</code>", $result);
  68. }
  69. public function tearDown()
  70. {
  71. set_error_handler($this->old_eh);
  72. }
  73. }