BrowserConsoleHandlerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Logger;
  13. /**
  14. * @covers Monolog\Handler\BrowserConsoleHandlerTest
  15. */
  16. class BrowserConsoleHandlerTest extends TestCase
  17. {
  18. protected function setUp()
  19. {
  20. BrowserConsoleHandler::reset();
  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(Logger::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 testEscaping()
  41. {
  42. $handler = new BrowserConsoleHandler();
  43. $handler->setFormatter($this->getIdentityFormatter());
  44. $handler->handle($this->getRecord(Logger::DEBUG, "[foo] [[\"bar\n[baz]\"]]{color: red}"));
  45. $expected = <<<EOF
  46. (function (c) {if (c && c.groupCollapsed) {
  47. c.log("%c[foo] %c\"bar\\n[baz]\"%c", "font-weight: normal", "color: red", "font-weight: normal");
  48. }})(console);
  49. EOF;
  50. $this->assertEquals($expected, $this->generateScript());
  51. }
  52. public function testAutolabel()
  53. {
  54. $handler = new BrowserConsoleHandler();
  55. $handler->setFormatter($this->getIdentityFormatter());
  56. $handler->handle($this->getRecord(Logger::DEBUG, '[[foo]]{macro: autolabel}'));
  57. $handler->handle($this->getRecord(Logger::DEBUG, '[[bar]]{macro: autolabel}'));
  58. $handler->handle($this->getRecord(Logger::DEBUG, '[[foo]]{macro: autolabel}'));
  59. $expected = <<<EOF
  60. (function (c) {if (c && c.groupCollapsed) {
  61. c.log("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  62. c.log("%c%cbar%c", "font-weight: normal", "background-color: green; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  63. c.log("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  64. }})(console);
  65. EOF;
  66. $this->assertEquals($expected, $this->generateScript());
  67. }
  68. public function testContext()
  69. {
  70. $handler = new BrowserConsoleHandler();
  71. $handler->setFormatter($this->getIdentityFormatter());
  72. $handler->handle($this->getRecord(Logger::DEBUG, 'test', ['foo' => 'bar']));
  73. $expected = <<<EOF
  74. (function (c) {if (c && c.groupCollapsed) {
  75. c.groupCollapsed("%ctest", "font-weight: normal");
  76. c.log("%c%s", "font-weight: bold", "Context");
  77. c.log("%s: %o", "foo", "bar");
  78. c.groupEnd();
  79. }})(console);
  80. EOF;
  81. $this->assertEquals($expected, $this->generateScript());
  82. }
  83. public function testConcurrentHandlers()
  84. {
  85. $handler1 = new BrowserConsoleHandler();
  86. $handler1->setFormatter($this->getIdentityFormatter());
  87. $handler2 = new BrowserConsoleHandler();
  88. $handler2->setFormatter($this->getIdentityFormatter());
  89. $handler1->handle($this->getRecord(Logger::DEBUG, 'test1'));
  90. $handler2->handle($this->getRecord(Logger::DEBUG, 'test2'));
  91. $handler1->handle($this->getRecord(Logger::DEBUG, 'test3'));
  92. $handler2->handle($this->getRecord(Logger::DEBUG, 'test4'));
  93. $expected = <<<EOF
  94. (function (c) {if (c && c.groupCollapsed) {
  95. c.log("%ctest1", "font-weight: normal");
  96. c.log("%ctest2", "font-weight: normal");
  97. c.log("%ctest3", "font-weight: normal");
  98. c.log("%ctest4", "font-weight: normal");
  99. }})(console);
  100. EOF;
  101. $this->assertEquals($expected, $this->generateScript());
  102. }
  103. }