1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-02-24 06:52:34 +01:00
php-monolog/tests/Monolog/Handler/SocketHandlerTest.php

285 lines
7.7 KiB
PHP
Raw Normal View History

2012-02-25 20:37:04 -03:00
<?php
/*
* 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;
/**
* @author Pablo de Leon Belloc <pablolb@gmail.com>
*/
2012-02-25 20:37:04 -03:00
class SocketHandlerTest extends TestCase
{
/**
* @var Monolog\Handler\SocketHandler
*/
private $handler;
2012-02-26 14:17:19 -03:00
/**
* @var resource
*/
private $res;
2012-02-26 14:17:19 -03:00
/**
* @expectedException UnexpectedValueException
*/
2012-02-26 14:17:19 -03:00
public function testInvalidHostname()
{
$this->createHandler('garbage://here');
$this->writeRecord('data');
}
2012-02-26 14:17:19 -03:00
/**
* @expectedException \InvalidArgumentException
*/
public function testBadConnectionTimeout()
{
$this->createHandler('localhost:1234');
$this->handler->setConnectionTimeout(-1);
}
2012-02-26 14:17:19 -03:00
public function testSetConnectionTimeout()
{
$this->createHandler('localhost:1234');
$this->handler->setConnectionTimeout(10);
$this->assertEquals(10, $this->handler->getConnectionTimeout());
}
2012-02-26 14:17:19 -03:00
/**
* @expectedException \InvalidArgumentException
*/
public function testBadTimeout()
{
$this->createHandler('localhost:1234');
$this->handler->setTimeout(-1);
}
2012-02-26 14:17:19 -03:00
public function testSetTimeout()
{
$this->createHandler('localhost:1234');
$this->handler->setTimeout(10);
$this->assertEquals(10, $this->handler->getTimeout());
}
2012-02-26 14:17:19 -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
/**
* @expectedException UnexpectedValueException
*/
2012-02-27 12:38:06 -03:00
public function testExceptionIsThrownOnFsockopenError()
{
2012-02-27 12:38:06 -03:00
$this->setMockHandler(array('fsockopen'));
$this->handler->expects($this->once())
->method('fsockopen')
->will($this->returnValue(false));
2012-02-27 12:38:06 -03:00
$this->writeRecord('Hello world');
}
/**
* @expectedException UnexpectedValueException
2012-02-27 12:38:06 -03:00
*/
public function testExceptionIsThrownOnPfsockopenError()
{
$this->setMockHandler(array('pfsockopen'));
$this->handler->expects($this->once())
->method('pfsockopen')
->will($this->returnValue(false));
2012-02-27 12:38:06 -03:00
$this->handler->setPersistent(true);
$this->writeRecord('Hello world');
}
/**
* @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')
->will($this->returnValue(false));
$this->writeRecord('Hello world');
}
2012-02-26 14:17:19 -03:00
/**
* @expectedException RuntimeException
*/
public function testWriteFailsOnIfFwriteReturnsFalse()
{
$this->setMockHandler(array('fwrite'));
$callback = function($arg)
{
$map = array(
'Hello world' => 6,
'world' => false,
);
return $map[$arg];
};
2012-02-26 14:17:19 -03:00
$this->handler->expects($this->exactly(2))
->method('fwrite')
->will($this->returnCallback($callback));
2012-02-26 14:17:19 -03:00
$this->writeRecord('Hello world');
}
2012-02-26 14:17:19 -03:00
/**
* @expectedException RuntimeException
*/
public function testWriteFailsIfStreamTimesOut()
{
2012-04-22 16:36:31 +02:00
$this->setMockHandler(array('fwrite', 'streamGetMetadata'));
$callback = function($arg)
{
$map = array(
'Hello world' => 6,
'world' => 5,
);
return $map[$arg];
};
2012-02-26 14:17:19 -03:00
$this->handler->expects($this->exactly(1))
->method('fwrite')
->will($this->returnCallback($callback));
$this->handler->expects($this->exactly(1))
2012-04-22 16:36:31 +02:00
->method('streamGetMetadata')
->will($this->returnValue(array('timed_out' => true)));
2012-02-26 14:17:19 -03:00
$this->writeRecord('Hello world');
}
2012-02-26 14:17:19 -03:00
/**
* @expectedException RuntimeException
*/
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;
$callback = function($string) use ($res)
{
fclose($res);
return strlen('Hello');
};
2012-02-26 14:17:19 -03:00
$this->handler->expects($this->exactly(1))
->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')
->will($this->returnValue(array('timed_out' => false)));
2012-02-26 14:17:19 -03:00
$this->writeRecord('Hello world');
2012-02-25 20:37:04 -03:00
}
2012-02-26 14:17:19 -03:00
public function testWriteWithMemoryFile()
2012-02-25 20:37:04 -03:00
{
2012-02-27 12:38:06 -03:00
$this->setMockHandler();
$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
public function testWriteWithMock()
{
$this->setMockHandler(array('fwrite'));
$callback = function($arg)
{
$map = array(
'Hello world' => 6,
'world' => 5,
);
return $map[$arg];
};
2012-02-26 14:17:19 -03:00
$this->handler->expects($this->exactly(2))
->method('fwrite')
->will($this->returnCallback($callback));
2012-02-26 14:17:19 -03:00
$this->writeRecord('Hello world');
}
2012-02-26 14:17:19 -03:00
public function testClose()
{
2012-02-27 12:38:06 -03:00
$this->setMockHandler();
$this->writeRecord('Hello world');
$this->assertInternalType('resource', $this->res);
$this->handler->close();
$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();
$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
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())
{
$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);
$this->handler = $this->getMock(
'\Monolog\Handler\SocketHandler', $finalMethods, array('localhost:1234')
);
2012-02-27 12:38:06 -03:00
if (!in_array('fsockopen', $methods)) {
$this->handler->expects($this->any())
->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())
->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')
->will($this->returnValue(true));
2012-02-27 12:38:06 -03:00
}
$this->handler->setFormatter($this->getIdentityFormatter());
}
2012-02-25 20:37:04 -03:00
}