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 Pablo de Leon Belloc <pablolb@gmail.com>
19 */
20 class SocketHandlerTest extends TestCase
21 {
22 /**
23 * @var Monolog\Handler\SocketHandler
24 */
25 private $handler;
26  
27 /**
28 * @var resource
29 */
30 private $res;
31  
32 /**
33 * @expectedException UnexpectedValueException
34 */
35 public function testInvalidHostname()
36 {
37 $this->createHandler('garbage://here');
38 $this->writeRecord('data');
39 }
40  
41 /**
42 * @expectedException \InvalidArgumentException
43 */
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  
57 /**
58 * @expectedException \InvalidArgumentException
59 */
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 testSetConnectionString()
81 {
82 $this->createHandler('tcp://localhost:9090');
83 $this->assertEquals('tcp://localhost:9090', $this->handler->getConnectionString());
84 }
85  
86 /**
87 * @expectedException UnexpectedValueException
88 */
89 public function testExceptionIsThrownOnFsockopenError()
90 {
91 $this->setMockHandler(array('fsockopen'));
92 $this->handler->expects($this->once())
93 ->method('fsockopen')
94 ->will($this->returnValue(false));
95 $this->writeRecord('Hello world');
96 }
97  
98 /**
99 * @expectedException UnexpectedValueException
100 */
101 public function testExceptionIsThrownOnPfsockopenError()
102 {
103 $this->setMockHandler(array('pfsockopen'));
104 $this->handler->expects($this->once())
105 ->method('pfsockopen')
106 ->will($this->returnValue(false));
107 $this->handler->setPersistent(true);
108 $this->writeRecord('Hello world');
109 }
110  
111 /**
112 * @expectedException UnexpectedValueException
113 */
114 public function testExceptionIsThrownIfCannotSetTimeout()
115 {
116 $this->setMockHandler(array('streamSetTimeout'));
117 $this->handler->expects($this->once())
118 ->method('streamSetTimeout')
119 ->will($this->returnValue(false));
120 $this->writeRecord('Hello world');
121 }
122  
123 /**
124 * @expectedException RuntimeException
125 */
126 public function testWriteFailsOnIfFwriteReturnsFalse()
127 {
128 $this->setMockHandler(array('fwrite'));
129  
130 $callback = function ($arg) {
131 $map = array(
132 'Hello world' => 6,
133 'world' => false,
134 );
135  
136 return $map[$arg];
137 };
138  
139 $this->handler->expects($this->exactly(2))
140 ->method('fwrite')
141 ->will($this->returnCallback($callback));
142  
143 $this->writeRecord('Hello world');
144 }
145  
146 /**
147 * @expectedException RuntimeException
148 */
149 public function testWriteFailsIfStreamTimesOut()
150 {
151 $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
152  
153 $callback = function ($arg) {
154 $map = array(
155 'Hello world' => 6,
156 'world' => 5,
157 );
158  
159 return $map[$arg];
160 };
161  
162 $this->handler->expects($this->exactly(1))
163 ->method('fwrite')
164 ->will($this->returnCallback($callback));
165 $this->handler->expects($this->exactly(1))
166 ->method('streamGetMetadata')
167 ->will($this->returnValue(array('timed_out' => true)));
168  
169 $this->writeRecord('Hello world');
170 }
171  
172 /**
173 * @expectedException RuntimeException
174 */
175 public function testWriteFailsOnIncompleteWrite()
176 {
177 $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
178  
179 $res = $this->res;
180 $callback = function ($string) use ($res) {
181 fclose($res);
182  
183 return strlen('Hello');
184 };
185  
186 $this->handler->expects($this->exactly(1))
187 ->method('fwrite')
188 ->will($this->returnCallback($callback));
189 $this->handler->expects($this->exactly(1))
190 ->method('streamGetMetadata')
191 ->will($this->returnValue(array('timed_out' => false)));
192  
193 $this->writeRecord('Hello world');
194 }
195  
196 public function testWriteWithMemoryFile()
197 {
198 $this->setMockHandler();
199 $this->writeRecord('test1');
200 $this->writeRecord('test2');
201 $this->writeRecord('test3');
202 fseek($this->res, 0);
203 $this->assertEquals('test1test2test3', fread($this->res, 1024));
204 }
205  
206 public function testWriteWithMock()
207 {
208 $this->setMockHandler(array('fwrite'));
209  
210 $callback = function ($arg) {
211 $map = array(
212 'Hello world' => 6,
213 'world' => 5,
214 );
215  
216 return $map[$arg];
217 };
218  
219 $this->handler->expects($this->exactly(2))
220 ->method('fwrite')
221 ->will($this->returnCallback($callback));
222  
223 $this->writeRecord('Hello world');
224 }
225  
226 public function testClose()
227 {
228 $this->setMockHandler();
229 $this->writeRecord('Hello world');
230 $this->assertInternalType('resource', $this->res);
231 $this->handler->close();
232 $this->assertFalse(is_resource($this->res), "Expected resource to be closed after closing handler");
233 }
234  
235 public function testCloseDoesNotClosePersistentSocket()
236 {
237 $this->setMockHandler();
238 $this->handler->setPersistent(true);
239 $this->writeRecord('Hello world');
240 $this->assertTrue(is_resource($this->res));
241 $this->handler->close();
242 $this->assertTrue(is_resource($this->res));
243 }
244  
245 /**
246 * @expectedException \RuntimeException
247 */
248 public function testAvoidInfiniteLoopWhenNoDataIsWrittenForAWritingTimeoutSeconds()
249 {
250 $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
251  
252 $this->handler->expects($this->any())
253 ->method('fwrite')
254 ->will($this->returnValue(0));
255  
256 $this->handler->expects($this->any())
257 ->method('streamGetMetadata')
258 ->will($this->returnValue(array('timed_out' => false)));
259  
260 $this->handler->setWritingTimeout(1);
261  
262 $this->writeRecord('Hello world');
263 }
264  
265 private function createHandler($connectionString)
266 {
267 $this->handler = new SocketHandler($connectionString);
268 $this->handler->setFormatter($this->getIdentityFormatter());
269 }
270  
271 private function writeRecord($string)
272 {
273 $this->handler->handle($this->getRecord(Logger::WARNING, $string));
274 }
275  
276 private function setMockHandler(array $methods = array())
277 {
278 $this->res = fopen('php://memory', 'a');
279  
280 $defaultMethods = array('fsockopen', 'pfsockopen', 'streamSetTimeout');
281 $newMethods = array_diff($methods, $defaultMethods);
282  
283 $finalMethods = array_merge($defaultMethods, $newMethods);
284  
285 $this->handler = $this->getMock(
286 '\Monolog\Handler\SocketHandler', $finalMethods, array('localhost:1234')
287 );
288  
289 if (!in_array('fsockopen', $methods)) {
290 $this->handler->expects($this->any())
291 ->method('fsockopen')
292 ->will($this->returnValue($this->res));
293 }
294  
295 if (!in_array('pfsockopen', $methods)) {
296 $this->handler->expects($this->any())
297 ->method('pfsockopen')
298 ->will($this->returnValue($this->res));
299 }
300  
301 if (!in_array('streamSetTimeout', $methods)) {
302 $this->handler->expects($this->any())
303 ->method('streamSetTimeout')
304 ->will($this->returnValue(true));
305 }
306  
307 $this->handler->setFormatter($this->getIdentityFormatter());
308 }
309 }