ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SlackRecordTest.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
12namespace Monolog\Handler\Slack;
13
16
21{
23
24 protected function setUp()
25 {
26 $this->jsonPrettyPrintFlag = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 128;
27 }
28
29 public function dataGetAttachmentColor()
30 {
31 return array(
40 );
41 }
42
49 public function testGetAttachmentColor($logLevel, $expectedColour)
50 {
51 $slackRecord = new SlackRecord();
52 $this->assertSame(
53 $expectedColour,
54 $slackRecord->getAttachmentColor($logLevel)
55 );
56 }
57
58 public function testAddsChannel()
59 {
60 $channel = '#test';
61 $record = new SlackRecord($channel);
62 $data = $record->getSlackData($this->getRecord());
63
64 $this->assertArrayHasKey('channel', $data);
65 $this->assertSame($channel, $data['channel']);
66 }
67
68 public function testNoUsernameByDefault()
69 {
70 $record = new SlackRecord();
71 $data = $record->getSlackData($this->getRecord());
72
73 $this->assertArrayNotHasKey('username', $data);
74 }
75
79 public function dataStringify()
80 {
81 $jsonPrettyPrintFlag = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 128;
82
83 $multipleDimensions = array(array(1, 2));
84 $numericKeys = array('library' => 'monolog');
85 $singleDimension = array(1, 'Hello', 'Jordi');
86
87 return array(
88 array(array(), '[]'),
89 array($multipleDimensions, json_encode($multipleDimensions, $jsonPrettyPrintFlag)),
90 array($numericKeys, json_encode($numericKeys, $jsonPrettyPrintFlag)),
91 array($singleDimension, json_encode($singleDimension))
92 );
93 }
94
98 public function testStringify($fields, $expectedResult)
99 {
100 $slackRecord = new SlackRecord(
101 '#test',
102 'test',
103 true,
104 null,
105 true,
106 true
107 );
108
109 $this->assertSame($expectedResult, $slackRecord->stringify($fields));
110 }
111
112 public function testAddsCustomUsername()
113 {
114 $username = 'Monolog bot';
115 $record = new SlackRecord(null, $username);
116 $data = $record->getSlackData($this->getRecord());
117
118 $this->assertArrayHasKey('username', $data);
119 $this->assertSame($username, $data['username']);
120 }
121
122 public function testNoIcon()
123 {
124 $record = new SlackRecord();
125 $data = $record->getSlackData($this->getRecord());
126
127 $this->assertArrayNotHasKey('icon_emoji', $data);
128 }
129
130 public function testAddsIcon()
131 {
132 $record = $this->getRecord();
133 $slackRecord = new SlackRecord(null, null, false, 'ghost');
134 $data = $slackRecord->getSlackData($record);
135
136 $slackRecord2 = new SlackRecord(null, null, false, 'http://github.com/Seldaek/monolog');
137 $data2 = $slackRecord2->getSlackData($record);
138
139 $this->assertArrayHasKey('icon_emoji', $data);
140 $this->assertSame(':ghost:', $data['icon_emoji']);
141 $this->assertArrayHasKey('icon_url', $data2);
142 $this->assertSame('http://github.com/Seldaek/monolog', $data2['icon_url']);
143 }
144
146 {
147 $record = new SlackRecord(null, null, false);
148 $data = $record->getSlackData($this->getRecord());
149
150 $this->assertArrayNotHasKey('attachments', $data);
151 }
152
153 public function testAddsOneAttachment()
154 {
155 $record = new SlackRecord();
156 $data = $record->getSlackData($this->getRecord());
157
158 $this->assertArrayHasKey('attachments', $data);
159 $this->assertArrayHasKey(0, $data['attachments']);
160 $this->assertInternalType('array', $data['attachments'][0]);
161 }
162
164 {
165 $message = 'Test message';
166 $record = new SlackRecord(null, null, false);
167 $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
168
169 $this->assertArrayHasKey('text', $data);
170 $this->assertSame($message, $data['text']);
171 }
172
174 {
175 $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
176 $formatter
177 ->expects($this->any())
178 ->method('format')
179 ->will($this->returnCallback(function ($record) { return $record['message'] . 'test'; }));
180
181 $formatter2 = $this->getMock('Monolog\\Formatter\\FormatterInterface');
182 $formatter2
183 ->expects($this->any())
184 ->method('format')
185 ->will($this->returnCallback(function ($record) { return $record['message'] . 'test1'; }));
186
187 $message = 'Test message';
188 $record = new SlackRecord(null, null, false, null, false, false, array(), $formatter);
189 $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
190
191 $this->assertArrayHasKey('text', $data);
192 $this->assertSame($message . 'test', $data['text']);
193
194 $record->setFormatter($formatter2);
195 $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
196
197 $this->assertArrayHasKey('text', $data);
198 $this->assertSame($message . 'test1', $data['text']);
199 }
200
202 {
203 $message = 'Test message';
204 $record = new SlackRecord(null);
205 $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
206
207 $this->assertSame($message, $data['attachments'][0]['text']);
208 $this->assertSame($message, $data['attachments'][0]['fallback']);
209 }
210
212 {
213 $record = new SlackRecord(null);
214 $errorLoggerRecord = $this->getRecord(Logger::ERROR);
215 $emergencyLoggerRecord = $this->getRecord(Logger::EMERGENCY);
216 $warningLoggerRecord = $this->getRecord(Logger::WARNING);
217 $infoLoggerRecord = $this->getRecord(Logger::INFO);
218 $debugLoggerRecord = $this->getRecord(Logger::DEBUG);
219
220 $data = $record->getSlackData($errorLoggerRecord);
221 $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
222
223 $data = $record->getSlackData($emergencyLoggerRecord);
224 $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
225
226 $data = $record->getSlackData($warningLoggerRecord);
227 $this->assertSame(SlackRecord::COLOR_WARNING, $data['attachments'][0]['color']);
228
229 $data = $record->getSlackData($infoLoggerRecord);
230 $this->assertSame(SlackRecord::COLOR_GOOD, $data['attachments'][0]['color']);
231
232 $data = $record->getSlackData($debugLoggerRecord);
233 $this->assertSame(SlackRecord::COLOR_DEFAULT, $data['attachments'][0]['color']);
234 }
235
237 {
238 $level = Logger::ERROR;
239 $levelName = Logger::getLevelName($level);
240 $record = new SlackRecord(null, null, true, null, true);
241 $data = $record->getSlackData($this->getRecord($level, 'test', array('test' => 1)));
242
243 $attachment = $data['attachments'][0];
244 $this->assertArrayHasKey('title', $attachment);
245 $this->assertArrayHasKey('fields', $attachment);
246 $this->assertSame($levelName, $attachment['title']);
247 $this->assertSame(array(), $attachment['fields']);
248 }
249
251 {
252 $level = Logger::ERROR;
253 $levelName = Logger::getLevelName($level);
254 $context = array('test' => 1);
255 $extra = array('tags' => array('web'));
256 $record = new SlackRecord(null, null, true, null, true, true);
257 $loggerRecord = $this->getRecord($level, 'test', $context);
258 $loggerRecord['extra'] = $extra;
259 $data = $record->getSlackData($loggerRecord);
260
261 $attachment = $data['attachments'][0];
262 $this->assertArrayHasKey('title', $attachment);
263 $this->assertArrayHasKey('fields', $attachment);
264 $this->assertCount(2, $attachment['fields']);
265 $this->assertSame($levelName, $attachment['title']);
266 $this->assertSame(
267 array(
268 array(
269 'title' => 'Extra',
270 'value' => sprintf('```%s```', json_encode($extra, $this->jsonPrettyPrintFlag)),
271 'short' => false
272 ),
273 array(
274 'title' => 'Context',
275 'value' => sprintf('```%s```', json_encode($context, $this->jsonPrettyPrintFlag)),
276 'short' => false
277 )
278 ),
279 $attachment['fields']
280 );
281 }
282
284 {
285 $level = Logger::ERROR;
286 $levelName = Logger::getLevelName($level);
287 $record = new SlackRecord(null, null, true, null);
288 $data = $record->getSlackData($this->getRecord($level, 'test', array('test' => 1)));
289
290 $attachment = $data['attachments'][0];
291 $this->assertArrayHasKey('title', $attachment);
292 $this->assertArrayHasKey('fields', $attachment);
293 $this->assertCount(1, $attachment['fields']);
294 $this->assertSame('Message', $attachment['title']);
295 $this->assertSame(
296 array(array(
297 'title' => 'Level',
298 'value' => $levelName,
299 'short' => false
300 )),
301 $attachment['fields']
302 );
303 }
304
306 {
307 $level = Logger::ERROR;
308 $levelName = Logger::getLevelName($level);
309 $context = array('test' => 1);
310 $extra = array('tags' => array('web'));
311 $record = new SlackRecord(null, null, true, null, false, true);
312 $loggerRecord = $this->getRecord($level, 'test', $context);
313 $loggerRecord['extra'] = $extra;
314 $data = $record->getSlackData($loggerRecord);
315
316 $expectedFields = array(
317 array(
318 'title' => 'Level',
319 'value' => $levelName,
320 'short' => false,
321 ),
322 array(
323 'title' => 'Tags',
324 'value' => sprintf('```%s```', json_encode($extra['tags'])),
325 'short' => false
326 ),
327 array(
328 'title' => 'Test',
329 'value' => $context['test'],
330 'short' => false
331 )
332 );
333
334 $attachment = $data['attachments'][0];
335 $this->assertArrayHasKey('title', $attachment);
336 $this->assertArrayHasKey('fields', $attachment);
337 $this->assertCount(3, $attachment['fields']);
338 $this->assertSame('Message', $attachment['title']);
339 $this->assertSame(
340 $expectedFields,
341 $attachment['fields']
342 );
343 }
344
346 {
347 $record = $this->getRecord();
348 $slackRecord = new SlackRecord();
349 $data = $slackRecord->getSlackData($this->getRecord());
350
351 $attachment = $data['attachments'][0];
352 $this->assertArrayHasKey('ts', $attachment);
353 $this->assertSame($record['datetime']->getTimestamp(), $attachment['ts']);
354 }
355
356 public function testContextHasException()
357 {
358 $record = $this->getRecord(Logger::CRITICAL, 'This is a critical message.', array('exception' => new \Exception()));
359 $slackRecord = new SlackRecord(null, null, true, null, false, true);
360 $data = $slackRecord->getSlackData($record);
361 $this->assertInternalType('string', $data['attachments'][0]['fields'][1]['value']);
362 }
363
365 {
366 $record = $this->getRecord(
368 'test',
369 array('info' => array('library' => 'monolog', 'author' => 'Jordi'))
370 );
371 $record['extra'] = array('tags' => array('web', 'cli'));
372
373 $slackRecord = new SlackRecord(null, null, true, null, false, true, array('context.info.library', 'extra.tags.1'));
374 $data = $slackRecord->getSlackData($record);
375 $attachment = $data['attachments'][0];
376
377 $expected = array(
378 array(
379 'title' => 'Info',
380 'value' => sprintf('```%s```', json_encode(array('author' => 'Jordi'), $this->jsonPrettyPrintFlag)),
381 'short' => false
382 ),
383 array(
384 'title' => 'Tags',
385 'value' => sprintf('```%s```', json_encode(array('web'))),
386 'short' => false
387 ),
388 );
389
390 foreach ($expected as $field) {
391 $this->assertNotFalse(array_search($field, $attachment['fields']));
392 break;
393 }
394 }
395}
An exception for terminatinating execution or to throw for unit testing.
@coversDefaultClass Monolog\Handler\Slack\SlackRecord
testGetAttachmentColor($logLevel, $expectedColour)
@dataProvider dataGetAttachmentColor
testStringify($fields, $expectedResult)
@dataProvider dataStringify
Slack record utility helping to log to Slack webhooks or API.
Definition: SlackRecord.php:27
Monolog log channel.
Definition: Logger.php:29
const EMERGENCY
Urgent alert.
Definition: Logger.php:78
const ERROR
Runtime errors.
Definition: Logger.php:58
const CRITICAL
Critical conditions.
Definition: Logger.php:65
static getLevelName($level)
Gets the name of the logging level.
Definition: Logger.php:513
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:53
const INFO
Interesting events.
Definition: Logger.php:40
const DEBUG
Detailed debug information.
Definition: Logger.php:33
const NOTICE
Uncommon events.
Definition: Logger.php:45
const ALERT
Action must be taken immediately.
Definition: Logger.php:73
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
catch(Exception $e) $message
$data
Definition: bench.php:6
$context
Definition: webdav.php:25