ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
16
22{
26 private $res;
27
31 private $handler;
32
33 public function setUp()
34 {
35 if (!extension_loaded('openssl')) {
36 $this->markTestSkipped('This test requires openssl to run');
37 }
38 }
39
40 public function testWriteHeader()
41 {
42 $this->createHandler();
43 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
44 fseek($this->res, 0);
45 $content = fread($this->res, 1024);
46
47 $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);
48 }
49
50 public function testWriteContent()
51 {
52 $this->createHandler();
53 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
54 fseek($this->res, 0);
55 $content = fread($this->res, 1024);
56
57 $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
58 }
59
60 public function testWriteContentWithEmoji()
61 {
62 $this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
63 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
64 fseek($this->res, 0);
65 $content = fread($this->res, 1024);
66
67 $this->assertRegexp('/icon_emoji=%3Aalien%3A$/', $content);
68 }
69
73 public function testWriteContentWithColors($level, $expectedColor)
74 {
75 $this->createHandler();
76 $this->handler->handle($this->getRecord($level, 'test1'));
77 fseek($this->res, 0);
78 $content = fread($this->res, 1024);
79
80 $this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content);
81 }
82
84 {
85 $this->createHandler('myToken', 'channel1', 'Monolog', false);
86 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
87 fseek($this->res, 0);
88 $content = fread($this->res, 1024);
89
90 $this->assertRegexp('/text=test1/', $content);
91 }
92
93 public function provideLevelColors()
94 {
95 return array(
96 array(Logger::DEBUG, '%23e3e4e6'), // escaped #e3e4e6
97 array(Logger::INFO, 'good'),
98 array(Logger::NOTICE, 'good'),
99 array(Logger::WARNING, 'warning'),
100 array(Logger::ERROR, 'danger'),
101 array(Logger::CRITICAL, 'danger'),
102 array(Logger::ALERT, 'danger'),
103 array(Logger::EMERGENCY,'danger'),
104 );
105 }
106
107 private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
108 {
109 $constructorArgs = array($token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true, $useShortAttachment, $includeExtra);
110 $this->res = fopen('php://memory', 'a');
111 $this->handler = $this->getMock(
112 '\Monolog\Handler\SlackHandler',
113 array('fsockopen', 'streamSetTimeout', 'closeSocket'),
114 $constructorArgs
115 );
116
117 $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
118 $reflectionProperty->setAccessible(true);
119 $reflectionProperty->setValue($this->handler, 'localhost:1234');
120
121 $this->handler->expects($this->any())
122 ->method('fsockopen')
123 ->will($this->returnValue($this->res));
124 $this->handler->expects($this->any())
125 ->method('streamSetTimeout')
126 ->will($this->returnValue(true));
127 $this->handler->expects($this->any())
128 ->method('closeSocket')
129 ->will($this->returnValue(true));
130
131 $this->handler->setFormatter($this->getIdentityFormatter());
132 }
133}
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