ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
RavenHandlerTest.php
Go to the documentation of this file.
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 
15 use Monolog\Logger;
17 
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 
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  $record = $this->getRecord(Logger::INFO, 'test', array('checksum' => $checksum, 'release' => $release));
96  $handler->handle($record);
97 
98  $this->assertEquals($checksum, $ravenClient->lastData['checksum']);
99  $this->assertEquals($release, $ravenClient->lastData['release']);
100  }
101 
102  public function testFingerprint()
103  {
104  $ravenClient = $this->getRavenClient();
105  $handler = $this->getHandler($ravenClient);
106 
107  $fingerprint = array('{{ default }}', 'other value');
108  $record = $this->getRecord(Logger::INFO, 'test', array('fingerprint' => $fingerprint));
109  $handler->handle($record);
110 
111  $this->assertEquals($fingerprint, $ravenClient->lastData['fingerprint']);
112  }
113 
114  public function testUserContext()
115  {
116  $ravenClient = $this->getRavenClient();
117  $handler = $this->getHandler($ravenClient);
118 
119  $recordWithNoContext = $this->getRecord(Logger::INFO, 'test with default user context');
120  // set user context 'externally'
121 
122  $user = array(
123  'id' => '123',
124  'email' => 'test@test.com',
125  );
126 
127  $recordWithContext = $this->getRecord(Logger::INFO, 'test', array('user' => $user));
128 
129  $ravenClient->user_context(array('id' => 'test_user_id'));
130  // handle context
131  $handler->handle($recordWithContext);
132  $this->assertEquals($user, $ravenClient->lastData['user']);
133 
134  // check to see if its reset
135  $handler->handle($recordWithNoContext);
136  $this->assertInternalType('array', $ravenClient->context->user);
137  $this->assertSame('test_user_id', $ravenClient->context->user['id']);
138 
139  // handle with null context
140  $ravenClient->user_context(null);
141  $handler->handle($recordWithContext);
142  $this->assertEquals($user, $ravenClient->lastData['user']);
143 
144  // check to see if its reset
145  $handler->handle($recordWithNoContext);
146  $this->assertNull($ravenClient->context->user);
147  }
148 
149  public function testException()
150  {
151  $ravenClient = $this->getRavenClient();
152  $handler = $this->getHandler($ravenClient);
153 
154  try {
156  } catch (\Exception $e) {
157  $record = $this->getRecord(Logger::ERROR, $e->getMessage(), array('exception' => $e));
158  $handler->handle($record);
159  }
160 
161  $this->assertEquals($record['message'], $ravenClient->lastData['message']);
162  }
163 
164  public function testHandleBatch()
165  {
166  $records = $this->getMultipleRecords();
167  $records[] = $this->getRecord(Logger::WARNING, 'warning');
168  $records[] = $this->getRecord(Logger::WARNING, 'warning');
169 
170  $logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
171  $logFormatter->expects($this->once())->method('formatBatch');
172 
173  $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
174  $formatter->expects($this->once())->method('format')->with($this->callback(function ($record) {
175  return $record['level'] == 400;
176  }));
177 
178  $handler = $this->getHandler($this->getRavenClient());
179  $handler->setBatchFormatter($logFormatter);
180  $handler->setFormatter($formatter);
181  $handler->handleBatch($records);
182  }
183 
185  {
186  $records = array(
187  $this->getRecord(Logger::DEBUG, 'debug message 1'),
188  $this->getRecord(Logger::DEBUG, 'debug message 2'),
189  $this->getRecord(Logger::INFO, 'information'),
190  );
191 
192  $handler = $this->getMock('Monolog\Handler\RavenHandler', null, array($this->getRavenClient()));
193  $handler->expects($this->never())->method('handle');
194  $handler->setLevel(Logger::ERROR);
195  $handler->handleBatch($records);
196  }
197 
198  public function testGetSetBatchFormatter()
199  {
200  $ravenClient = $this->getRavenClient();
201  $handler = $this->getHandler($ravenClient);
202 
203  $handler->setBatchFormatter($formatter = new LineFormatter());
204  $this->assertSame($formatter, $handler->getBatchFormatter());
205  }
206 
207  public function testRelease()
208  {
209  $ravenClient = $this->getRavenClient();
210  $handler = $this->getHandler($ravenClient);
211  $release = 'v42.42.42';
212  $handler->setRelease($release);
213  $record = $this->getRecord(Logger::INFO, 'test');
214  $handler->handle($record);
215  $this->assertEquals($release, $ravenClient->lastData['release']);
216 
217  $localRelease = 'v41.41.41';
218  $record = $this->getRecord(Logger::INFO, 'test', array('release' => $localRelease));
219  $handler->handle($record);
220  $this->assertEquals($localRelease, $ravenClient->lastData['release']);
221  }
222 
223  private function methodThatThrowsAnException()
224  {
225  throw new \Exception('This is an exception');
226  }
227 }
const DEBUG
Detailed debug information.
Definition: Logger.php:32
const ERROR
Runtime errors.
Definition: Logger.php:57
testConstruct()
Monolog::__construct
$records
Definition: simple_test.php:22
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
const DEBUG
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
Create styles array
The data for the language used.
Handler to send messages to a Sentry (https://github.com/getsentry/sentry) server using raven-php (ht...
Formats incoming records into a one-line string.
const INFO
Interesting events.
Definition: Logger.php:39