ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SocketHandlerTest.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
21{
25 private $handler;
26
30 private $res;
31
35 public function testInvalidHostname()
36 {
37 $this->createHandler('garbage://here');
38 $this->writeRecord('data');
39 }
40
44 public function testBadConnectionTimeout()
45 {
46 $this->createHandler('localhost:1234');
47 $this->handler->setConnectionTimeout(-1);
48 }
49
50 public function testSetConnectionTimeout()
51 {
52 $this->createHandler('localhost:1234');
53 $this->handler->setConnectionTimeout(10.1);
54 $this->assertEquals(10.1, $this->handler->getConnectionTimeout());
55 }
56
60 public function testBadTimeout()
61 {
62 $this->createHandler('localhost:1234');
63 $this->handler->setTimeout(-1);
64 }
65
66 public function testSetTimeout()
67 {
68 $this->createHandler('localhost:1234');
69 $this->handler->setTimeout(10.25);
70 $this->assertEquals(10.25, $this->handler->getTimeout());
71 }
72
73 public function testSetWritingTimeout()
74 {
75 $this->createHandler('localhost:1234');
76 $this->handler->setWritingTimeout(10.25);
77 $this->assertEquals(10.25, $this->handler->getWritingTimeout());
78 }
79
80 public function testSetChunkSize()
81 {
82 $this->createHandler('localhost:1234');
83 $this->handler->setChunkSize(1025);
84 $this->assertEquals(1025, $this->handler->getChunkSize());
85 }
86
87 public function testSetConnectionString()
88 {
89 $this->createHandler('tcp://localhost:9090');
90 $this->assertEquals('tcp://localhost:9090', $this->handler->getConnectionString());
91 }
92
97 {
98 $this->setMockHandler(array('fsockopen'));
99 $this->handler->expects($this->once())
100 ->method('fsockopen')
101 ->will($this->returnValue(false));
102 $this->writeRecord('Hello world');
103 }
104
109 {
110 $this->setMockHandler(array('pfsockopen'));
111 $this->handler->expects($this->once())
112 ->method('pfsockopen')
113 ->will($this->returnValue(false));
114 $this->handler->setPersistent(true);
115 $this->writeRecord('Hello world');
116 }
117
122 {
123 $this->setMockHandler(array('streamSetTimeout'));
124 $this->handler->expects($this->once())
125 ->method('streamSetTimeout')
126 ->will($this->returnValue(false));
127 $this->writeRecord('Hello world');
128 }
129
134 {
135 $this->setMockHandler(array('streamSetChunkSize'));
136 $this->handler->setChunkSize(8192);
137 $this->handler->expects($this->once())
138 ->method('streamSetChunkSize')
139 ->will($this->returnValue(false));
140 $this->writeRecord('Hello world');
141 }
142
147 {
148 $this->setMockHandler(array('fwrite'));
149
150 $callback = function ($arg) {
151 $map = array(
152 'Hello world' => 6,
153 'world' => false,
154 );
155
156 return $map[$arg];
157 };
158
159 $this->handler->expects($this->exactly(2))
160 ->method('fwrite')
161 ->will($this->returnCallback($callback));
162
163 $this->writeRecord('Hello world');
164 }
165
170 {
171 $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
172
173 $callback = function ($arg) {
174 $map = array(
175 'Hello world' => 6,
176 'world' => 5,
177 );
178
179 return $map[$arg];
180 };
181
182 $this->handler->expects($this->exactly(1))
183 ->method('fwrite')
184 ->will($this->returnCallback($callback));
185 $this->handler->expects($this->exactly(1))
186 ->method('streamGetMetadata')
187 ->will($this->returnValue(array('timed_out' => true)));
188
189 $this->writeRecord('Hello world');
190 }
191
196 {
197 $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
198
200 $callback = function ($string) use ($res) {
201 fclose($res);
202
203 return strlen('Hello');
204 };
205
206 $this->handler->expects($this->exactly(1))
207 ->method('fwrite')
208 ->will($this->returnCallback($callback));
209 $this->handler->expects($this->exactly(1))
210 ->method('streamGetMetadata')
211 ->will($this->returnValue(array('timed_out' => false)));
212
213 $this->writeRecord('Hello world');
214 }
215
216 public function testWriteWithMemoryFile()
217 {
218 $this->setMockHandler();
219 $this->writeRecord('test1');
220 $this->writeRecord('test2');
221 $this->writeRecord('test3');
222 fseek($this->res, 0);
223 $this->assertEquals('test1test2test3', fread($this->res, 1024));
224 }
225
226 public function testWriteWithMock()
227 {
228 $this->setMockHandler(array('fwrite'));
229
230 $callback = function ($arg) {
231 $map = array(
232 'Hello world' => 6,
233 'world' => 5,
234 );
235
236 return $map[$arg];
237 };
238
239 $this->handler->expects($this->exactly(2))
240 ->method('fwrite')
241 ->will($this->returnCallback($callback));
242
243 $this->writeRecord('Hello world');
244 }
245
246 public function testClose()
247 {
248 $this->setMockHandler();
249 $this->writeRecord('Hello world');
250 $this->assertInternalType('resource', $this->res);
251 $this->handler->close();
252 $this->assertFalse(is_resource($this->res), "Expected resource to be closed after closing handler");
253 }
254
256 {
257 $this->setMockHandler();
258 $this->handler->setPersistent(true);
259 $this->writeRecord('Hello world');
260 $this->assertTrue(is_resource($this->res));
261 $this->handler->close();
262 $this->assertTrue(is_resource($this->res));
263 }
264
269 {
270 $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
271
272 $this->handler->expects($this->any())
273 ->method('fwrite')
274 ->will($this->returnValue(0));
275
276 $this->handler->expects($this->any())
277 ->method('streamGetMetadata')
278 ->will($this->returnValue(array('timed_out' => false)));
279
280 $this->handler->setWritingTimeout(1);
281
282 $this->writeRecord('Hello world');
283 }
284
285 private function createHandler($connectionString)
286 {
287 $this->handler = new SocketHandler($connectionString);
288 $this->handler->setFormatter($this->getIdentityFormatter());
289 }
290
291 private function writeRecord($string)
292 {
293 $this->handler->handle($this->getRecord(Logger::WARNING, $string));
294 }
295
296 private function setMockHandler(array $methods = array())
297 {
298 $this->res = fopen('php://memory', 'a');
299
300 $defaultMethods = array('fsockopen', 'pfsockopen', 'streamSetTimeout');
301 $newMethods = array_diff($methods, $defaultMethods);
302
303 $finalMethods = array_merge($defaultMethods, $newMethods);
304
305 $this->handler = $this->getMock(
306 '\Monolog\Handler\SocketHandler', $finalMethods, array('localhost:1234')
307 );
308
309 if (!in_array('fsockopen', $methods)) {
310 $this->handler->expects($this->any())
311 ->method('fsockopen')
312 ->will($this->returnValue($this->res));
313 }
314
315 if (!in_array('pfsockopen', $methods)) {
316 $this->handler->expects($this->any())
317 ->method('pfsockopen')
318 ->will($this->returnValue($this->res));
319 }
320
321 if (!in_array('streamSetTimeout', $methods)) {
322 $this->handler->expects($this->any())
323 ->method('streamSetTimeout')
324 ->will($this->returnValue(true));
325 }
326
327 if (!in_array('streamSetChunkSize', $methods)) {
328 $this->handler->expects($this->any())
329 ->method('streamSetChunkSize')
330 ->will($this->returnValue(8192));
331 }
332
333 $this->handler->setFormatter($this->getIdentityFormatter());
334 }
335}
An exception for terminatinating execution or to throw for unit testing.
testExceptionIsThrownOnPfsockopenError()
@expectedException UnexpectedValueException
testInvalidHostname()
@expectedException UnexpectedValueException
testBadConnectionTimeout()
@expectedException \InvalidArgumentException
setMockHandler(array $methods=array())
testWriteFailsOnIncompleteWrite()
@expectedException RuntimeException
testWriteFailsIfStreamTimesOut()
@expectedException RuntimeException
testExceptionIsThrownOnFsockopenError()
@expectedException UnexpectedValueException
testBadTimeout()
@expectedException \InvalidArgumentException
testExceptionIsThrownIfCannotSetChunkSize()
@expectedException UnexpectedValueException
testWriteFailsOnIfFwriteReturnsFalse()
@expectedException RuntimeException
testAvoidInfiniteLoopWhenNoDataIsWrittenForAWritingTimeoutSeconds()
@expectedException \RuntimeException
testExceptionIsThrownIfCannotSetTimeout()
@expectedException UnexpectedValueException
Stores to any socket - uses fsockopen() or pfsockopen().
Monolog log channel.
Definition: Logger.php:29
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:53
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.