]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/monolog/monolog/tests/Monolog/Handler/SlackHandlerTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / monolog / monolog / tests / Monolog / Handler / SlackHandlerTest.php
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;
13
14 use Monolog\TestCase;
15 use Monolog\Logger;
16 use Monolog\Formatter\LineFormatter;
17 use Monolog\Handler\Slack\SlackRecord;
18
19 /**
20  * @author Greg Kedzierski <greg@gregkedzierski.com>
21  * @see    https://api.slack.com/
22  */
23 class SlackHandlerTest extends TestCase
24 {
25     /**
26      * @var resource
27      */
28     private $res;
29
30     /**
31      * @var SlackHandler
32      */
33     private $handler;
34
35     public function setUp()
36     {
37         if (!extension_loaded('openssl')) {
38             $this->markTestSkipped('This test requires openssl to run');
39         }
40     }
41
42     public function testWriteHeader()
43     {
44         $this->createHandler();
45         $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
46         fseek($this->res, 0);
47         $content = fread($this->res, 1024);
48
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);
50     }
51
52     public function testWriteContent()
53     {
54         $this->createHandler();
55         $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
56         fseek($this->res, 0);
57         $content = fread($this->res, 1024);
58
59         $this->assertRegExp('/username=Monolog/', $content);
60         $this->assertRegExp('/channel=channel1/', $content);
61         $this->assertRegExp('/token=myToken/', $content);
62         $this->assertRegExp('/attachments/', $content);
63     }
64
65     public function testWriteContentUsesFormatterIfProvided()
66     {
67         $this->createHandler('myToken', 'channel1', 'Monolog', false);
68         $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
69         fseek($this->res, 0);
70         $content = fread($this->res, 1024);
71
72         $this->createHandler('myToken', 'channel1', 'Monolog', false);
73         $this->handler->setFormatter(new LineFormatter('foo--%message%'));
74         $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test2'));
75         fseek($this->res, 0);
76         $content2 = fread($this->res, 1024);
77
78         $this->assertRegexp('/text=test1/', $content);
79         $this->assertRegexp('/text=foo--test2/', $content2);
80     }
81
82     public function testWriteContentWithEmoji()
83     {
84         $this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
85         $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
86         fseek($this->res, 0);
87         $content = fread($this->res, 1024);
88
89         $this->assertRegexp('/icon_emoji=%3Aalien%3A/', $content);
90     }
91
92     /**
93      * @dataProvider provideLevelColors
94      */
95     public function testWriteContentWithColors($level, $expectedColor)
96     {
97         $this->createHandler();
98         $this->handler->handle($this->getRecord($level, 'test1'));
99         fseek($this->res, 0);
100         $content = fread($this->res, 1024);
101
102         $this->assertRegexp('/%22color%22%3A%22'.$expectedColor.'/', $content);
103     }
104
105     public function testWriteContentWithPlainTextMessage()
106     {
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);
111
112         $this->assertRegexp('/text=test1/', $content);
113     }
114
115     public function provideLevelColors()
116     {
117         return array(
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),
126         );
127     }
128
129     private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
130     {
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'),
136             $constructorArgs
137         );
138
139         $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
140         $reflectionProperty->setAccessible(true);
141         $reflectionProperty->setValue($this->handler, 'localhost:1234');
142
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));
152
153         $this->handler->setFormatter($this->getIdentityFormatter());
154     }
155 }