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.

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