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 use Monolog\Formatter\LineFormatter;
17  
18 class RavenHandlerTest extends TestCase
19 {
20 public function setUp()
21 {
22 if (!class_exists('Raven_Client')) {
23 $this->markTestSkipped('raven/raven not installed');
24 }
25  
26 require_once __DIR__ . '/MockRavenClient.php';
27 }
28  
29 /**
30 * @covers Monolog\Handler\RavenHandler::__construct
31 */
32 public function testConstruct()
33 {
34 $handler = new RavenHandler($this->getRavenClient());
35 $this->assertInstanceOf('Monolog\Handler\RavenHandler', $handler);
36 }
37  
38 protected function getHandler($ravenClient)
39 {
40 $handler = new RavenHandler($ravenClient);
41  
42 return $handler;
43 }
44  
45 protected function getRavenClient()
46 {
47 $dsn = 'http://43f6017361224d098402974103bfc53d:a6a0538fc2934ba2bed32e08741b2cd3@marca.python.live.cheggnet.com:9000/1';
48  
49 return new MockRavenClient($dsn);
50 }
51  
52 public function testDebug()
53 {
54 $ravenClient = $this->getRavenClient();
55 $handler = $this->getHandler($ravenClient);
56  
57 $record = $this->getRecord(Logger::DEBUG, 'A test debug message');
58 $handler->handle($record);
59  
60 $this->assertEquals($ravenClient::DEBUG, $ravenClient->lastData['level']);
61 $this->assertContains($record['message'], $ravenClient->lastData['message']);
62 }
63  
64 public function testWarning()
65 {
66 $ravenClient = $this->getRavenClient();
67 $handler = $this->getHandler($ravenClient);
68  
69 $record = $this->getRecord(Logger::WARNING, 'A test warning message');
70 $handler->handle($record);
71  
72 $this->assertEquals($ravenClient::WARNING, $ravenClient->lastData['level']);
73 $this->assertContains($record['message'], $ravenClient->lastData['message']);
74 }
75  
76 public function testTag()
77 {
78 $ravenClient = $this->getRavenClient();
79 $handler = $this->getHandler($ravenClient);
80  
81 $tags = array(1, 2, 'foo');
82 $record = $this->getRecord(Logger::INFO, 'test', array('tags' => $tags));
83 $handler->handle($record);
84  
85 $this->assertEquals($tags, $ravenClient->lastData['tags']);
86 }
87  
88 public function testExtraParameters()
89 {
90 $ravenClient = $this->getRavenClient();
91 $handler = $this->getHandler($ravenClient);
92  
93 $checksum = '098f6bcd4621d373cade4e832627b4f6';
94 $release = '05a671c66aefea124cc08b76ea6d30bb';
95 $eventId = '31423';
96 $record = $this->getRecord(Logger::INFO, 'test', array('checksum' => $checksum, 'release' => $release, 'event_id' => $eventId));
97 $handler->handle($record);
98  
99 $this->assertEquals($checksum, $ravenClient->lastData['checksum']);
100 $this->assertEquals($release, $ravenClient->lastData['release']);
101 $this->assertEquals($eventId, $ravenClient->lastData['event_id']);
102 }
103  
104 public function testFingerprint()
105 {
106 $ravenClient = $this->getRavenClient();
107 $handler = $this->getHandler($ravenClient);
108  
109 $fingerprint = array('{{ default }}', 'other value');
110 $record = $this->getRecord(Logger::INFO, 'test', array('fingerprint' => $fingerprint));
111 $handler->handle($record);
112  
113 $this->assertEquals($fingerprint, $ravenClient->lastData['fingerprint']);
114 }
115  
116 public function testUserContext()
117 {
118 $ravenClient = $this->getRavenClient();
119 $handler = $this->getHandler($ravenClient);
120  
121 $recordWithNoContext = $this->getRecord(Logger::INFO, 'test with default user context');
122 // set user context 'externally'
123  
124 $user = array(
125 'id' => '123',
126 'email' => 'test@test.com',
127 );
128  
129 $recordWithContext = $this->getRecord(Logger::INFO, 'test', array('user' => $user));
130  
131 $ravenClient->user_context(array('id' => 'test_user_id'));
132 // handle context
133 $handler->handle($recordWithContext);
134 $this->assertEquals($user, $ravenClient->lastData['user']);
135  
136 // check to see if its reset
137 $handler->handle($recordWithNoContext);
138 $this->assertInternalType('array', $ravenClient->context->user);
139 $this->assertSame('test_user_id', $ravenClient->context->user['id']);
140  
141 // handle with null context
142 $ravenClient->user_context(null);
143 $handler->handle($recordWithContext);
144 $this->assertEquals($user, $ravenClient->lastData['user']);
145  
146 // check to see if its reset
147 $handler->handle($recordWithNoContext);
148 $this->assertNull($ravenClient->context->user);
149 }
150  
151 public function testException()
152 {
153 $ravenClient = $this->getRavenClient();
154 $handler = $this->getHandler($ravenClient);
155  
156 try {
157 $this->methodThatThrowsAnException();
158 } catch (\Exception $e) {
159 $record = $this->getRecord(Logger::ERROR, $e->getMessage(), array('exception' => $e));
160 $handler->handle($record);
161 }
162  
163 $this->assertEquals($record['message'], $ravenClient->lastData['message']);
164 }
165  
166 public function testHandleBatch()
167 {
168 $records = $this->getMultipleRecords();
169 $records[] = $this->getRecord(Logger::WARNING, 'warning');
170 $records[] = $this->getRecord(Logger::WARNING, 'warning');
171  
172 $logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
173 $logFormatter->expects($this->once())->method('formatBatch');
174  
175 $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
176 $formatter->expects($this->once())->method('format')->with($this->callback(function ($record) {
177 return $record['level'] == 400;
178 }));
179  
180 $handler = $this->getHandler($this->getRavenClient());
181 $handler->setBatchFormatter($logFormatter);
182 $handler->setFormatter($formatter);
183 $handler->handleBatch($records);
184 }
185  
186 public function testHandleBatchDoNothingIfRecordsAreBelowLevel()
187 {
188 $records = array(
189 $this->getRecord(Logger::DEBUG, 'debug message 1'),
190 $this->getRecord(Logger::DEBUG, 'debug message 2'),
191 $this->getRecord(Logger::INFO, 'information'),
192 );
193  
194 $handler = $this->getMock('Monolog\Handler\RavenHandler', null, array($this->getRavenClient()));
195 $handler->expects($this->never())->method('handle');
196 $handler->setLevel(Logger::ERROR);
197 $handler->handleBatch($records);
198 }
199  
200 public function testHandleBatchPicksProperMessage()
201 {
202 $records = array(
203 $this->getRecord(Logger::DEBUG, 'debug message 1'),
204 $this->getRecord(Logger::DEBUG, 'debug message 2'),
205 $this->getRecord(Logger::INFO, 'information 1'),
206 $this->getRecord(Logger::ERROR, 'error 1'),
207 $this->getRecord(Logger::WARNING, 'warning'),
208 $this->getRecord(Logger::ERROR, 'error 2'),
209 $this->getRecord(Logger::INFO, 'information 2'),
210 );
211  
212 $logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
213 $logFormatter->expects($this->once())->method('formatBatch');
214  
215 $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
216 $formatter->expects($this->once())->method('format')->with($this->callback(function ($record) use ($records) {
217 return $record['message'] == 'error 1';
218 }));
219  
220 $handler = $this->getHandler($this->getRavenClient());
221 $handler->setBatchFormatter($logFormatter);
222 $handler->setFormatter($formatter);
223 $handler->handleBatch($records);
224 }
225  
226 public function testGetSetBatchFormatter()
227 {
228 $ravenClient = $this->getRavenClient();
229 $handler = $this->getHandler($ravenClient);
230  
231 $handler->setBatchFormatter($formatter = new LineFormatter());
232 $this->assertSame($formatter, $handler->getBatchFormatter());
233 }
234  
235 public function testRelease()
236 {
237 $ravenClient = $this->getRavenClient();
238 $handler = $this->getHandler($ravenClient);
239 $release = 'v42.42.42';
240 $handler->setRelease($release);
241 $record = $this->getRecord(Logger::INFO, 'test');
242 $handler->handle($record);
243 $this->assertEquals($release, $ravenClient->lastData['release']);
244  
245 $localRelease = 'v41.41.41';
246 $record = $this->getRecord(Logger::INFO, 'test', array('release' => $localRelease));
247 $handler->handle($record);
248 $this->assertEquals($localRelease, $ravenClient->lastData['release']);
249 }
250  
251 private function methodThatThrowsAnException()
252 {
253 throw new \Exception('This is an exception');
254 }
255 }