1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-13 16:44:23 +02:00

Merge branch '1.x'

This commit is contained in:
Jordi Boggiano
2016-11-26 01:23:57 +01:00
11 changed files with 1010 additions and 171 deletions

View File

@@ -0,0 +1,341 @@
<?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\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 testAttachmentsNotPresentIfNoAttachment()
{
$record = new SlackRecord($this->channel, 'Monolog', false);
$data = $record->getSlackData($this->getRecord());
$this->assertArrayNotHasKey('attachments', $data);
}
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->getMock('Monolog\\Formatter\\FormatterInterface');
$formatter
->expects($this->any())
->method('format')
->will($this->returnCallback(function ($record) { return $record['message'] . 'test'; }));
$formatter2 = $this->getMock('Monolog\\Formatter\\FormatterInterface');
$formatter2
->expects($this->any())
->method('format')
->will($this->returnCallback(function ($record) { return $record['message'] . 'test1'; }));
$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']);
$record->setFormatter($formatter2);
$data = $record->getSlackData($this->getRecord(Logger::WARNING, $message));
$this->assertArrayHasKey('text', $data);
$this->assertSame($message . 'test1', $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' => '["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']
);
}
}

View File

@@ -14,6 +14,7 @@ namespace Monolog\Handler;
use Monolog\Test\TestCase;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\Slack\SlackRecord;
/**
* @author Greg Kedzierski <greg@gregkedzierski.com>
@@ -55,7 +56,10 @@ class SlackHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
$this->assertRegExp('/username=Monolog/', $content);
$this->assertRegExp('/channel=channel1/', $content);
$this->assertRegExp('/token=myToken/', $content);
$this->assertRegExp('/attachments/', $content);
}
public function testWriteContentUsesFormatterIfProvided()
@@ -71,8 +75,8 @@ class SlackHandlerTest extends TestCase
fseek($this->res, 0);
$content2 = fread($this->res, 1024);
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=test1.*$/', $content);
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=foo--test2.*$/', $content2);
$this->assertRegexp('/text=test1/', $content);
$this->assertRegexp('/text=foo--test2/', $content2);
}
public function testWriteContentWithEmoji()
@@ -82,7 +86,7 @@ class SlackHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/icon_emoji=%3Aalien%3A$/', $content);
$this->assertRegexp('/icon_emoji=%3Aalien%3A/', $content);
}
/**
@@ -95,7 +99,7 @@ class SlackHandlerTest extends TestCase
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content);
$this->assertRegexp('/%22color%22%3A%22'.$expectedColor.'/', $content);
}
public function testWriteContentWithPlainTextMessage()
@@ -110,16 +114,16 @@ class SlackHandlerTest extends TestCase
public function provideLevelColors()
{
return [
[Logger::DEBUG, '%23e3e4e6'], // escaped #e3e4e6
[Logger::INFO, 'good'],
[Logger::NOTICE, 'good'],
[Logger::WARNING, 'warning'],
[Logger::ERROR, 'danger'],
[Logger::CRITICAL, 'danger'],
[Logger::ALERT, 'danger'],
[Logger::EMERGENCY,'danger'],
];
return array(
array(Logger::DEBUG, urlencode(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),
);
}
private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)

View File

@@ -0,0 +1,106 @@
<?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;
use Monolog\TestCase;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\Slack\SlackRecord;
/**
* @author Haralan Dobrev <hkdobrev@gmail.com>
* @see https://api.slack.com/incoming-webhooks
* @coversDefaultClass Monolog\Handler\SlackWebhookHandler
*/
class SlackWebhookHandlerTest extends TestCase
{
const WEBHOOK_URL = 'https://hooks.slack.com/services/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E';
/**
* @covers ::__construct
* @covers ::getSlackRecord
*/
public function testConstructorMinimal()
{
$handler = new SlackWebhookHandler(self::WEBHOOK_URL);
$slackRecord = $handler->getSlackRecord();
$this->assertInstanceOf('Monolog\Handler\Slack\SlackRecord', $slackRecord);
$this->assertEquals(array(
'username' => 'Monolog',
'text' => '',
'attachments' => array(
array(
'fallback' => 'test',
'text' => 'test',
'color' => SlackRecord::COLOR_WARNING,
'fields' => array(
array(
'title' => 'Level',
'value' => 'WARNING',
'short' => true,
),
),
'title' => 'Message',
),
),
), $slackRecord->getSlackData($this->getRecord()));
}
/**
* @covers ::__construct
* @covers ::getSlackRecord
*/
public function testConstructorFull()
{
$handler = new SlackWebhookHandler(
self::WEBHOOK_URL,
'test-channel',
'test-username',
false,
':ghost:',
false,
false,
Logger::DEBUG,
false
);
$slackRecord = $handler->getSlackRecord();
$this->assertInstanceOf('Monolog\Handler\Slack\SlackRecord', $slackRecord);
$this->assertEquals(array(
'username' => 'test-username',
'text' => 'test',
'channel' => 'test-channel',
'icon_emoji' => ':ghost:',
), $slackRecord->getSlackData($this->getRecord()));
}
/**
* @covers ::getFormatter
*/
public function testGetFormatter()
{
$handler = new SlackWebhookHandler(self::WEBHOOK_URL);
$formatter = $handler->getFormatter();
$this->assertInstanceOf('Monolog\Formatter\FormatterInterface', $formatter);
}
/**
* @covers ::setFormatter
*/
public function testSetFormatter()
{
$handler = new SlackWebhookHandler(self::WEBHOOK_URL);
$formatter = new LineFormatter();
$handler->setFormatter($formatter);
$this->assertSame($formatter, $handler->getFormatter());
}
}

View File

@@ -0,0 +1,47 @@
<?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;
use Monolog\TestCase;
use Monolog\Logger;
/**
* @author Haralan Dobrev <hkdobrev@gmail.com>
* @see https://slack.com/apps/A0F81R8ET-slackbot
* @coversDefaultClass Monolog\Handler\SlackbotHandler
*/
class SlackbotHandlerTest extends TestCase
{
/**
* @covers ::__construct
*/
public function testConstructorMinimal()
{
$handler = new SlackbotHandler('test-team', 'test-token', 'test-channel');
$this->assertInstanceOf('Monolog\Handler\AbstractProcessingHandler', $handler);
}
/**
* @covers ::__construct
*/
public function testConstructorFull()
{
$handler = new SlackbotHandler(
'test-team',
'test-token',
'test-channel',
Logger::DEBUG,
false
);
$this->assertInstanceOf('Monolog\Handler\AbstractProcessingHandler', $handler);
}
}