ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
SlackHandlerTest.php
Go to the documentation of this file.
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
12namespace Monolog\Handler;
13
17
23{
27 private $res;
28
32 private $handler;
33
34 public function setUp()
35 {
36 if (!extension_loaded('openssl')) {
37 $this->markTestSkipped('This test requires openssl to run');
38 }
39 }
40
41 public function testWriteHeader()
42 {
43 $this->createHandler();
44 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
45 fseek($this->res, 0);
46 $content = fread($this->res, 1024);
47
48 $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);
49 }
50
51 public function testWriteContent()
52 {
53 $this->createHandler();
54 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
55 fseek($this->res, 0);
56 $content = fread($this->res, 1024);
57
58 $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
59 }
60
62 {
63 $this->createHandler('myToken', 'channel1', 'Monolog', false);
64 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
65 fseek($this->res, 0);
66 $content = fread($this->res, 1024);
67
68 $this->createHandler('myToken', 'channel1', 'Monolog', false);
69 $this->handler->setFormatter(new LineFormatter('foo--%message%'));
70 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test2'));
71 fseek($this->res, 0);
72 $content2 = fread($this->res, 1024);
73
74 $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=test1.*$/', $content);
75 $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=foo--test2.*$/', $content2);
76 }
77
78 public function testWriteContentWithEmoji()
79 {
80 $this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
81 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
82 fseek($this->res, 0);
83 $content = fread($this->res, 1024);
84
85 $this->assertRegexp('/icon_emoji=%3Aalien%3A$/', $content);
86 }
87
91 public function testWriteContentWithColors($level, $expectedColor)
92 {
93 $this->createHandler();
94 $this->handler->handle($this->getRecord($level, 'test1'));
95 fseek($this->res, 0);
96 $content = fread($this->res, 1024);
97
98 $this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content);
99 }
100
102 {
103 $this->createHandler('myToken', 'channel1', 'Monolog', false);
104 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
105 fseek($this->res, 0);
106 $content = fread($this->res, 1024);
107
108 $this->assertRegexp('/text=test1/', $content);
109 }
110
111 public function provideLevelColors()
112 {
113 return array(
114 array(Logger::DEBUG, '%23e3e4e6'), // escaped #e3e4e6
115 array(Logger::INFO, 'good'),
116 array(Logger::NOTICE, 'good'),
117 array(Logger::WARNING, 'warning'),
118 array(Logger::ERROR, 'danger'),
119 array(Logger::CRITICAL, 'danger'),
120 array(Logger::ALERT, 'danger'),
121 array(Logger::EMERGENCY,'danger'),
122 );
123 }
124
125 private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
126 {
127 $constructorArgs = array($token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true, $useShortAttachment, $includeExtra);
128 $this->res = fopen('php://memory', 'a');
129 $this->handler = $this->getMock(
130 '\Monolog\Handler\SlackHandler',
131 array('fsockopen', 'streamSetTimeout', 'closeSocket'),
132 $constructorArgs
133 );
134
135 $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
136 $reflectionProperty->setAccessible(true);
137 $reflectionProperty->setValue($this->handler, 'localhost:1234');
138
139 $this->handler->expects($this->any())
140 ->method('fsockopen')
141 ->will($this->returnValue($this->res));
142 $this->handler->expects($this->any())
143 ->method('streamSetTimeout')
144 ->will($this->returnValue(true));
145 $this->handler->expects($this->any())
146 ->method('closeSocket')
147 ->will($this->returnValue(true));
148
149 $this->handler->setFormatter($this->getIdentityFormatter());
150 }
151}
An exception for terminatinating execution or to throw for unit testing.
Formats incoming records into a one-line string.
testWriteContentWithColors($level, $expectedColor)
@dataProvider provideLevelColors
createHandler($token='myToken', $channel='channel1', $username='Monolog', $useAttachment=true, $iconEmoji=null, $useShortAttachment=false, $includeExtra=false)
Monolog log channel.
Definition: Logger.php:28
const EMERGENCY
Urgent alert.
Definition: Logger.php:77
const ERROR
Runtime errors.
Definition: Logger.php:57
const CRITICAL
Critical conditions.
Definition: Logger.php:64
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
const INFO
Interesting events.
Definition: Logger.php:39
const DEBUG
Detailed debug information.
Definition: Logger.php:32
const NOTICE
Uncommon events.
Definition: Logger.php:44
const ALERT
Action must be taken immediately.
Definition: Logger.php:72
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19