BrowserConsoleHandlerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  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\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. public function testStyling()
  23. {
  24. $handler = new BrowserConsoleHandler();
  25. $handler->setFormatter($this->getIdentityFormatter());
  26. $handler->handle($this->getRecord(Logger::DEBUG, 'foo[[bar]]{color: red}'));
  27. $expected = <<<EOF
  28. (function(c){if (c && c.groupCollapsed) {
  29. c.log("%cfoo%cbar%c", "font-weight: normal", "color: red", "font-weight: normal");
  30. }})(console);
  31. EOF;
  32. $this->assertEquals($expected, BrowserConsoleHandler::generateScript());
  33. }
  34. public function testEscaping()
  35. {
  36. $handler = new BrowserConsoleHandler();
  37. $handler->setFormatter($this->getIdentityFormatter());
  38. $handler->handle($this->getRecord(Logger::DEBUG, "[foo] [[\"bar\n[baz]\"]]{color: red}"));
  39. $expected = <<<EOF
  40. (function(c){if (c && c.groupCollapsed) {
  41. c.log("%c[foo] %c\"bar\\n[baz]\"%c", "font-weight: normal", "color: red", "font-weight: normal");
  42. }})(console);
  43. EOF;
  44. $this->assertEquals($expected, BrowserConsoleHandler::generateScript());
  45. }
  46. public function testAutolabel()
  47. {
  48. $handler = new BrowserConsoleHandler();
  49. $handler->setFormatter($this->getIdentityFormatter());
  50. $handler->handle($this->getRecord(Logger::DEBUG, '[[foo]]{macro: autolabel}'));
  51. $handler->handle($this->getRecord(Logger::DEBUG, '[[bar]]{macro: autolabel}'));
  52. $handler->handle($this->getRecord(Logger::DEBUG, '[[foo]]{macro: autolabel}'));
  53. $expected = <<<EOF
  54. (function(c){if (c && c.groupCollapsed) {
  55. c.log("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  56. c.log("%c%cbar%c", "font-weight: normal", "background-color: green; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  57. c.log("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  58. }})(console);
  59. EOF;
  60. $this->assertEquals($expected, BrowserConsoleHandler::generateScript());
  61. }
  62. public function testContext()
  63. {
  64. $handler = new BrowserConsoleHandler();
  65. $handler->setFormatter($this->getIdentityFormatter());
  66. $handler->handle($this->getRecord(Logger::DEBUG, 'test', array('foo' => 'bar')));
  67. $expected = <<<EOF
  68. (function(c){if (c && c.groupCollapsed) {
  69. c.groupCollapsed("%ctest", "font-weight: normal");
  70. c.log("%c%s", "font-weight: bold", "Context");
  71. c.log("%s: %o", "foo", "bar");
  72. c.groupEnd();
  73. }})(console);
  74. EOF;
  75. $this->assertEquals($expected, BrowserConsoleHandler::generateScript());
  76. }
  77. public function testConcurrentHandlers()
  78. {
  79. $handler1 = new BrowserConsoleHandler();
  80. $handler1->setFormatter($this->getIdentityFormatter());
  81. $handler2 = new BrowserConsoleHandler();
  82. $handler2->setFormatter($this->getIdentityFormatter());
  83. $handler1->handle($this->getRecord(Logger::DEBUG, 'test1'));
  84. $handler2->handle($this->getRecord(Logger::DEBUG, 'test2'));
  85. $handler1->handle($this->getRecord(Logger::DEBUG, 'test3'));
  86. $handler2->handle($this->getRecord(Logger::DEBUG, 'test4'));
  87. $expected = <<<EOF
  88. (function(c){if (c && c.groupCollapsed) {
  89. c.log("%ctest1", "font-weight: normal");
  90. c.log("%ctest2", "font-weight: normal");
  91. c.log("%ctest3", "font-weight: normal");
  92. c.log("%ctest4", "font-weight: normal");
  93. }})(console);
  94. EOF;
  95. $this->assertEquals($expected, BrowserConsoleHandler::generateScript());
  96. }
  97. }