26 $this->jsonPrettyPrintFlag = defined(
'JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 128;
54 $slackRecord->getAttachmentColor($logLevel)
64 $this->assertArrayHasKey(
'channel',
$data);
65 $this->assertSame($channel,
$data[
'channel']);
73 $this->assertArrayNotHasKey(
'username',
$data);
83 $multipleDimensions = array(array(1, 2));
84 $numericKeys = array(
'library' =>
'monolog');
85 $singleDimension = array(1,
'Hello',
'Jordi');
91 array($singleDimension, json_encode($singleDimension))
109 $this->assertSame($expectedResult, $slackRecord->stringify($fields));
114 $username =
'Monolog bot';
118 $this->assertArrayHasKey(
'username',
$data);
119 $this->assertSame($username,
$data[
'username']);
127 $this->assertArrayNotHasKey(
'icon_emoji',
$data);
133 $slackRecord =
new SlackRecord(
null,
null,
false,
'ghost');
134 $data = $slackRecord->getSlackData($record);
136 $slackRecord2 =
new SlackRecord(
null,
null,
false,
'http://github.com/Seldaek/monolog');
137 $data2 = $slackRecord2->getSlackData($record);
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']);
150 $this->assertArrayNotHasKey(
'attachments',
$data);
158 $this->assertArrayHasKey(
'attachments',
$data);
159 $this->assertArrayHasKey(0,
$data[
'attachments']);
160 $this->assertInternalType(
'array',
$data[
'attachments'][0]);
169 $this->assertArrayHasKey(
'text',
$data);
175 $formatter = $this->getMock(
'Monolog\\Formatter\\FormatterInterface');
177 ->expects($this->any())
179 ->will($this->returnCallback(
function ($record) {
return $record[
'message'] .
'test'; }));
181 $formatter2 = $this->getMock(
'Monolog\\Formatter\\FormatterInterface');
183 ->expects($this->any())
185 ->will($this->returnCallback(
function ($record) {
return $record[
'message'] .
'test1'; }));
188 $record =
new SlackRecord(
null,
null,
false,
null,
false,
false, array(), $formatter);
191 $this->assertArrayHasKey(
'text',
$data);
194 $record->setFormatter($formatter2);
197 $this->assertArrayHasKey(
'text',
$data);
207 $this->assertSame(
$message,
$data[
'attachments'][0][
'text']);
208 $this->assertSame(
$message,
$data[
'attachments'][0][
'fallback']);
220 $data = $record->getSlackData($errorLoggerRecord);
223 $data = $record->getSlackData($emergencyLoggerRecord);
226 $data = $record->getSlackData($warningLoggerRecord);
229 $data = $record->getSlackData($infoLoggerRecord);
232 $data = $record->getSlackData($debugLoggerRecord);
240 $record =
new SlackRecord(
null,
null,
true,
null,
true);
241 $data = $record->getSlackData($this->
getRecord($level,
'test', array(
'test' => 1)));
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']);
255 $extra = array(
'tags' => array(
'web'));
256 $record =
new SlackRecord(
null,
null,
true,
null,
true,
true);
258 $loggerRecord[
'extra'] = $extra;
259 $data = $record->getSlackData($loggerRecord);
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']);
270 'value' => sprintf(
'```%s```', json_encode($extra, $this->jsonPrettyPrintFlag)),
274 'title' =>
'Context',
275 'value' => sprintf(
'```%s```', json_encode(
$context, $this->jsonPrettyPrintFlag)),
279 $attachment[
'fields']
288 $data = $record->getSlackData($this->
getRecord($level,
'test', array(
'test' => 1)));
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']);
298 'value' => $levelName,
301 $attachment[
'fields']
310 $extra = array(
'tags' => array(
'web'));
311 $record =
new SlackRecord(
null,
null,
true,
null,
false,
true);
313 $loggerRecord[
'extra'] = $extra;
314 $data = $record->getSlackData($loggerRecord);
316 $expectedFields = array(
319 'value' => $levelName,
324 'value' => sprintf(
'```%s```', json_encode($extra[
'tags'])),
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']);
341 $attachment[
'fields']
351 $attachment =
$data[
'attachments'][0];
352 $this->assertArrayHasKey(
'ts', $attachment);
353 $this->assertSame($record[
'datetime']->getTimestamp(), $attachment[
'ts']);
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']);
369 array(
'info' => array(
'library' =>
'monolog',
'author' =>
'Jordi'))
371 $record[
'extra'] = array(
'tags' => array(
'web',
'cli'));
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];
380 'value' => sprintf(
'```%s```', json_encode(array(
'author' =>
'Jordi'), $this->jsonPrettyPrintFlag)),
385 'value' => sprintf(
'```%s```', json_encode(array(
'web'))),
390 foreach ($expected as $field) {
391 $this->assertNotFalse(array_search($field, $attachment[
'fields']));
An exception for terminatinating execution or to throw for unit testing.
@coversDefaultClass Monolog\Handler\Slack\SlackRecord
testMapsLevelToColorAttachmentColor()
testContextHasException()
testAddsLongAttachmentWithoutContextAndExtra()
testNoUsernameByDefault()
testTextEqualsFormatterOutput()
testAddsShortAttachmentWithoutContextAndExtra()
testAttachmentsNotPresentIfNoAttachment()
testAddsShortAttachmentWithContextAndExtra()
testAddsLongAttachmentWithContextAndExtra()
testAddsTimestampToAttachment()
testExcludeExtraAndContextFields()
testTextEqualsMessageIfNoAttachment()
testGetAttachmentColor($logLevel, $expectedColour)
@dataProvider dataGetAttachmentColor
testAddsFallbackAndTextToAttachment()
testStringify($fields, $expectedResult)
@dataProvider dataStringify
Slack record utility helping to log to Slack webhooks or API.
const EMERGENCY
Urgent alert.
const ERROR
Runtime errors.
const CRITICAL
Critical conditions.
static getLevelName($level)
Gets the name of the logging level.
const WARNING
Exceptional occurrences that are not errors.
const INFO
Interesting events.
const DEBUG
Detailed debug information.
const NOTICE
Uncommon events.
const ALERT
Action must be taken immediately.
getRecord($level=Logger::WARNING, $message='test', $context=array())
catch(Exception $e) $message