ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 testUserContext()
89  {
90  $ravenClient = $this->getRavenClient();
91  $handler = $this->getHandler($ravenClient);
92 
93  $recordWithNoContext = $this->getRecord(Logger::INFO, 'test with default user context');
94  // set user context 'externally'
95 
96  $user = array(
97  'id' => '123',
98  'email' => 'test@test.com'
99  );
100 
101  $recordWithContext = $this->getRecord(Logger::INFO, 'test', array('user' => $user));
102 
103  $ravenClient->user_context(array('id' => 'test_user_id'));
104  // handle context
105  $handler->handle($recordWithContext);
106  $this->assertEquals($user, $ravenClient->lastData['sentry.interfaces.User']);
107 
108  // check to see if its reset
109  $handler->handle($recordWithNoContext);
110  $this->assertInternalType('array', $ravenClient->context->user);
111  $this->assertSame('test_user_id', $ravenClient->context->user['id']);
112 
113  // handle with null context
114  $ravenClient->user_context(null);
115  $handler->handle($recordWithContext);
116  $this->assertEquals($user, $ravenClient->lastData['sentry.interfaces.User']);
117 
118  // check to see if its reset
119  $handler->handle($recordWithNoContext);
120  $this->assertNull($ravenClient->context->user);
121  }
122 
123  public function testException()
124  {
125  $ravenClient = $this->getRavenClient();
126  $handler = $this->getHandler($ravenClient);
127 
128  try {
130  } catch (\Exception $e) {
131  $record = $this->getRecord(Logger::ERROR, $e->getMessage(), array('exception' => $e));
132  $handler->handle($record);
133  }
134 
135  $this->assertEquals($record['message'], $ravenClient->lastData['message']);
136  }
137 
138  public function testHandleBatch()
139  {
140  $records = $this->getMultipleRecords();
141  $records[] = $this->getRecord(Logger::WARNING, 'warning');
142  $records[] = $this->getRecord(Logger::WARNING, 'warning');
143 
144  $logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
145  $logFormatter->expects($this->once())->method('formatBatch');
146 
147  $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
148  $formatter->expects($this->once())->method('format')->with($this->callback(function ($record) {
149  return $record['level'] == 400;
150  }));
151 
152  $handler = $this->getHandler($this->getRavenClient());
153  $handler->setBatchFormatter($logFormatter);
154  $handler->setFormatter($formatter);
155  $handler->handleBatch($records);
156  }
157 
159  {
160  $records = array(
161  $this->getRecord(Logger::DEBUG, 'debug message 1'),
162  $this->getRecord(Logger::DEBUG, 'debug message 2'),
163  $this->getRecord(Logger::INFO, 'information'),
164  );
165 
166  $handler = $this->getMock('Monolog\Handler\RavenHandler', null, array($this->getRavenClient()));
167  $handler->expects($this->never())->method('handle');
168  $handler->setLevel(Logger::ERROR);
169  $handler->handleBatch($records);
170  }
171 
172  public function testGetSetBatchFormatter()
173  {
174  $ravenClient = $this->getRavenClient();
175  $handler = $this->getHandler($ravenClient);
176 
177  $handler->setBatchFormatter($formatter = new LineFormatter());
178  $this->assertSame($formatter, $handler->getBatchFormatter());
179  }
180 
181  private function methodThatThrowsAnException()
182  {
183  throw new \Exception('This is an exception');
184  }
185 }
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:17
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
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