BrowserConsoleHandlerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 (console && console.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 testAutolabel()
  35. {
  36. $handler = new BrowserConsoleHandler();
  37. $handler->setFormatter($this->getIdentityFormatter());
  38. $handler->handle($this->getRecord(Logger::DEBUG, '[foo]{macro: autolabel}'));
  39. $handler->handle($this->getRecord(Logger::DEBUG, '[bar]{macro: autolabel}'));
  40. $handler->handle($this->getRecord(Logger::DEBUG, '[foo]{macro: autolabel}'));
  41. $expected = <<<EOF
  42. (function(c){if (console && console.groupCollapsed) {
  43. c.log("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  44. c.log("%c%cbar%c", "font-weight: normal", "background-color: green; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  45. c.log("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
  46. }})(console);
  47. EOF;
  48. $this->assertEquals($expected, BrowserConsoleHandler::generateScript());
  49. }
  50. public function testContext()
  51. {
  52. $handler = new BrowserConsoleHandler();
  53. $handler->setFormatter($this->getIdentityFormatter());
  54. $handler->handle($this->getRecord(Logger::DEBUG, 'test', array('foo' => 'bar')));
  55. $expected = <<<EOF
  56. (function(c){if (console && console.groupCollapsed) {
  57. c.groupCollapsed("%ctest", "font-weight: normal");
  58. c.log("%c%s", "font-weight: bold", "Context");
  59. c.log("%s: %o", "foo", "bar");
  60. c.groupEnd();
  61. }})(console);
  62. EOF;
  63. $this->assertEquals($expected, BrowserConsoleHandler::generateScript());
  64. }
  65. public function testConcurrentHandlers()
  66. {
  67. $handler1 = new BrowserConsoleHandler();
  68. $handler1->setFormatter($this->getIdentityFormatter());
  69. $handler2 = new BrowserConsoleHandler();
  70. $handler2->setFormatter($this->getIdentityFormatter());
  71. $handler1->handle($this->getRecord(Logger::DEBUG, 'test1'));
  72. $handler2->handle($this->getRecord(Logger::DEBUG, 'test2'));
  73. $handler1->handle($this->getRecord(Logger::DEBUG, 'test3'));
  74. $handler2->handle($this->getRecord(Logger::DEBUG, 'test4'));
  75. $expected = <<<EOF
  76. (function(c){if (console && console.groupCollapsed) {
  77. c.log("%ctest1", "font-weight: normal");
  78. c.log("%ctest2", "font-weight: normal");
  79. c.log("%ctest3", "font-weight: normal");
  80. c.log("%ctest4", "font-weight: normal");
  81. }})(console);
  82. EOF;
  83. $this->assertEquals($expected, BrowserConsoleHandler::generateScript());
  84. }
  85. }