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\Slack;
13  
14 use Monolog\Logger;
15 use Monolog\TestCase;
16  
17 /**
18 * @coversDefaultClass Monolog\Handler\Slack\SlackRecord
19 */
20 class SlackRecordTest extends TestCase
21 {
22 private $jsonPrettyPrintFlag;
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(
32 array(Logger::DEBUG, SlackRecord::COLOR_DEFAULT),
33 array(Logger::INFO, SlackRecord::COLOR_GOOD),
34 array(Logger::NOTICE, SlackRecord::COLOR_GOOD),
35 array(Logger::WARNING, SlackRecord::COLOR_WARNING),
36 array(Logger::ERROR, SlackRecord::COLOR_DANGER),
37 array(Logger::CRITICAL, SlackRecord::COLOR_DANGER),
38 array(Logger::ALERT, SlackRecord::COLOR_DANGER),
39 array(Logger::EMERGENCY, SlackRecord::COLOR_DANGER),
40 );
41 }
42  
43 /**
44 * @dataProvider dataGetAttachmentColor
45 * @param int $logLevel
46 * @param string $expectedColour RGB hex color or name of Slack color
47 * @covers ::getAttachmentColor
48 */
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  
76 /**
77 * @return array
78 */
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  
95 /**
96 * @dataProvider dataStringify
97 */
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  
145 public function testAttachmentsNotPresentIfNoAttachment()
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  
163 public function testTextEqualsMessageIfNoAttachment()
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  
173 public function testTextEqualsFormatterOutput()
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  
201 public function testAddsFallbackAndTextToAttachment()
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  
211 public function testMapsLevelToColorAttachmentColor()
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  
236 public function testAddsShortAttachmentWithoutContextAndExtra()
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  
250 public function testAddsShortAttachmentWithContextAndExtra()
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  
283 public function testAddsLongAttachmentWithoutContextAndExtra()
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  
305 public function testAddsLongAttachmentWithContextAndExtra()
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  
345 public function testAddsTimestampToAttachment()
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 testExcludeExtraAndContextFields()
357 {
358 $record = $this->getRecord(
359 Logger::WARNING,
360 'test',
361 array('info' => array('library' => 'monolog', 'author' => 'Jordi'))
362 );
363 $record['extra'] = array('tags' => array('web', 'cli'));
364  
365 $slackRecord = new SlackRecord(null, null, true, null, false, true, array('context.info.library', 'extra.tags.1'));
366 $data = $slackRecord->getSlackData($record);
367 $attachment = $data['attachments'][0];
368  
369 $expected = array(
370 array(
371 'title' => 'info',
372 'value' => sprintf('```%s```', json_encode(array('author' => 'Jordi'), $this->jsonPrettyPrintFlag)),
373 'short' => false
374 ),
375 array(
376 'title' => 'tags',
377 'value' => sprintf('```%s```', json_encode(array('web'))),
378 'short' => false
379 ),
380 );
381  
382 foreach ($expected as $field) {
383 $this->assertNotFalse(array_search($field, $attachment['fields']));
384 break;
385 }
386 }
387 }