ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
12 namespace Monolog\Handler;
13 
15 use Monolog\Logger;
18 
24 {
28  private $res;
29 
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 
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 
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 
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)),
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 }
const NOTICE
Uncommon events.
Definition: Logger.php:45
const DEBUG
Detailed debug information.
Definition: Logger.php:33
const ERROR
Runtime errors.
Definition: Logger.php:58
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
testWriteContentWithColors($level, $expectedColor)
provideLevelColors
createHandler($token='myToken', $channel='channel1', $username='Monolog', $useAttachment=true, $iconEmoji=null, $useShortAttachment=false, $includeExtra=false)
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:53
const EMERGENCY
Urgent alert.
Definition: Logger.php:78
const CRITICAL
Critical conditions.
Definition: Logger.php:65
const ALERT
Action must be taken immediately.
Definition: Logger.php:73
Formats incoming records into a one-line string.
const INFO
Interesting events.
Definition: Logger.php:40