| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- /*
- * This file is part of the Monolog package.
- *
- * (c) Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Monolog\Handler\Slack;
- use Monolog\Formatter\FormatterInterface;
- use Monolog\Logger;
- use Monolog\TestCase;
- /**
- * @coversDefaultClass Monolog\Handler\Slack\SlackRecord
- */
- class SlackRecordTest extends TestCase
- {
- private $channel;
- protected function setUp()
- {
- $this->channel = 'monolog_alerts';
- }
- public function dataGetAttachmentColor()
- {
- return array(
- array(Logger::DEBUG, SlackRecord::COLOR_DEFAULT),
- array(Logger::INFO, SlackRecord::COLOR_GOOD),
- array(Logger::NOTICE, SlackRecord::COLOR_GOOD),
- array(Logger::WARNING, SlackRecord::COLOR_WARNING),
- array(Logger::ERROR, SlackRecord::COLOR_DANGER),
- array(Logger::CRITICAL, SlackRecord::COLOR_DANGER),
- array(Logger::ALERT, SlackRecord::COLOR_DANGER),
- array(Logger::EMERGENCY, SlackRecord::COLOR_DANGER),
- );
- }
- /**
- * @dataProvider dataGetAttachmentColor
- * @param int $logLevel
- * @param string $expectedColour RGB hex color or name of Slack color
- * @covers ::getAttachmentColor
- */
- public function testGetAttachmentColor($logLevel, $expectedColour)
- {
- $slackRecord = new SlackRecord('#test');
- $this->assertSame(
- $expectedColour,
- $slackRecord->getAttachmentColor($logLevel)
- );
- }
- public function testAddsChannel()
- {
- $record = new SlackRecord($this->channel);
- $data = $record->getSlackData($this->getRecord());
- $this->assertArrayHasKey('channel', $data);
- $this->assertSame($this->channel, $data['channel']);
- }
- public function testStringifyReturnsNullWithNoLineFormatter()
- {
- $slackRecord = new SlackRecord('#test');
- $this->assertNull($slackRecord->stringify(array('foo' => 'bar')));
- }
- public function testAddsDefaultUsername()
- {
- $record = new SlackRecord($this->channel);
- $data = $record->getSlackData($this->getRecord());
- $this->assertArrayHasKey('username', $data);
- $this->assertSame('Monolog', $data['username']);
- }
- /**
- * @return array
- */
- public function dataStringify()
- {
- return array(
- array(array(), ''),
- array(array('foo' => 'bar'), 'foo: bar'),
- array(array('Foo' => 'bAr'), 'Foo: bAr'),
- );
- }
- /**
- * @dataProvider dataStringify
- */
- public function testStringifyWithLineFormatter($fields, $expectedResult)
- {
- $slackRecord = new SlackRecord(
- '#test',
- 'test',
- true,
- null,
- true,
- true
- );
- $this->assertSame($expectedResult, $slackRecord->stringify($fields));
- }
- public function testAddsCustomUsername()
- {
- $username = 'Monolog bot';
- $record = new SlackRecord($this->channel, $username);
- $data = $record->getSlackData($this->getRecord());
- $this->assertArrayHasKey('username', $data);
- $this->assertSame($username, $data['username']);
- }
- public function testNoIcon()
- {
- $record = new SlackRecord($this->channel);
- $data = $record->getSlackData($this->getRecord());
- $this->assertArrayNotHasKey('icon_emoji', $data);
- }
- public function testAddsIcon()
- {
- $record = new SlackRecord($this->channel, 'Monolog', true, 'ghost');
- $data = $record->getSlackData($this->getRecord());
- $this->assertArrayHasKey('icon_emoji', $data);
- $this->assertSame(':ghost:', $data['icon_emoji']);
- }
- public function testAddsEmptyTextIfUseAttachment()
- {
- $record = new SlackRecord($this->channel);
- $data = $record->getSlackData($this->getRecord());
- $this->assertArrayHasKey('text', $data);
- $this->assertSame('', $data['text']);
- }
- public function testAttachmentsEmptyIfNoAttachment()
- {
- $record = new SlackRecord($this->channel, 'Monolog', false);
- $data = $record->getSlackData($this->getRecord());
- $this->assertArrayHasKey('attachments', $data);
- $this->assertSame(array(), $data['attachments']);
- }
- public function testAddsOneAttachment()
- {
- $record = new SlackRecord($this->channel);
- $data = $record->getSlackData($this->getRecord());
- $this->assertArrayHasKey('attachments', $data);
- $this->assertArrayHasKey(0, $data['attachments']);
- $this->assertInternalType('array', $data['attachments'][0]);
- }
- public function testTextEqualsMessageIfNoFormatter()
- {
- $message = 'Test message';
- $record = new SlackRecord($this->channel, 'Monolog', false);
- $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
- $this->assertArrayHasKey('text', $data);
- $this->assertSame($message, $data['text']);
- }
- public function testTextEqualsFormatterOutput()
- {
- $formatter = $this->createMock(FormatterInterface::class);
- $formatter
- ->expects($this->any())
- ->method('format')
- ->will($this->returnCallback(function ($record) { return $record['message'] . 'test'; }));
- $message = 'Test message';
- $record = new SlackRecord($this->channel, 'Monolog', false, null, false, false, $formatter);
- $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
- $this->assertArrayHasKey('text', $data);
- $this->assertSame($message . 'test', $data['text']);
- }
- public function testAddsFallbackAndTextToAttachment()
- {
- $message = 'Test message';
- $record = new SlackRecord($this->channel);
- $data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
- $this->assertSame($message, $data['attachments'][0]['text']);
- $this->assertSame($message, $data['attachments'][0]['fallback']);
- }
- public function testMapsLevelToColorAttachmentColor()
- {
- $record = new SlackRecord($this->channel);
- $errorLoggerRecord = $this->getRecord(Logger::ERROR);
- $emergencyLoggerRecord = $this->getRecord(Logger::EMERGENCY);
- $warningLoggerRecord = $this->getRecord(Logger::WARNING);
- $infoLoggerRecord = $this->getRecord(Logger::INFO);
- $debugLoggerRecord = $this->getRecord(Logger::DEBUG);
- $data = $record->getSlackData($errorLoggerRecord);
- $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
- $data = $record->getSlackData($emergencyLoggerRecord);
- $this->assertSame(SlackRecord::COLOR_DANGER, $data['attachments'][0]['color']);
- $data = $record->getSlackData($warningLoggerRecord);
- $this->assertSame(SlackRecord::COLOR_WARNING, $data['attachments'][0]['color']);
- $data = $record->getSlackData($infoLoggerRecord);
- $this->assertSame(SlackRecord::COLOR_GOOD, $data['attachments'][0]['color']);
- $data = $record->getSlackData($debugLoggerRecord);
- $this->assertSame(SlackRecord::COLOR_DEFAULT, $data['attachments'][0]['color']);
- }
- public function testAddsShortAttachmentWithoutContextAndExtra()
- {
- $level = Logger::ERROR;
- $levelName = Logger::getLevelName($level);
- $record = new SlackRecord($this->channel, 'Monolog', true, null, true);
- $data = $record->getSlackData($this->getRecord($level, 'test', array('test' => 1)));
- $attachment = $data['attachments'][0];
- $this->assertArrayHasKey('title', $attachment);
- $this->assertArrayHasKey('fields', $attachment);
- $this->assertSame($levelName, $attachment['title']);
- $this->assertSame(array(), $attachment['fields']);
- }
- public function testAddsShortAttachmentWithContextAndExtra()
- {
- $level = Logger::ERROR;
- $levelName = Logger::getLevelName($level);
- $record = new SlackRecord($this->channel, 'Monolog', true, null, true, true);
- $loggerRecord = $this->getRecord($level, 'test', array('test' => 1));
- $loggerRecord['extra'] = array('tags' => array('web'));
- $data = $record->getSlackData($loggerRecord);
- $attachment = $data['attachments'][0];
- $this->assertArrayHasKey('title', $attachment);
- $this->assertArrayHasKey('fields', $attachment);
- $this->assertCount(2, $attachment['fields']);
- $this->assertSame($levelName, $attachment['title']);
- $this->assertSame(
- array(
- array(
- 'title' => 'Extra',
- 'value' => 'tags: ["web"]',
- 'short' => true
- ),
- array(
- 'title' => 'Context',
- 'value' => 'test: 1',
- 'short' => true
- )
- ),
- $attachment['fields']
- );
- }
- public function testAddsLongAttachmentWithoutContextAndExtra()
- {
- $level = Logger::ERROR;
- $levelName = Logger::getLevelName($level);
- $record = new SlackRecord($this->channel, 'Monolog', true, null);
- $data = $record->getSlackData($this->getRecord($level, 'test', array('test' => 1)));
- $attachment = $data['attachments'][0];
- $this->assertArrayHasKey('title', $attachment);
- $this->assertArrayHasKey('fields', $attachment);
- $this->assertCount(1, $attachment['fields']);
- $this->assertSame('Message', $attachment['title']);
- $this->assertSame(
- array(array(
- 'title' => 'Level',
- 'value' => $levelName,
- 'short' => true
- )),
- $attachment['fields']
- );
- }
- public function testAddsLongAttachmentWithContextAndExtra()
- {
- $level = Logger::ERROR;
- $levelName = Logger::getLevelName($level);
- $record = new SlackRecord($this->channel, 'Monolog', true, null, false, true);
- $loggerRecord = $this->getRecord($level, 'test', array('test' => 1));
- $loggerRecord['extra'] = array('tags' => array('web'));
- $data = $record->getSlackData($loggerRecord);
- $expectedFields = array(
- array(
- 'title' => 'Level',
- 'value' => $levelName,
- 'short' => true,
- ),
- array(
- 'title' => 'tags',
- 'value' => array('web'),
- 'short' => false
- ),
- array(
- 'title' => 'test',
- 'value' => 1,
- 'short' => false
- )
- );
- $attachment = $data['attachments'][0];
- $this->assertArrayHasKey('title', $attachment);
- $this->assertArrayHasKey('fields', $attachment);
- $this->assertCount(3, $attachment['fields']);
- $this->assertSame('Message', $attachment['title']);
- $this->assertSame(
- $expectedFields,
- $attachment['fields']
- );
- }
- }
|