2
0

ChromePHPHandlerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\TestCase;
  12. use Monolog\Logger;
  13. /**
  14. * @covers Monolog\Handler\ChromePHPHandler
  15. */
  16. class ChromePHPHandlerTest extends TestCase
  17. {
  18. protected function setUp()
  19. {
  20. TestChromePHPHandler::reset();
  21. $_SERVER['HTTP_USER_AGENT'] = 'Monolog Test; Chrome/1.0';
  22. }
  23. public function testHeaders()
  24. {
  25. $handler = new TestChromePHPHandler();
  26. $handler->setFormatter($this->getIdentityFormatter());
  27. $handler->handle($this->getRecord(Logger::DEBUG));
  28. $handler->handle($this->getRecord(Logger::WARNING));
  29. $expected = array(
  30. 'X-ChromeLogger-Data' => base64_encode(utf8_encode(json_encode(array(
  31. 'version' => ChromePHPHandler::VERSION,
  32. 'columns' => array('label', 'log', 'backtrace', 'type'),
  33. 'rows' => array(
  34. 'test',
  35. 'test',
  36. ),
  37. 'request_uri' => '',
  38. )))),
  39. );
  40. $this->assertEquals($expected, $handler->getHeaders());
  41. }
  42. public function testHeadersOverflow()
  43. {
  44. $handler = new TestChromePHPHandler();
  45. $handler->handle($this->getRecord(Logger::DEBUG));
  46. $handler->handle($this->getRecord(Logger::WARNING, str_repeat('a', 150 * 1024)));
  47. // overflow chrome headers limit
  48. $handler->handle($this->getRecord(Logger::WARNING, str_repeat('a', 100 * 1024)));
  49. $expected = array(
  50. 'X-ChromeLogger-Data' => base64_encode(utf8_encode(json_encode(array(
  51. 'version' => ChromePHPHandler::VERSION,
  52. 'columns' => array('label', 'log', 'backtrace', 'type'),
  53. 'rows' => array(
  54. array(
  55. 'test',
  56. 'test',
  57. 'unknown',
  58. 'log',
  59. ),
  60. array(
  61. 'test',
  62. str_repeat('a', 150 * 1024),
  63. 'unknown',
  64. 'warn',
  65. ),
  66. array(
  67. 'monolog',
  68. 'Incomplete logs, chrome header size limit reached',
  69. 'unknown',
  70. 'warn',
  71. ),
  72. ),
  73. 'request_uri' => '',
  74. )))),
  75. );
  76. $this->assertEquals($expected, $handler->getHeaders());
  77. }
  78. public function testConcurrentHandlers()
  79. {
  80. $handler = new TestChromePHPHandler();
  81. $handler->setFormatter($this->getIdentityFormatter());
  82. $handler->handle($this->getRecord(Logger::DEBUG));
  83. $handler->handle($this->getRecord(Logger::WARNING));
  84. $handler2 = new TestChromePHPHandler();
  85. $handler2->setFormatter($this->getIdentityFormatter());
  86. $handler2->handle($this->getRecord(Logger::DEBUG));
  87. $handler2->handle($this->getRecord(Logger::WARNING));
  88. $expected = array(
  89. 'X-ChromeLogger-Data' => base64_encode(utf8_encode(json_encode(array(
  90. 'version' => ChromePHPHandler::VERSION,
  91. 'columns' => array('label', 'log', 'backtrace', 'type'),
  92. 'rows' => array(
  93. 'test',
  94. 'test',
  95. 'test',
  96. 'test',
  97. ),
  98. 'request_uri' => '',
  99. )))),
  100. );
  101. $this->assertEquals($expected, $handler2->getHeaders());
  102. }
  103. }
  104. class TestChromePHPHandler extends ChromePHPHandler
  105. {
  106. protected $headers = array();
  107. public static function reset()
  108. {
  109. self::$initialized = false;
  110. self::$overflowed = false;
  111. self::$sendHeaders = true;
  112. self::$json['rows'] = array();
  113. }
  114. protected function sendHeader($header, $content)
  115. {
  116. $this->headers[$header] = $content;
  117. }
  118. public function getHeaders()
  119. {
  120. return $this->headers;
  121. }
  122. }