ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
HipChatHandlerTest.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;
16 
22 {
23  private $res;
24  private $handler;
25 
26  public function testWriteHeader()
27  {
28  $this->createHandler();
29  $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
30  fseek($this->res, 0);
31  $content = fread($this->res, 1024);
32 
33  $this->assertRegexp('/POST \/v1\/rooms\/message\?format=json&auth_token=.* HTTP\/1.1\\r\\nHost: api.hipchat.com\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
34 
35  return $content;
36  }
37 
38  public function testWriteCustomHostHeader()
39  {
40  $this->createHandler('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar');
41  $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
42  fseek($this->res, 0);
43  $content = fread($this->res, 1024);
44 
45  $this->assertRegexp('/POST \/v1\/rooms\/message\?format=json&auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
46 
47  return $content;
48  }
49 
50  public function testWriteV2() {
51  $this->createHandler('myToken', 'room1', 'Monolog', false, 'hipchat.foo.bar', 'v2');
52  $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
53  fseek($this->res, 0);
54  $content = fread($this->res, 1024);
55 
56  $this->assertRegexp('/POST \/v2\/room\/room1\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
57 
58  return $content;
59  }
60 
61  public function testWriteV2Notify() {
62  $this->createHandler('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar', 'v2');
63  $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
64  fseek($this->res, 0);
65  $content = fread($this->res, 1024);
66 
67  $this->assertRegexp('/POST \/v2\/room\/room1\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
68 
69  return $content;
70  }
71 
72  public function testRoomSpaces() {
73  $this->createHandler('myToken', 'room name', 'Monolog', false, 'hipchat.foo.bar', 'v2');
74  $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
75  fseek($this->res, 0);
76  $content = fread($this->res, 1024);
77 
78  $this->assertRegexp('/POST \/v2\/room\/room%20name\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
79 
80  return $content;
81  }
82 
86  public function testWriteContent($content)
87  {
88  $this->assertRegexp('/notify=0&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
89  }
90 
94  public function testWriteContentNotify($content)
95  {
96  $this->assertRegexp('/notify=1&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
97  }
98 
102  public function testWriteContentV2($content)
103  {
104  $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content);
105  }
106 
110  public function testWriteContentV2Notify($content)
111  {
112  $this->assertRegexp('/notify=true&message=test1&message_format=text&color=red$/', $content);
113  }
114 
115  public function testWriteWithComplexMessage()
116  {
117  $this->createHandler();
118  $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
119  fseek($this->res, 0);
120  $content = fread($this->res, 1024);
121 
122  $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
123  }
124 
128  public function testWriteWithErrorLevelsAndColors($level, $expectedColor)
129  {
130  $this->createHandler();
131  $this->handler->handle($this->getRecord($level, 'Backup of database "example" finished in 16 minutes.'));
132  fseek($this->res, 0);
133  $content = fread($this->res, 1024);
134 
135  $this->assertRegexp('/color='.$expectedColor.'/', $content);
136  }
137 
138  public function provideLevelColors()
139  {
140  return array(
141  array(Logger::DEBUG, 'gray'),
142  array(Logger::INFO, 'green'),
143  array(Logger::WARNING, 'yellow'),
144  array(Logger::ERROR, 'red'),
145  array(Logger::CRITICAL, 'red'),
146  array(Logger::ALERT, 'red'),
147  array(Logger::EMERGENCY,'red'),
148  array(Logger::NOTICE, 'green'),
149  );
150  }
151 
155  public function testHandleBatch($records, $expectedColor)
156  {
157  $this->createHandler();
158 
159  $this->handler->handleBatch($records);
160 
161  fseek($this->res, 0);
162  $content = fread($this->res, 1024);
163 
164  $this->assertRegexp('/color='.$expectedColor.'/', $content);
165  }
166 
167  public function provideBatchRecords()
168  {
169  return array(
170  array(
171  array(
172  array('level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTime()),
173  array('level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTime()),
174  array('level' => Logger::CRITICAL, 'message' => 'Everything is broken!', 'level_name' => 'critical', 'datetime' => new \DateTime())
175  ),
176  'red',
177  ),
178  array(
179  array(
180  array('level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTime()),
181  array('level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTime()),
182  ),
183  'yellow',
184  ),
185  array(
186  array(
187  array('level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTime()),
188  array('level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTime()),
189  ),
190  'green',
191  ),
192  array(
193  array(
194  array('level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTime()),
195  ),
196  'gray',
197  ),
198  );
199  }
200 
201  private function createHandler($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com', $version = 'v1')
202  {
203  $constructorArgs = array($token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host, $version);
204  $this->res = fopen('php://memory', 'a');
205  $this->handler = $this->getMock(
206  '\Monolog\Handler\HipChatHandler',
207  array('fsockopen', 'streamSetTimeout', 'closeSocket'),
208  $constructorArgs
209  );
210 
211  $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
212  $reflectionProperty->setAccessible(true);
213  $reflectionProperty->setValue($this->handler, 'localhost:1234');
214 
215  $this->handler->expects($this->any())
216  ->method('fsockopen')
217  ->will($this->returnValue($this->res));
218  $this->handler->expects($this->any())
219  ->method('streamSetTimeout')
220  ->will($this->returnValue(true));
221  $this->handler->expects($this->any())
222  ->method('closeSocket')
223  ->will($this->returnValue(true));
224 
225  $this->handler->setFormatter($this->getIdentityFormatter());
226  }
227 
231  public function testCreateWithTooLongName()
232  {
233  $hipChatHandler = new \Monolog\Handler\HipChatHandler('token', 'room', 'SixteenCharsHere');
234  }
235 
236  public function testCreateWithTooLongNameV2() {
237  // creating a handler with too long of a name but using the v2 api doesn't matter.
238  $hipChatHandler = new \Monolog\Handler\HipChatHandler('token', 'room', 'SixteenCharsHere', false, Logger::CRITICAL, true, true, 'test', 'api.hipchat.com', 'v2');
239  }
240 }
const NOTICE
Uncommon events.
Definition: Logger.php:44
const DEBUG
Detailed debug information.
Definition: Logger.php:32
const ERROR
Runtime errors.
Definition: Logger.php:57
createHandler($token='myToken', $room='room1', $name='Monolog', $notify=false, $host='api.hipchat.com', $version='v1')
testWriteContent($content)
testWriteHeader
$records
Definition: simple_test.php:17
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
testWriteContentV2Notify($content)
testWriteV2Notify
testWriteWithErrorLevelsAndColors($level, $expectedColor)
provideLevelColors
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
testHandleBatch($records, $expectedColor)
provideBatchRecords
const EMERGENCY
Urgent alert.
Definition: Logger.php:77
const CRITICAL
Critical conditions.
Definition: Logger.php:64
testWriteContentNotify($content)
testWriteCustomHostHeader
const ALERT
Action must be taken immediately.
Definition: Logger.php:72
testCreateWithTooLongName()
InvalidArgumentException
const INFO
Interesting events.
Definition: Logger.php:39