scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 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  
17 /**
18 * @author Rafael Dohms <rafael@doh.ms>
19 * @see https://www.hipchat.com/docs/api
20 */
21 class HipChatHandlerTest extends TestCase
22 {
23 private $res;
24 /** @var HipChatHandler */
25 private $handler;
26  
27 public function testWriteHeader()
28 {
29 $this->createHandler();
30 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
31 fseek($this->res, 0);
32 $content = fread($this->res, 1024);
33  
34 $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);
35  
36 return $content;
37 }
38  
39 public function testWriteCustomHostHeader()
40 {
41 $this->createHandler('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar');
42 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
43 fseek($this->res, 0);
44 $content = fread($this->res, 1024);
45  
46 $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);
47  
48 return $content;
49 }
50  
51 public function testWriteV2()
52 {
53 $this->createHandler('myToken', 'room1', 'Monolog', false, 'hipchat.foo.bar', 'v2');
54 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
55 fseek($this->res, 0);
56 $content = fread($this->res, 1024);
57  
58 $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);
59  
60 return $content;
61 }
62  
63 public function testWriteV2Notify()
64 {
65 $this->createHandler('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar', 'v2');
66 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
67 fseek($this->res, 0);
68 $content = fread($this->res, 1024);
69  
70 $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);
71  
72 return $content;
73 }
74  
75 public function testRoomSpaces()
76 {
77 $this->createHandler('myToken', 'room name', 'Monolog', false, 'hipchat.foo.bar', 'v2');
78 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
79 fseek($this->res, 0);
80 $content = fread($this->res, 1024);
81  
82 $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);
83  
84 return $content;
85 }
86  
87 /**
88 * @depends testWriteHeader
89 */
90 public function testWriteContent($content)
91 {
92 $this->assertRegexp('/notify=0&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
93 }
94  
95 public function testWriteContentV1WithoutName()
96 {
97 $this->createHandler('myToken', 'room1', null, false, 'hipchat.foo.bar', 'v1');
98 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
99 fseek($this->res, 0);
100 $content = fread($this->res, 1024);
101  
102 $this->assertRegexp('/notify=0&message=test1&message_format=text&color=red&room_id=room1&from=$/', $content);
103  
104 return $content;
105 }
106  
107 /**
108 * @depends testWriteCustomHostHeader
109 */
110 public function testWriteContentNotify($content)
111 {
112 $this->assertRegexp('/notify=1&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
113 }
114  
115 /**
116 * @depends testWriteV2
117 */
118 public function testWriteContentV2($content)
119 {
120 $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red&from=Monolog$/', $content);
121 }
122  
123 /**
124 * @depends testWriteV2Notify
125 */
126 public function testWriteContentV2Notify($content)
127 {
128 $this->assertRegexp('/notify=true&message=test1&message_format=text&color=red&from=Monolog$/', $content);
129 }
130  
131 public function testWriteContentV2WithoutName()
132 {
133 $this->createHandler('myToken', 'room1', null, false, 'hipchat.foo.bar', 'v2');
134 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
135 fseek($this->res, 0);
136 $content = fread($this->res, 1024);
137  
138 $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content);
139  
140 return $content;
141 }
142  
143 public function testWriteWithComplexMessage()
144 {
145 $this->createHandler();
146 $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
147 fseek($this->res, 0);
148 $content = fread($this->res, 1024);
149  
150 $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
151 }
152  
153 public function testWriteTruncatesLongMessage()
154 {
155 $this->createHandler();
156 $this->handler->handle($this->getRecord(Logger::CRITICAL, str_repeat('abcde', 2000)));
157 fseek($this->res, 0);
158 $content = fread($this->res, 12000);
159  
160 $this->assertRegexp('/message='.str_repeat('abcde', 1900).'\+%5Btruncated%5D/', $content);
161 }
162  
163 /**
164 * @dataProvider provideLevelColors
165 */
166 public function testWriteWithErrorLevelsAndColors($level, $expectedColor)
167 {
168 $this->createHandler();
169 $this->handler->handle($this->getRecord($level, 'Backup of database "example" finished in 16 minutes.'));
170 fseek($this->res, 0);
171 $content = fread($this->res, 1024);
172  
173 $this->assertRegexp('/color='.$expectedColor.'/', $content);
174 }
175  
176 public function provideLevelColors()
177 {
178 return array(
179 array(Logger::DEBUG, 'gray'),
180 array(Logger::INFO, 'green'),
181 array(Logger::WARNING, 'yellow'),
182 array(Logger::ERROR, 'red'),
183 array(Logger::CRITICAL, 'red'),
184 array(Logger::ALERT, 'red'),
185 array(Logger::EMERGENCY,'red'),
186 array(Logger::NOTICE, 'green'),
187 );
188 }
189  
190 /**
191 * @dataProvider provideBatchRecords
192 */
193 public function testHandleBatch($records, $expectedColor)
194 {
195 $this->createHandler();
196  
197 $this->handler->handleBatch($records);
198  
199 fseek($this->res, 0);
200 $content = fread($this->res, 1024);
201  
202 $this->assertRegexp('/color='.$expectedColor.'/', $content);
203 }
204  
205 public function provideBatchRecords()
206 {
207 return array(
208 array(
209 array(
210 array('level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTime()),
211 array('level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTime()),
212 array('level' => Logger::CRITICAL, 'message' => 'Everything is broken!', 'level_name' => 'critical', 'datetime' => new \DateTime()),
213 ),
214 'red',
215 ),
216 array(
217 array(
218 array('level' => Logger::WARNING, 'message' => 'Oh bugger!', 'level_name' => 'warning', 'datetime' => new \DateTime()),
219 array('level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTime()),
220 ),
221 'yellow',
222 ),
223 array(
224 array(
225 array('level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTime()),
226 array('level' => Logger::NOTICE, 'message' => 'Something noticeable happened.', 'level_name' => 'notice', 'datetime' => new \DateTime()),
227 ),
228 'green',
229 ),
230 array(
231 array(
232 array('level' => Logger::DEBUG, 'message' => 'Just debugging.', 'level_name' => 'debug', 'datetime' => new \DateTime()),
233 ),
234 'gray',
235 ),
236 );
237 }
238  
239 private function createHandler($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com', $version = 'v1')
240 {
241 $constructorArgs = array($token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host, $version);
242 $this->res = fopen('php://memory', 'a');
243 $this->handler = $this->getMock(
244 '\Monolog\Handler\HipChatHandler',
245 array('fsockopen', 'streamSetTimeout', 'closeSocket'),
246 $constructorArgs
247 );
248  
249 $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
250 $reflectionProperty->setAccessible(true);
251 $reflectionProperty->setValue($this->handler, 'localhost:1234');
252  
253 $this->handler->expects($this->any())
254 ->method('fsockopen')
255 ->will($this->returnValue($this->res));
256 $this->handler->expects($this->any())
257 ->method('streamSetTimeout')
258 ->will($this->returnValue(true));
259 $this->handler->expects($this->any())
260 ->method('closeSocket')
261 ->will($this->returnValue(true));
262  
263 $this->handler->setFormatter($this->getIdentityFormatter());
264 }
265  
266 /**
267 * @expectedException InvalidArgumentException
268 */
269 public function testCreateWithTooLongName()
270 {
271 $hipChatHandler = new HipChatHandler('token', 'room', 'SixteenCharsHere');
272 }
273  
274 public function testCreateWithTooLongNameV2()
275 {
276 // creating a handler with too long of a name but using the v2 api doesn't matter.
277 $hipChatHandler = new HipChatHandler('token', 'room', 'SixteenCharsHere', false, Logger::CRITICAL, true, true, 'test', 'api.hipchat.com', 'v2');
278 }
279 }