4 * This file is part of the Monolog package.
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Monolog\Handler;
16 use Monolog\Formatter\LineFormatter;
17 use Monolog\Handler\Slack\SlackRecord;
20 * @author Greg Kedzierski <greg@gregkedzierski.com>
21 * @see https://api.slack.com/
23 class SlackHandlerTest extends TestCase
35 public function setUp()
37 if (!extension_loaded('openssl')) {
38 $this->markTestSkipped('This test requires openssl to run');
42 public function testWriteHeader()
44 $this->createHandler();
45 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
47 $content = fread($this->res, 1024);
49 $this->assertRegexp('/POST \/api\/chat.postMessage HTTP\/1.1\\r\\nHost: slack.com\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
52 public function testWriteContent()
54 $this->createHandler();
55 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
57 $content = fread($this->res, 1024);
59 $this->assertRegExp('/username=Monolog/', $content);
60 $this->assertRegExp('/channel=channel1/', $content);
61 $this->assertRegExp('/token=myToken/', $content);
62 $this->assertRegExp('/attachments/', $content);
65 public function testWriteContentUsesFormatterIfProvided()
67 $this->createHandler('myToken', 'channel1', 'Monolog', false);
68 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
70 $content = fread($this->res, 1024);
72 $this->createHandler('myToken', 'channel1', 'Monolog', false);
73 $this->handler->setFormatter(new LineFormatter('foo--%message%'));
74 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test2'));
76 $content2 = fread($this->res, 1024);
78 $this->assertRegexp('/text=test1/', $content);
79 $this->assertRegexp('/text=foo--test2/', $content2);
82 public function testWriteContentWithEmoji()
84 $this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
85 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
87 $content = fread($this->res, 1024);
89 $this->assertRegexp('/icon_emoji=%3Aalien%3A/', $content);
93 * @dataProvider provideLevelColors
95 public function testWriteContentWithColors($level, $expectedColor)
97 $this->createHandler();
98 $this->handler->handle($this->getRecord($level, 'test1'));
100 $content = fread($this->res, 1024);
102 $this->assertRegexp('/%22color%22%3A%22'.$expectedColor.'/', $content);
105 public function testWriteContentWithPlainTextMessage()
107 $this->createHandler('myToken', 'channel1', 'Monolog', false);
108 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
109 fseek($this->res, 0);
110 $content = fread($this->res, 1024);
112 $this->assertRegexp('/text=test1/', $content);
115 public function provideLevelColors()
118 array(Logger::DEBUG, urlencode(SlackRecord::COLOR_DEFAULT)),
119 array(Logger::INFO, SlackRecord::COLOR_GOOD),
120 array(Logger::NOTICE, SlackRecord::COLOR_GOOD),
121 array(Logger::WARNING, SlackRecord::COLOR_WARNING),
122 array(Logger::ERROR, SlackRecord::COLOR_DANGER),
123 array(Logger::CRITICAL, SlackRecord::COLOR_DANGER),
124 array(Logger::ALERT, SlackRecord::COLOR_DANGER),
125 array(Logger::EMERGENCY,SlackRecord::COLOR_DANGER),
129 private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
131 $constructorArgs = array($token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true, $useShortAttachment, $includeExtra);
132 $this->res = fopen('php://memory', 'a');
133 $this->handler = $this->getMock(
134 '\Monolog\Handler\SlackHandler',
135 array('fsockopen', 'streamSetTimeout', 'closeSocket'),
139 $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
140 $reflectionProperty->setAccessible(true);
141 $reflectionProperty->setValue($this->handler, 'localhost:1234');
143 $this->handler->expects($this->any())
144 ->method('fsockopen')
145 ->will($this->returnValue($this->res));
146 $this->handler->expects($this->any())
147 ->method('streamSetTimeout')
148 ->will($this->returnValue(true));
149 $this->handler->expects($this->any())
150 ->method('closeSocket')
151 ->will($this->returnValue(true));
153 $this->handler->setFormatter($this->getIdentityFormatter());