]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - vendor/monolog/monolog/tests/Monolog/Handler/BrowserConsoleHandlerTest.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / vendor / monolog / monolog / tests / Monolog / Handler / BrowserConsoleHandlerTest.php
1 <?php
2
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Monolog\Handler;
13
14 use Monolog\TestCase;
15 use Monolog\Logger;
16
17 /**
18  * @covers Monolog\Handler\BrowserConsoleHandlerTest
19  */
20 class BrowserConsoleHandlerTest extends TestCase
21 {
22     protected function setUp()
23     {
24         BrowserConsoleHandler::reset();
25     }
26
27     protected function generateScript()
28     {
29         $reflMethod = new \ReflectionMethod('Monolog\Handler\BrowserConsoleHandler', 'generateScript');
30         $reflMethod->setAccessible(true);
31
32         return $reflMethod->invoke(null);
33     }
34
35     public function testStyling()
36     {
37         $handler = new BrowserConsoleHandler();
38         $handler->setFormatter($this->getIdentityFormatter());
39
40         $handler->handle($this->getRecord(Logger::DEBUG, 'foo[[bar]]{color: red}'));
41
42         $expected = <<<EOF
43 (function (c) {if (c && c.groupCollapsed) {
44 c.log("%cfoo%cbar%c", "font-weight: normal", "color: red", "font-weight: normal");
45 }})(console);
46 EOF;
47
48         $this->assertEquals($expected, $this->generateScript());
49     }
50
51     public function testEscaping()
52     {
53         $handler = new BrowserConsoleHandler();
54         $handler->setFormatter($this->getIdentityFormatter());
55
56         $handler->handle($this->getRecord(Logger::DEBUG, "[foo] [[\"bar\n[baz]\"]]{color: red}"));
57
58         $expected = <<<EOF
59 (function (c) {if (c && c.groupCollapsed) {
60 c.log("%c[foo] %c\"bar\\n[baz]\"%c", "font-weight: normal", "color: red", "font-weight: normal");
61 }})(console);
62 EOF;
63
64         $this->assertEquals($expected, $this->generateScript());
65     }
66
67     public function testAutolabel()
68     {
69         $handler = new BrowserConsoleHandler();
70         $handler->setFormatter($this->getIdentityFormatter());
71
72         $handler->handle($this->getRecord(Logger::DEBUG, '[[foo]]{macro: autolabel}'));
73         $handler->handle($this->getRecord(Logger::DEBUG, '[[bar]]{macro: autolabel}'));
74         $handler->handle($this->getRecord(Logger::DEBUG, '[[foo]]{macro: autolabel}'));
75
76         $expected = <<<EOF
77 (function (c) {if (c && c.groupCollapsed) {
78 c.log("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
79 c.log("%c%cbar%c", "font-weight: normal", "background-color: green; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
80 c.log("%c%cfoo%c", "font-weight: normal", "background-color: blue; color: white; border-radius: 3px; padding: 0 2px 0 2px", "font-weight: normal");
81 }})(console);
82 EOF;
83
84         $this->assertEquals($expected, $this->generateScript());
85     }
86
87     public function testContext()
88     {
89         $handler = new BrowserConsoleHandler();
90         $handler->setFormatter($this->getIdentityFormatter());
91
92         $handler->handle($this->getRecord(Logger::DEBUG, 'test', array('foo' => 'bar')));
93
94         $expected = <<<EOF
95 (function (c) {if (c && c.groupCollapsed) {
96 c.groupCollapsed("%ctest", "font-weight: normal");
97 c.log("%c%s", "font-weight: bold", "Context");
98 c.log("%s: %o", "foo", "bar");
99 c.groupEnd();
100 }})(console);
101 EOF;
102
103         $this->assertEquals($expected, $this->generateScript());
104     }
105
106     public function testConcurrentHandlers()
107     {
108         $handler1 = new BrowserConsoleHandler();
109         $handler1->setFormatter($this->getIdentityFormatter());
110
111         $handler2 = new BrowserConsoleHandler();
112         $handler2->setFormatter($this->getIdentityFormatter());
113
114         $handler1->handle($this->getRecord(Logger::DEBUG, 'test1'));
115         $handler2->handle($this->getRecord(Logger::DEBUG, 'test2'));
116         $handler1->handle($this->getRecord(Logger::DEBUG, 'test3'));
117         $handler2->handle($this->getRecord(Logger::DEBUG, 'test4'));
118
119         $expected = <<<EOF
120 (function (c) {if (c && c.groupCollapsed) {
121 c.log("%ctest1", "font-weight: normal");
122 c.log("%ctest2", "font-weight: normal");
123 c.log("%ctest3", "font-weight: normal");
124 c.log("%ctest4", "font-weight: normal");
125 }})(console);
126 EOF;
127
128         $this->assertEquals($expected, $this->generateScript());
129     }
130 }