1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-01 10:50:21 +02:00

Undo this LocalSocket fiasco, fix tests for latest phpunit

This commit is contained in:
Jordi Boggiano
2016-09-25 16:07:01 +02:00
parent 3dc7a79a3e
commit 28742b656f
17 changed files with 456 additions and 459 deletions

View File

@@ -13,7 +13,6 @@ namespace Monolog\Handler;
use Monolog\Test\TestCase;
use Monolog\Logger;
use Monolog\Util\LocalSocket;
/**
* @author Pablo de Leon Belloc <pablolb@gmail.com>
@@ -89,8 +88,10 @@ class SocketHandlerTest extends TestCase
*/
public function testExceptionIsThrownOnFsockopenError()
{
$this->createHandler('tcp://127.0.0.1:51985');
$this->setMockHandler(['fsockopen']);
$this->handler->expects($this->once())
->method('fsockopen')
->will($this->returnValue(false));
$this->writeRecord('Hello world');
}
@@ -99,9 +100,23 @@ class SocketHandlerTest extends TestCase
*/
public function testExceptionIsThrownOnPfsockopenError()
{
$this->createHandler('tcp://127.0.0.1:51985');
$this->setMockHandler(['pfsockopen']);
$this->handler->expects($this->once())
->method('pfsockopen')
->will($this->returnValue(false));
$this->handler->setPersistent(true);
$this->writeRecord('Hello world');
}
/**
* @expectedException UnexpectedValueException
*/
public function testExceptionIsThrownIfCannotSetTimeout()
{
$this->setMockHandler(['streamSetTimeout']);
$this->handler->expects($this->once())
->method('streamSetTimeout')
->will($this->returnValue(false));
$this->writeRecord('Hello world');
}
@@ -110,49 +125,141 @@ class SocketHandlerTest extends TestCase
*/
public function testWriteFailsOnIfFwriteReturnsFalse()
{
$this->initHandlerAndSocket();
$this->setMockHandler(['fwrite']);
$callback = function ($arg) {
$map = [
'Hello world' => 6,
'world' => false,
];
return $map[$arg];
};
$this->handler->expects($this->exactly(2))
->method('fwrite')
->will($this->returnCallback($callback));
$this->writeRecord('Hello world');
LocalSocket::shutdownSocket();
$this->writeRecord('Hello world2');
}
public function testWriteRealSocket()
/**
* @expectedException RuntimeException
*/
public function testWriteFailsIfStreamTimesOut()
{
$this->initHandlerAndSocket();
$this->writeRecord("foo bar baz content test1\n");
$this->writeRecord("foo bar baz content test2\n");
$this->writeRecord("foo bar baz content test3\n");
$this->setMockHandler(['fwrite', 'streamGetMetadata']);
$this->assertEquals("foo bar baz content test1\nfoo bar baz content test2\nfoo bar baz content test3\n", $this->socket->getOutput());
$callback = function ($arg) {
$map = [
'Hello world' => 6,
'world' => 5,
];
return $map[$arg];
};
$this->handler->expects($this->exactly(1))
->method('fwrite')
->will($this->returnCallback($callback));
$this->handler->expects($this->exactly(1))
->method('streamGetMetadata')
->will($this->returnValue(['timed_out' => true]));
$this->writeRecord('Hello world');
}
/**
* @expectedException RuntimeException
*/
public function testWriteFailsOnIncompleteWrite()
{
$this->setMockHandler(['fwrite', 'streamGetMetadata']);
$res = $this->res;
$callback = function ($string) use ($res) {
fclose($res);
return strlen('Hello');
};
$this->handler->expects($this->exactly(1))
->method('fwrite')
->will($this->returnCallback($callback));
$this->handler->expects($this->exactly(1))
->method('streamGetMetadata')
->will($this->returnValue(['timed_out' => false]));
$this->writeRecord('Hello world');
}
public function testWriteWithMemoryFile()
{
$this->setMockHandler();
$this->writeRecord('test1');
$this->writeRecord('test2');
$this->writeRecord('test3');
fseek($this->res, 0);
$this->assertEquals('test1test2test3', fread($this->res, 1024));
}
public function testWriteWithMock()
{
$this->setMockHandler(['fwrite']);
$callback = function ($arg) {
$map = [
'Hello world' => 6,
'world' => 5,
];
return $map[$arg];
};
$this->handler->expects($this->exactly(2))
->method('fwrite')
->will($this->returnCallback($callback));
$this->writeRecord('Hello world');
}
public function testClose()
{
$this->initHandlerAndSocket();
$this->setMockHandler();
$this->writeRecord('Hello world');
$reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'resource');
$reflectionProperty->setAccessible(true);
$this->assertInternalType('resource', $reflectionProperty->getValue($this->handler));
$this->assertInternalType('resource', $this->res);
$this->handler->close();
$this->assertFalse(is_resource($reflectionProperty->getValue($this->handler)), "Expected resource to be closed after closing handler");
$this->assertFalse(is_resource($this->res), "Expected resource to be closed after closing handler");
}
public function testCloseDoesNotClosePersistentSocket()
{
$this->initHandlerAndSocket();
$this->setMockHandler();
$this->handler->setPersistent(true);
$this->writeRecord('Hello world');
$reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'resource');
$reflectionProperty->setAccessible(true);
$this->assertTrue(is_resource($reflectionProperty->getValue($this->handler)));
$this->assertTrue(is_resource($this->res));
$this->handler->close();
$this->assertTrue(is_resource($reflectionProperty->getValue($this->handler)));
$this->assertTrue(is_resource($this->res));
}
/**
* @expectedException \RuntimeException
*/
public function testAvoidInfiniteLoopWhenNoDataIsWrittenForAWritingTimeoutSeconds()
{
$this->setMockHandler(['fwrite', 'streamGetMetadata']);
$this->handler->expects($this->any())
->method('fwrite')
->will($this->returnValue(0));
$this->handler->expects($this->any())
->method('streamGetMetadata')
->will($this->returnValue(['timed_out' => false]));
$this->handler->setWritingTimeout(1);
$this->writeRecord('Hello world');
}
private function createHandler($connectionString)
@@ -166,16 +273,38 @@ class SocketHandlerTest extends TestCase
$this->handler->handle($this->getRecord(Logger::WARNING, $string));
}
private function initHandlerAndSocket()
private function setMockHandler(array $methods = [])
{
$this->socket = LocalSocket::initSocket();
$this->res = fopen('php://memory', 'a');
$defaultMethods = ['fsockopen', 'pfsockopen', 'streamSetTimeout'];
$newMethods = array_diff($methods, $defaultMethods);
$finalMethods = array_merge($defaultMethods, $newMethods);
$this->handler = $this->getMockBuilder('Monolog\Handler\SocketHandler')
->setMethods($finalMethods)
->setConstructorArgs(['localhost:1234'])
->getMock();
if (!in_array('fsockopen', $methods)) {
$this->handler->expects($this->any())
->method('fsockopen')
->will($this->returnValue($this->res));
}
if (!in_array('pfsockopen', $methods)) {
$this->handler->expects($this->any())
->method('pfsockopen')
->will($this->returnValue($this->res));
}
if (!in_array('streamSetTimeout', $methods)) {
$this->handler->expects($this->any())
->method('streamSetTimeout')
->will($this->returnValue(true));
}
$this->handler = new SocketHandler('tcp://127.0.0.1:51984');
$this->handler->setFormatter($this->getIdentityFormatter());
}
public function tearDown()
{
unset($this->socket, $this->handler);
}
}