BrowserConsoleHandlerTest.php 4.7 KB

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