BrowserConsoleHandlerTest.php 4.7 KB

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