2012-02-25 20:37:04 -03:00
|
|
|
<?php
|
|
|
|
|
2012-02-25 23:36:07 -03:00
|
|
|
/*
|
|
|
|
* This file is part of the Monolog package.
|
|
|
|
*
|
|
|
|
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
2012-02-25 20:37:04 -03:00
|
|
|
|
|
|
|
namespace Monolog\Handler;
|
|
|
|
|
|
|
|
use Monolog\TestCase;
|
|
|
|
use Monolog\Logger;
|
2012-02-25 23:36:07 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Pablo de Leon Belloc <pablolb@gmail.com>
|
|
|
|
*/
|
2012-02-25 20:37:04 -03:00
|
|
|
class SocketHandlerTest extends TestCase
|
|
|
|
{
|
2012-02-26 11:45:10 -03:00
|
|
|
/**
|
|
|
|
* @var Monolog\Handler\SocketHandler
|
|
|
|
*/
|
|
|
|
private $handler;
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
/**
|
|
|
|
* @var resource
|
|
|
|
*/
|
|
|
|
private $res;
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
/**
|
2012-03-28 08:49:25 -03:00
|
|
|
* @expectedException UnexpectedValueException
|
2012-02-26 11:45:10 -03:00
|
|
|
*/
|
2012-02-26 14:17:19 -03:00
|
|
|
public function testInvalidHostname()
|
|
|
|
{
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->createHandler('garbage://here');
|
|
|
|
$this->writeRecord('data');
|
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testBadConnectionTimeout()
|
|
|
|
{
|
|
|
|
$this->createHandler('localhost:1234');
|
|
|
|
$this->handler->setConnectionTimeout(-1);
|
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
public function testSetConnectionTimeout()
|
|
|
|
{
|
|
|
|
$this->createHandler('localhost:1234');
|
2012-08-02 13:10:43 +02:00
|
|
|
$this->handler->setConnectionTimeout(10.1);
|
|
|
|
$this->assertEquals(10.1, $this->handler->getConnectionTimeout());
|
2012-02-26 11:45:10 -03:00
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testBadTimeout()
|
|
|
|
{
|
|
|
|
$this->createHandler('localhost:1234');
|
|
|
|
$this->handler->setTimeout(-1);
|
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
public function testSetTimeout()
|
|
|
|
{
|
|
|
|
$this->createHandler('localhost:1234');
|
2012-08-02 13:10:43 +02:00
|
|
|
$this->handler->setTimeout(10.25);
|
|
|
|
$this->assertEquals(10.25, $this->handler->getTimeout());
|
2012-02-26 11:45:10 -03:00
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
public function testSetConnectionString()
|
|
|
|
{
|
|
|
|
$this->createHandler('tcp://localhost:9090');
|
|
|
|
$this->assertEquals('tcp://localhost:9090', $this->handler->getConnectionString());
|
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
/**
|
2012-03-28 08:49:25 -03:00
|
|
|
* @expectedException UnexpectedValueException
|
2012-02-26 11:45:10 -03:00
|
|
|
*/
|
2012-02-27 12:38:06 -03:00
|
|
|
public function testExceptionIsThrownOnFsockopenError()
|
2012-02-26 11:45:10 -03:00
|
|
|
{
|
2012-02-27 12:38:06 -03:00
|
|
|
$this->setMockHandler(array('fsockopen'));
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->handler->expects($this->once())
|
2012-04-01 20:43:39 -03:00
|
|
|
->method('fsockopen')
|
|
|
|
->will($this->returnValue(false));
|
2012-02-27 12:38:06 -03:00
|
|
|
$this->writeRecord('Hello world');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-28 08:49:25 -03:00
|
|
|
* @expectedException UnexpectedValueException
|
2012-02-27 12:38:06 -03:00
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownOnPfsockopenError()
|
|
|
|
{
|
|
|
|
$this->setMockHandler(array('pfsockopen'));
|
|
|
|
$this->handler->expects($this->once())
|
2012-04-01 20:43:39 -03:00
|
|
|
->method('pfsockopen')
|
|
|
|
->will($this->returnValue(false));
|
2012-02-27 12:38:06 -03:00
|
|
|
$this->handler->setPersistent(true);
|
|
|
|
$this->writeRecord('Hello world');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-28 08:49:25 -03:00
|
|
|
* @expectedException UnexpectedValueException
|
2012-02-27 12:38:06 -03:00
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfCannotSetTimeout()
|
|
|
|
{
|
2012-04-22 16:36:31 +02:00
|
|
|
$this->setMockHandler(array('streamSetTimeout'));
|
2012-02-27 12:38:06 -03:00
|
|
|
$this->handler->expects($this->once())
|
2012-04-22 16:36:31 +02:00
|
|
|
->method('streamSetTimeout')
|
2012-04-01 20:43:39 -03:00
|
|
|
->will($this->returnValue(false));
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->writeRecord('Hello world');
|
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
/**
|
2012-03-28 08:49:25 -03:00
|
|
|
* @expectedException RuntimeException
|
2012-02-26 11:45:10 -03:00
|
|
|
*/
|
|
|
|
public function testWriteFailsOnIfFwriteReturnsFalse()
|
|
|
|
{
|
|
|
|
$this->setMockHandler(array('fwrite'));
|
2012-04-01 20:43:39 -03:00
|
|
|
|
2014-03-23 20:50:26 +01:00
|
|
|
$callback = function ($arg) {
|
2012-04-01 21:11:25 -03:00
|
|
|
$map = array(
|
|
|
|
'Hello world' => 6,
|
|
|
|
'world' => false,
|
|
|
|
);
|
2012-06-14 15:46:17 +02:00
|
|
|
|
2012-04-01 21:11:25 -03:00
|
|
|
return $map[$arg];
|
|
|
|
};
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->handler->expects($this->exactly(2))
|
2012-04-01 20:43:39 -03:00
|
|
|
->method('fwrite')
|
|
|
|
->will($this->returnCallback($callback));
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->writeRecord('Hello world');
|
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
/**
|
2012-03-28 08:49:25 -03:00
|
|
|
* @expectedException RuntimeException
|
2012-02-26 11:45:10 -03:00
|
|
|
*/
|
|
|
|
public function testWriteFailsIfStreamTimesOut()
|
|
|
|
{
|
2012-04-22 16:36:31 +02:00
|
|
|
$this->setMockHandler(array('fwrite', 'streamGetMetadata'));
|
2012-04-01 20:43:39 -03:00
|
|
|
|
2014-03-23 20:50:26 +01:00
|
|
|
$callback = function ($arg) {
|
2012-04-01 21:11:25 -03:00
|
|
|
$map = array(
|
|
|
|
'Hello world' => 6,
|
|
|
|
'world' => 5,
|
|
|
|
);
|
2012-06-14 15:46:17 +02:00
|
|
|
|
2012-04-01 21:11:25 -03:00
|
|
|
return $map[$arg];
|
|
|
|
};
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->handler->expects($this->exactly(1))
|
2012-04-01 20:43:39 -03:00
|
|
|
->method('fwrite')
|
|
|
|
->will($this->returnCallback($callback));
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->handler->expects($this->exactly(1))
|
2012-04-22 16:36:31 +02:00
|
|
|
->method('streamGetMetadata')
|
2012-04-01 20:43:39 -03:00
|
|
|
->will($this->returnValue(array('timed_out' => true)));
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->writeRecord('Hello world');
|
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
/**
|
2012-03-28 08:49:25 -03:00
|
|
|
* @expectedException RuntimeException
|
2012-02-26 11:45:10 -03:00
|
|
|
*/
|
|
|
|
public function testWriteFailsOnIncompleteWrite()
|
2012-02-25 20:37:04 -03:00
|
|
|
{
|
2012-04-22 16:36:31 +02:00
|
|
|
$this->setMockHandler(array('fwrite', 'streamGetMetadata'));
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-27 12:38:06 -03:00
|
|
|
$res = $this->res;
|
2014-03-23 20:50:26 +01:00
|
|
|
$callback = function ($string) use ($res) {
|
2012-04-01 21:11:25 -03:00
|
|
|
fclose($res);
|
2012-06-14 15:46:17 +02:00
|
|
|
|
2012-04-01 21:11:25 -03:00
|
|
|
return strlen('Hello');
|
|
|
|
};
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->handler->expects($this->exactly(1))
|
2012-04-01 20:43:39 -03:00
|
|
|
->method('fwrite')
|
|
|
|
->will($this->returnCallback($callback));
|
2012-02-27 12:38:06 -03:00
|
|
|
$this->handler->expects($this->exactly(1))
|
2012-04-22 16:36:31 +02:00
|
|
|
->method('streamGetMetadata')
|
2012-04-01 20:43:39 -03:00
|
|
|
->will($this->returnValue(array('timed_out' => false)));
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->writeRecord('Hello world');
|
2012-02-25 20:37:04 -03:00
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
public function testWriteWithMemoryFile()
|
2012-02-25 20:37:04 -03:00
|
|
|
{
|
2012-02-27 12:38:06 -03:00
|
|
|
$this->setMockHandler();
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->writeRecord('test1');
|
|
|
|
$this->writeRecord('test2');
|
|
|
|
$this->writeRecord('test3');
|
|
|
|
fseek($this->res, 0);
|
|
|
|
$this->assertEquals('test1test2test3', fread($this->res, 1024));
|
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
public function testWriteWithMock()
|
|
|
|
{
|
|
|
|
$this->setMockHandler(array('fwrite'));
|
2012-04-01 20:43:39 -03:00
|
|
|
|
2014-03-23 20:50:26 +01:00
|
|
|
$callback = function ($arg) {
|
2012-04-01 21:11:25 -03:00
|
|
|
$map = array(
|
|
|
|
'Hello world' => 6,
|
|
|
|
'world' => 5,
|
|
|
|
);
|
2012-06-14 15:46:17 +02:00
|
|
|
|
2012-04-01 21:11:25 -03:00
|
|
|
return $map[$arg];
|
|
|
|
};
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->handler->expects($this->exactly(2))
|
2012-04-01 20:43:39 -03:00
|
|
|
->method('fwrite')
|
|
|
|
->will($this->returnCallback($callback));
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->writeRecord('Hello world');
|
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
public function testClose()
|
|
|
|
{
|
2012-02-27 12:38:06 -03:00
|
|
|
$this->setMockHandler();
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->writeRecord('Hello world');
|
2012-04-01 20:43:39 -03:00
|
|
|
$this->assertInternalType('resource', $this->res);
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->handler->close();
|
2012-04-01 20:43:39 -03:00
|
|
|
$this->assertFalse(is_resource($this->res), "Expected resource to be closed after closing handler");
|
2012-02-25 20:37:04 -03:00
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-25 20:37:04 -03:00
|
|
|
public function testCloseDoesNotClosePersistentSocket()
|
|
|
|
{
|
2012-02-27 12:38:06 -03:00
|
|
|
$this->setMockHandler();
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->handler->setPersistent(true);
|
|
|
|
$this->writeRecord('Hello world');
|
|
|
|
$this->assertTrue(is_resource($this->res));
|
|
|
|
$this->handler->close();
|
|
|
|
$this->assertTrue(is_resource($this->res));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createHandler($connectionString)
|
|
|
|
{
|
|
|
|
$this->handler = new SocketHandler($connectionString);
|
|
|
|
$this->handler->setFormatter($this->getIdentityFormatter());
|
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
private function writeRecord($string)
|
|
|
|
{
|
|
|
|
$this->handler->handle($this->getRecord(Logger::WARNING, $string));
|
|
|
|
}
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-02-27 12:38:06 -03:00
|
|
|
private function setMockHandler(array $methods = array())
|
2012-02-26 11:45:10 -03:00
|
|
|
{
|
|
|
|
$this->res = fopen('php://memory', 'a');
|
2012-02-26 14:17:19 -03:00
|
|
|
|
2012-04-22 16:36:31 +02:00
|
|
|
$defaultMethods = array('fsockopen', 'pfsockopen', 'streamSetTimeout');
|
2012-02-27 12:38:06 -03:00
|
|
|
$newMethods = array_diff($methods, $defaultMethods);
|
|
|
|
|
|
|
|
$finalMethods = array_merge($defaultMethods, $newMethods);
|
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->handler = $this->getMock(
|
2012-04-01 20:43:39 -03:00
|
|
|
'\Monolog\Handler\SocketHandler', $finalMethods, array('localhost:1234')
|
2012-02-26 11:45:10 -03:00
|
|
|
);
|
2012-02-27 12:38:06 -03:00
|
|
|
|
|
|
|
if (!in_array('fsockopen', $methods)) {
|
|
|
|
$this->handler->expects($this->any())
|
2012-04-01 20:43:39 -03:00
|
|
|
->method('fsockopen')
|
|
|
|
->will($this->returnValue($this->res));
|
2012-02-27 12:38:06 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array('pfsockopen', $methods)) {
|
|
|
|
$this->handler->expects($this->any())
|
2012-04-01 20:43:39 -03:00
|
|
|
->method('pfsockopen')
|
|
|
|
->will($this->returnValue($this->res));
|
2012-02-27 12:38:06 -03:00
|
|
|
}
|
|
|
|
|
2012-04-22 16:36:31 +02:00
|
|
|
if (!in_array('streamSetTimeout', $methods)) {
|
2012-02-27 12:38:06 -03:00
|
|
|
$this->handler->expects($this->any())
|
2012-04-22 16:36:31 +02:00
|
|
|
->method('streamSetTimeout')
|
2012-04-01 20:43:39 -03:00
|
|
|
->will($this->returnValue(true));
|
2012-02-27 12:38:06 -03:00
|
|
|
}
|
|
|
|
|
2012-02-26 11:45:10 -03:00
|
|
|
$this->handler->setFormatter($this->getIdentityFormatter());
|
|
|
|
}
|
2012-02-25 20:37:04 -03:00
|
|
|
}
|