2
0

BrowserConsoleHandlerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /**
  13. * @covers Monolog\Handler\BrowserConsoleHandlerTest
  14. */
  15. class BrowserConsoleHandlerTest extends \Monolog\Test\MonologTestCase
  16. {
  17. protected function setUp(): void
  18. {
  19. BrowserConsoleHandler::resetStatic();
  20. }
  21. protected function generateScript()
  22. {
  23. $reflMethod = new \ReflectionMethod('Monolog\Handler\BrowserConsoleHandler', 'generateScript');
  24. return $reflMethod->invoke(null);
  25. }
  26. public function testStyling()
  27. {
  28. $handler = new BrowserConsoleHandler();
  29. $handler->setFormatter($this->getIdentityFormatter());
  30. $handler->handle($this->getRecord(Level::Debug, 'foo[[bar]]{color: red}'));
  31. $expected = <<<EOF
  32. (function (c) {if (c && c.groupCollapsed) {
  33. c.debug("%cfoo%cbar%c", "font-weight: normal", "color: red", "font-weight: normal");
  34. }})(console);
  35. EOF;
  36. $this->assertEquals($expected, $this->generateScript());
  37. }
  38. public function testStylingMultiple()
  39. {
  40. $handler = new BrowserConsoleHandler();
  41. $handler->setFormatter($this->getIdentityFormatter());
  42. $handler->handle($this->getRecord(Level::Debug, 'foo[[bar]]{color: red}[[baz]]{color: blue}'));
  43. $expected = <<<EOF
  44. (function (c) {if (c && c.groupCollapsed) {
  45. c.debug("%cfoo%cbar%c%cbaz%c", "font-weight: normal", "color: red", "font-weight: normal", "color: blue", "font-weight: normal");
  46. }})(console);
  47. EOF;
  48. $this->assertEquals($expected, $this->generateScript());
  49. }
  50. public function testEscaping()
  51. {
  52. $handler = new BrowserConsoleHandler();
  53. $handler->setFormatter($this->getIdentityFormatter());
  54. $handler->handle($this->getRecord(Level::Debug, "[foo] [[\"bar\n[baz]\"]]{color: red}"));
  55. $expected = <<<EOF
  56. (function (c) {if (c && c.groupCollapsed) {
  57. c.debug("%c[foo] %c\"bar\\n[baz]\"%c", "font-weight: normal", "color: red", "font-weight: normal");
  58. }})(console);
  59. EOF;
  60. $this->assertEquals($expected, $this->generateScript());
  61. }
  62. public function testAutolabel()
  63. {
  64. $handler = new BrowserConsoleHandler();
  65. $handler->setFormatter($this->getIdentityFormatter());
  66. $handler->handle($this->getRecord(Level::Debug, '[[foo]]{macro: autolabel}'));
  67. $handler->handle($this->getRecord(Level::Debug, '[[bar]]{macro: autolabel}'));
  68. $handler->handle($this->getRecord(Level::Debug, '[[foo]]{macro: autolabel}'));
  69. $expected = <<<EOF
  70. (function (c) {if (c && c.groupCollapsed) {
  71. c.debug("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  72. c.debug("%c%cbar%c", "font-weight: normal", "background-color: green; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  73. c.debug("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  74. }})(console);
  75. EOF;
  76. $this->assertEquals($expected, $this->generateScript());
  77. }
  78. public function testContext()
  79. {
  80. $handler = new BrowserConsoleHandler();
  81. $handler->setFormatter($this->getIdentityFormatter());
  82. $handler->handle($this->getRecord(Level::Debug, 'test', ['foo' => 'bar', 0 => 'oop']));
  83. $expected = <<<EOF
  84. (function (c) {if (c && c.groupCollapsed) {
  85. c.groupCollapsed("%ctest", "font-weight: normal");
  86. c.log("%c%s", "font-weight: bold", "Context");
  87. c.log("%s: %o", "foo", "bar");
  88. c.log("%s: %o", "0", "oop");
  89. c.groupEnd();
  90. }})(console);
  91. EOF;
  92. $this->assertEquals($expected, $this->generateScript());
  93. }
  94. public function testConcurrentHandlers()
  95. {
  96. $handler1 = new BrowserConsoleHandler();
  97. $handler1->setFormatter($this->getIdentityFormatter());
  98. $handler2 = new BrowserConsoleHandler();
  99. $handler2->setFormatter($this->getIdentityFormatter());
  100. $handler1->handle($this->getRecord(Level::Debug, 'test1'));
  101. $handler2->handle($this->getRecord(Level::Debug, 'test2'));
  102. $handler1->handle($this->getRecord(Level::Debug, 'test3'));
  103. $handler2->handle($this->getRecord(Level::Debug, 'test4'));
  104. $expected = <<<EOF
  105. (function (c) {if (c && c.groupCollapsed) {
  106. c.debug("%ctest1", "font-weight: normal");
  107. c.debug("%ctest2", "font-weight: normal");
  108. c.debug("%ctest3", "font-weight: normal");
  109. c.debug("%ctest4", "font-weight: normal");
  110. }})(console);
  111. EOF;
  112. $this->assertEquals($expected, $this->generateScript());
  113. }
  114. }