scratch – Blame information for rev 115

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 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\ChromePHPHandler
19 */
20 class ChromePHPHandlerTest extends TestCase
21 {
22 protected function setUp()
23 {
24 TestChromePHPHandler::reset();
25 $_SERVER['HTTP_USER_AGENT'] = 'Monolog Test; Chrome/1.0';
26 }
27  
28 /**
29 * @dataProvider agentsProvider
30 */
31 public function testHeaders($agent)
32 {
33 $_SERVER['HTTP_USER_AGENT'] = $agent;
34  
35 $handler = new TestChromePHPHandler();
36 $handler->setFormatter($this->getIdentityFormatter());
37 $handler->handle($this->getRecord(Logger::DEBUG));
38 $handler->handle($this->getRecord(Logger::WARNING));
39  
40 $expected = array(
41 'X-ChromeLogger-Data' => base64_encode(utf8_encode(json_encode(array(
42 'version' => ChromePHPHandler::VERSION,
43 'columns' => array('label', 'log', 'backtrace', 'type'),
44 'rows' => array(
45 'test',
46 'test',
47 ),
48 'request_uri' => '',
49 )))),
50 );
51  
52 $this->assertEquals($expected, $handler->getHeaders());
53 }
54  
55 public static function agentsProvider()
56 {
57 return array(
58 array('Monolog Test; Chrome/1.0'),
59 array('Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'),
60 array('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/56.0.2924.76 Chrome/56.0.2924.76 Safari/537.36'),
61 array('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome Safari/537.36'),
62 );
63 }
64  
65 public function testHeadersOverflow()
66 {
67 $handler = new TestChromePHPHandler();
68 $handler->handle($this->getRecord(Logger::DEBUG));
69 $handler->handle($this->getRecord(Logger::WARNING, str_repeat('a', 150 * 1024)));
70  
71 // overflow chrome headers limit
72 $handler->handle($this->getRecord(Logger::WARNING, str_repeat('a', 100 * 1024)));
73  
74 $expected = array(
75 'X-ChromeLogger-Data' => base64_encode(utf8_encode(json_encode(array(
76 'version' => ChromePHPHandler::VERSION,
77 'columns' => array('label', 'log', 'backtrace', 'type'),
78 'rows' => array(
79 array(
80 'test',
81 'test',
82 'unknown',
83 'log',
84 ),
85 array(
86 'test',
87 str_repeat('a', 150 * 1024),
88 'unknown',
89 'warn',
90 ),
91 array(
92 'monolog',
93 'Incomplete logs, chrome header size limit reached',
94 'unknown',
95 'warn',
96 ),
97 ),
98 'request_uri' => '',
99 )))),
100 );
101  
102 $this->assertEquals($expected, $handler->getHeaders());
103 }
104  
105 public function testConcurrentHandlers()
106 {
107 $handler = new TestChromePHPHandler();
108 $handler->setFormatter($this->getIdentityFormatter());
109 $handler->handle($this->getRecord(Logger::DEBUG));
110 $handler->handle($this->getRecord(Logger::WARNING));
111  
112 $handler2 = new TestChromePHPHandler();
113 $handler2->setFormatter($this->getIdentityFormatter());
114 $handler2->handle($this->getRecord(Logger::DEBUG));
115 $handler2->handle($this->getRecord(Logger::WARNING));
116  
117 $expected = array(
118 'X-ChromeLogger-Data' => base64_encode(utf8_encode(json_encode(array(
119 'version' => ChromePHPHandler::VERSION,
120 'columns' => array('label', 'log', 'backtrace', 'type'),
121 'rows' => array(
122 'test',
123 'test',
124 'test',
125 'test',
126 ),
127 'request_uri' => '',
128 )))),
129 );
130  
131 $this->assertEquals($expected, $handler2->getHeaders());
132 }
133 }
134  
135 class TestChromePHPHandler extends ChromePHPHandler
136 {
137 protected $headers = array();
138  
139 public static function reset()
140 {
141 self::$initialized = false;
142 self::$overflowed = false;
143 self::$sendHeaders = true;
144 self::$json['rows'] = array();
145 }
146  
147 protected function sendHeader($header, $content)
148 {
149 $this->headers[$header] = $content;
150 }
151  
152 public function getHeaders()
153 {
154 return $this->headers;
155 }
156 }