ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
12 namespace Monolog\Handler;
13 
15 use Monolog\Logger;
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 testSetConnectionString()
74  {
75  $this->createHandler('tcp://localhost:9090');
76  $this->assertEquals('tcp://localhost:9090', $this->handler->getConnectionString());
77  }
78 
83  {
84  $this->setMockHandler(array('fsockopen'));
85  $this->handler->expects($this->once())
86  ->method('fsockopen')
87  ->will($this->returnValue(false));
88  $this->writeRecord('Hello world');
89  }
90 
95  {
96  $this->setMockHandler(array('pfsockopen'));
97  $this->handler->expects($this->once())
98  ->method('pfsockopen')
99  ->will($this->returnValue(false));
100  $this->handler->setPersistent(true);
101  $this->writeRecord('Hello world');
102  }
103 
108  {
109  $this->setMockHandler(array('streamSetTimeout'));
110  $this->handler->expects($this->once())
111  ->method('streamSetTimeout')
112  ->will($this->returnValue(false));
113  $this->writeRecord('Hello world');
114  }
115 
120  {
121  $this->setMockHandler(array('fwrite'));
122 
123  $callback = function ($arg) {
124  $map = array(
125  'Hello world' => 6,
126  'world' => false,
127  );
128 
129  return $map[$arg];
130  };
131 
132  $this->handler->expects($this->exactly(2))
133  ->method('fwrite')
134  ->will($this->returnCallback($callback));
135 
136  $this->writeRecord('Hello world');
137  }
138 
143  {
144  $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
145 
146  $callback = function ($arg) {
147  $map = array(
148  'Hello world' => 6,
149  'world' => 5,
150  );
151 
152  return $map[$arg];
153  };
154 
155  $this->handler->expects($this->exactly(1))
156  ->method('fwrite')
157  ->will($this->returnCallback($callback));
158  $this->handler->expects($this->exactly(1))
159  ->method('streamGetMetadata')
160  ->will($this->returnValue(array('timed_out' => true)));
161 
162  $this->writeRecord('Hello world');
163  }
164 
169  {
170  $this->setMockHandler(array('fwrite', 'streamGetMetadata'));
171 
172  $res = $this->res;
173  $callback = function ($string) use ($res) {
174  fclose($res);
175 
176  return strlen('Hello');
177  };
178 
179  $this->handler->expects($this->exactly(1))
180  ->method('fwrite')
181  ->will($this->returnCallback($callback));
182  $this->handler->expects($this->exactly(1))
183  ->method('streamGetMetadata')
184  ->will($this->returnValue(array('timed_out' => false)));
185 
186  $this->writeRecord('Hello world');
187  }
188 
189  public function testWriteWithMemoryFile()
190  {
191  $this->setMockHandler();
192  $this->writeRecord('test1');
193  $this->writeRecord('test2');
194  $this->writeRecord('test3');
195  fseek($this->res, 0);
196  $this->assertEquals('test1test2test3', fread($this->res, 1024));
197  }
198 
199  public function testWriteWithMock()
200  {
201  $this->setMockHandler(array('fwrite'));
202 
203  $callback = function ($arg) {
204  $map = array(
205  'Hello world' => 6,
206  'world' => 5,
207  );
208 
209  return $map[$arg];
210  };
211 
212  $this->handler->expects($this->exactly(2))
213  ->method('fwrite')
214  ->will($this->returnCallback($callback));
215 
216  $this->writeRecord('Hello world');
217  }
218 
219  public function testClose()
220  {
221  $this->setMockHandler();
222  $this->writeRecord('Hello world');
223  $this->assertInternalType('resource', $this->res);
224  $this->handler->close();
225  $this->assertFalse(is_resource($this->res), "Expected resource to be closed after closing handler");
226  }
227 
229  {
230  $this->setMockHandler();
231  $this->handler->setPersistent(true);
232  $this->writeRecord('Hello world');
233  $this->assertTrue(is_resource($this->res));
234  $this->handler->close();
235  $this->assertTrue(is_resource($this->res));
236  }
237 
238  private function createHandler($connectionString)
239  {
240  $this->handler = new SocketHandler($connectionString);
241  $this->handler->setFormatter($this->getIdentityFormatter());
242  }
243 
244  private function writeRecord($string)
245  {
246  $this->handler->handle($this->getRecord(Logger::WARNING, $string));
247  }
248 
249  private function setMockHandler(array $methods = array())
250  {
251  $this->res = fopen('php://memory', 'a');
252 
253  $defaultMethods = array('fsockopen', 'pfsockopen', 'streamSetTimeout');
254  $newMethods = array_diff($methods, $defaultMethods);
255 
256  $finalMethods = array_merge($defaultMethods, $newMethods);
257 
258  $this->handler = $this->getMock(
259  '\Monolog\Handler\SocketHandler', $finalMethods, array('localhost:1234')
260  );
261 
262  if (!in_array('fsockopen', $methods)) {
263  $this->handler->expects($this->any())
264  ->method('fsockopen')
265  ->will($this->returnValue($this->res));
266  }
267 
268  if (!in_array('pfsockopen', $methods)) {
269  $this->handler->expects($this->any())
270  ->method('pfsockopen')
271  ->will($this->returnValue($this->res));
272  }
273 
274  if (!in_array('streamSetTimeout', $methods)) {
275  $this->handler->expects($this->any())
276  ->method('streamSetTimeout')
277  ->will($this->returnValue(true));
278  }
279 
280  $this->handler->setFormatter($this->getIdentityFormatter());
281  }
282 }
setMockHandler(array $methods=array())
Stores to any socket - uses fsockopen() or pfsockopen().
testExceptionIsThrownIfCannotSetTimeout()
UnexpectedValueException
testWriteFailsIfStreamTimesOut()
RuntimeException
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
testInvalidHostname()
UnexpectedValueException
testExceptionIsThrownOnPfsockopenError()
UnexpectedValueException
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
testWriteFailsOnIncompleteWrite()
RuntimeException
testWriteFailsOnIfFwriteReturnsFalse()
RuntimeException
testExceptionIsThrownOnFsockopenError()
UnexpectedValueException