ChromePHPHandlerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php declare(strict_types=1);
  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\Level;
  12. use PHPUnit\Framework\Attributes\DataProvider;
  13. /**
  14. * @covers Monolog\Handler\ChromePHPHandler
  15. */
  16. class ChromePHPHandlerTest extends \Monolog\Test\MonologTestCase
  17. {
  18. protected function setUp(): void
  19. {
  20. TestChromePHPHandler::resetStatic();
  21. $_SERVER['HTTP_USER_AGENT'] = 'Monolog Test; Chrome/1.0';
  22. }
  23. #[DataProvider('agentsProvider')]
  24. public function testHeaders($agent)
  25. {
  26. $_SERVER['HTTP_USER_AGENT'] = $agent;
  27. $handler = new TestChromePHPHandler();
  28. $handler->setFormatter($this->getIdentityFormatter());
  29. $handler->handle($this->getRecord(Level::Debug));
  30. $handler->handle($this->getRecord(Level::Warning));
  31. $expected = [
  32. 'X-ChromeLogger-Data' => base64_encode(json_encode([
  33. 'version' => '4.0',
  34. 'columns' => ['label', 'log', 'backtrace', 'type'],
  35. 'rows' => [
  36. 'test',
  37. 'test',
  38. ],
  39. 'request_uri' => '',
  40. ])),
  41. ];
  42. $this->assertEquals($expected, $handler->getHeaders());
  43. }
  44. public static function agentsProvider()
  45. {
  46. return [
  47. ['Monolog Test; Chrome/1.0'],
  48. ['Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'],
  49. ['Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/56.0.2924.76 Chrome/56.0.2924.76 Safari/537.36'],
  50. ['Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome Safari/537.36'],
  51. ];
  52. }
  53. public function testHeadersOverflow()
  54. {
  55. $handler = new TestChromePHPHandler();
  56. $handler->handle($this->getRecord(Level::Debug));
  57. $handler->handle($this->getRecord(Level::Warning, str_repeat('a', 2 * 1024)));
  58. // overflow chrome headers limit
  59. $handler->handle($this->getRecord(Level::Warning, str_repeat('b', 2 * 1024)));
  60. $expected = [
  61. 'X-ChromeLogger-Data' => base64_encode(json_encode([
  62. 'version' => '4.0',
  63. 'columns' => ['label', 'log', 'backtrace', 'type'],
  64. 'rows' => [
  65. [
  66. 'test',
  67. 'test',
  68. 'unknown',
  69. 'log',
  70. ],
  71. [
  72. 'test',
  73. str_repeat('a', 2 * 1024),
  74. 'unknown',
  75. 'warn',
  76. ],
  77. [
  78. 'monolog',
  79. 'Incomplete logs, chrome header size limit reached',
  80. 'unknown',
  81. 'warn',
  82. ],
  83. ],
  84. 'request_uri' => '',
  85. ])),
  86. ];
  87. $this->assertEquals($expected, $handler->getHeaders());
  88. }
  89. public function testConcurrentHandlers()
  90. {
  91. $handler = new TestChromePHPHandler();
  92. $handler->setFormatter($this->getIdentityFormatter());
  93. $handler->handle($this->getRecord(Level::Debug));
  94. $handler->handle($this->getRecord(Level::Warning));
  95. $handler2 = new TestChromePHPHandler();
  96. $handler2->setFormatter($this->getIdentityFormatter());
  97. $handler2->handle($this->getRecord(Level::Debug));
  98. $handler2->handle($this->getRecord(Level::Warning));
  99. $expected = [
  100. 'X-ChromeLogger-Data' => base64_encode(json_encode([
  101. 'version' => '4.0',
  102. 'columns' => ['label', 'log', 'backtrace', 'type'],
  103. 'rows' => [
  104. 'test',
  105. 'test',
  106. 'test',
  107. 'test',
  108. ],
  109. 'request_uri' => '',
  110. ])),
  111. ];
  112. $this->assertEquals($expected, $handler2->getHeaders());
  113. }
  114. }
  115. class TestChromePHPHandler extends ChromePHPHandler
  116. {
  117. protected array $headers = [];
  118. public static function resetStatic(): void
  119. {
  120. self::$initialized = false;
  121. self::$overflowed = false;
  122. self::$sendHeaders = true;
  123. self::$json['rows'] = [];
  124. }
  125. protected function sendHeader(string $header, string $content): void
  126. {
  127. $this->headers[$header] = $content;
  128. }
  129. public function getHeaders(): array
  130. {
  131. return $this->headers;
  132. }
  133. protected function isWebRequest(): bool
  134. {
  135. return true;
  136. }
  137. }