1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-04 04:07:39 +02:00

Remove old SocketTest. Fix CS issues

This commit is contained in:
Pablo Belloc
2012-02-26 14:17:19 -03:00
parent cc9c02250c
commit 722cabc535
5 changed files with 92 additions and 93 deletions

View File

@@ -15,7 +15,6 @@ use Monolog\Logger;
use Monolog\Handler\SocketHandler\Exception\ConnectionException; use Monolog\Handler\SocketHandler\Exception\ConnectionException;
use Monolog\Handler\SocketHandler\Exception\WriteToSocketException; use Monolog\Handler\SocketHandler\Exception\WriteToSocketException;
/** /**
* Stores to any socket - uses fsockopen() or pfsockopen(). * Stores to any socket - uses fsockopen() or pfsockopen().
* *
@@ -24,12 +23,13 @@ use Monolog\Handler\SocketHandler\Exception\WriteToSocketException;
*/ */
class SocketHandler extends AbstractProcessingHandler class SocketHandler extends AbstractProcessingHandler
{ {
private $connectionString; private $connectionString;
private $connectionTimeout; private $connectionTimeout;
private $resource; private $resource;
private $timeout = 0; private $timeout = 0;
private $persistent = false; private $persistent = false;
/** /**
* @param string $connectionString * @param string $connectionString
* @param integer $level The minimum logging level at which this handler will be triggered * @param integer $level The minimum logging level at which this handler will be triggered
@@ -39,9 +39,9 @@ class SocketHandler extends AbstractProcessingHandler
{ {
parent::__construct($level, $bubble); parent::__construct($level, $bubble);
$this->connectionString = $connectionString; $this->connectionString = $connectionString;
$this->connectionTimeout = (float)ini_get('default_socket_timeout'); $this->connectionTimeout = (float) ini_get('default_socket_timeout');
} }
/** /**
* Connect (if necessary) and write to the socket * Connect (if necessary) and write to the socket
* *
@@ -54,7 +54,7 @@ class SocketHandler extends AbstractProcessingHandler
$this->connectIfNotConnected(); $this->connectIfNotConnected();
$this->writeToSocket((string) $record['formatted']); $this->writeToSocket((string) $record['formatted']);
} }
/** /**
* We will not close a PersistentSocket instance so it can be reused in other requests. * We will not close a PersistentSocket instance so it can be reused in other requests.
*/ */
@@ -65,7 +65,7 @@ class SocketHandler extends AbstractProcessingHandler
} }
$this->closeSocket(); $this->closeSocket();
} }
public function closeSocket() public function closeSocket()
{ {
if (is_resource($this->resource)) { if (is_resource($this->resource)) {
@@ -73,12 +73,12 @@ class SocketHandler extends AbstractProcessingHandler
$this->resource = null; $this->resource = null;
} }
} }
public function setPersistent($boolean) public function setPersistent($boolean)
{ {
$this->persistent = (boolean)$boolean; $this->persistent = (boolean) $boolean;
} }
/** /**
* Set connection timeout. Only has effect before we connect. * Set connection timeout. Only has effect before we connect.
* *
@@ -88,9 +88,9 @@ class SocketHandler extends AbstractProcessingHandler
public function setConnectionTimeout($seconds) public function setConnectionTimeout($seconds)
{ {
$this->validateTimeout($seconds); $this->validateTimeout($seconds);
$this->connectionTimeout = (float)$seconds; $this->connectionTimeout = (float) $seconds;
} }
/** /**
* Set write timeout. Only has effect before we connect. * Set write timeout. Only has effect before we connect.
* *
@@ -100,37 +100,39 @@ class SocketHandler extends AbstractProcessingHandler
public function setTimeout($seconds) public function setTimeout($seconds)
{ {
$this->validateTimeout($seconds); $this->validateTimeout($seconds);
$this->timeout = (int)$seconds; $this->timeout = (int) $seconds;
} }
private function validateTimeout($value) private function validateTimeout($value)
{ {
$ok = filter_var($value, FILTER_VALIDATE_INT, array('options' => array( $ok = filter_var($value, FILTER_VALIDATE_INT, array('options' => array(
'min_range' => 0, 'min_range' => 0,
))); )));
if ($ok === false) { if ($ok === false) {
throw new \InvalidArgumentException("Timeout must be 0 or a positive integer (got $value)"); throw new \InvalidArgumentException("Timeout must be 0 or a positive integer (got $value)");
} }
} }
public function getConnectionString() public function getConnectionString()
{ {
return $this->connectionString; return $this->connectionString;
} }
public function isPersistent() public function isPersistent()
{ {
return $this->persistent; return $this->persistent;
} }
public function getConnectionTimeout() { public function getConnectionTimeout()
{
return $this->connectionTimeout; return $this->connectionTimeout;
} }
public function getTimeout() { public function getTimeout()
{
return $this->timeout; return $this->timeout;
} }
/** /**
* Allow injecting a resource opened somewhere else. Used in tests. * Allow injecting a resource opened somewhere else. Used in tests.
* *
@@ -153,7 +155,7 @@ class SocketHandler extends AbstractProcessingHandler
} }
$this->connect(); $this->connect();
} }
/** /**
* Check to see if the socket is currently available. * Check to see if the socket is currently available.
* *
@@ -164,15 +166,15 @@ class SocketHandler extends AbstractProcessingHandler
public function isConnected() public function isConnected()
{ {
return is_resource($this->resource) return is_resource($this->resource)
&& !feof($this->resource); // on TCP - other party can close connection. && !feof($this->resource); // on TCP - other party can close connection.
} }
private function connect() private function connect()
{ {
$this->createSocketResource(); $this->createSocketResource();
$this->setSocketTimeout(); $this->setSocketTimeout();
} }
protected function createSocketResource() protected function createSocketResource()
{ {
if ($this->persistent) { if ($this->persistent) {
@@ -185,14 +187,14 @@ class SocketHandler extends AbstractProcessingHandler
} }
$this->resource = $resource; $this->resource = $resource;
} }
private function setSocketTimeout() private function setSocketTimeout()
{ {
if (!stream_set_timeout($this->resource, $this->timeout)) { if (!stream_set_timeout($this->resource, $this->timeout)) {
throw new ConnectionException("Failed setting timeout with stream_set_timeout()"); throw new ConnectionException("Failed setting timeout with stream_set_timeout()");
} }
} }
protected function writeToSocket($data) protected function writeToSocket($data)
{ {
$length = strlen($data); $length = strlen($data);
@@ -212,7 +214,7 @@ class SocketHandler extends AbstractProcessingHandler
throw new WriteToSocketException("End-of-file reached, probably we got disconnected (sent $sent of $length)"); throw new WriteToSocketException("End-of-file reached, probably we got disconnected (sent $sent of $length)");
} }
} }
/** /**
* Allow mock * Allow mock
*/ */
@@ -220,7 +222,7 @@ class SocketHandler extends AbstractProcessingHandler
{ {
return @fwrite($this->resource, $data); return @fwrite($this->resource, $data);
} }
/** /**
* Allow mock * Allow mock
*/ */
@@ -228,4 +230,5 @@ class SocketHandler extends AbstractProcessingHandler
{ {
return stream_get_meta_data($this->resource); return stream_get_meta_data($this->resource);
} }
} }

View File

@@ -13,4 +13,5 @@ namespace Monolog\Handler\SocketHandler\Exception;
class ConnectionException extends \RuntimeException class ConnectionException extends \RuntimeException
{ {
} }

View File

@@ -13,4 +13,5 @@ namespace Monolog\Handler\SocketHandler\Exception;
class WriteToSocketException extends \RuntimeException class WriteToSocketException extends \RuntimeException
{ {
} }

View File

@@ -1,8 +0,0 @@
<?php
namespace Monolog\Handler\SocketHandler;
class SocketTest extends \PHPUnit_Framework_TestCase
{
}

View File

@@ -21,24 +21,26 @@ use Monolog\Handler\SocketHandler\Exception\WriteToSocketException;
*/ */
class SocketHandlerTest extends TestCase class SocketHandlerTest extends TestCase
{ {
/** /**
* @var Monolog\Handler\SocketHandler * @var Monolog\Handler\SocketHandler
*/ */
private $handler; private $handler;
/** /**
* @var resource * @var resource
*/ */
private $res; private $res;
/** /**
* @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException * @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException
*/ */
public function testInvalidHostname() { public function testInvalidHostname()
{
$this->createHandler('garbage://here'); $this->createHandler('garbage://here');
$this->writeRecord('data'); $this->writeRecord('data');
} }
/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */
@@ -47,14 +49,14 @@ class SocketHandlerTest extends TestCase
$this->createHandler('localhost:1234'); $this->createHandler('localhost:1234');
$this->handler->setConnectionTimeout(-1); $this->handler->setConnectionTimeout(-1);
} }
public function testSetConnectionTimeout() public function testSetConnectionTimeout()
{ {
$this->createHandler('localhost:1234'); $this->createHandler('localhost:1234');
$this->handler->setConnectionTimeout(10); $this->handler->setConnectionTimeout(10);
$this->assertEquals(10, $this->handler->getConnectionTimeout()); $this->assertEquals(10, $this->handler->getConnectionTimeout());
} }
/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */
@@ -63,20 +65,20 @@ class SocketHandlerTest extends TestCase
$this->createHandler('localhost:1234'); $this->createHandler('localhost:1234');
$this->handler->setTimeout(-1); $this->handler->setTimeout(-1);
} }
public function testSetTimeout() public function testSetTimeout()
{ {
$this->createHandler('localhost:1234'); $this->createHandler('localhost:1234');
$this->handler->setTimeout(10); $this->handler->setTimeout(10);
$this->assertEquals(10, $this->handler->getTimeout()); $this->assertEquals(10, $this->handler->getTimeout());
} }
public function testSetConnectionString() public function testSetConnectionString()
{ {
$this->createHandler('tcp://localhost:9090'); $this->createHandler('tcp://localhost:9090');
$this->assertEquals('tcp://localhost:9090', $this->handler->getConnectionString()); $this->assertEquals('tcp://localhost:9090', $this->handler->getConnectionString());
} }
public function testConnectionRefuesed() public function testConnectionRefuesed()
{ {
try { try {
@@ -85,9 +87,10 @@ class SocketHandlerTest extends TestCase
$this->writeRecord($string); $this->writeRecord($string);
$this->fail("Shoul not connect - are you running a server on 127.0.0.1:7894 ?"); $this->fail("Shoul not connect - are you running a server on 127.0.0.1:7894 ?");
} catch (\Monolog\Handler\SocketHandler\Exception\ConnectionException $e) { } catch (\Monolog\Handler\SocketHandler\Exception\ConnectionException $e) {
} }
} }
/** /**
* @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException * @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException
*/ */
@@ -95,84 +98,84 @@ class SocketHandlerTest extends TestCase
{ {
$this->setMockHandler(array('createSocketResource')); $this->setMockHandler(array('createSocketResource'));
$this->handler->expects($this->once()) $this->handler->expects($this->once())
->method('createSocketResource') ->method('createSocketResource')
->will($this->throwException(new ConnectionException())); ->will($this->throwException(new ConnectionException()));
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
/** /**
* @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
*/ */
public function testWriteFailsOnIfFwriteReturnsFalse() public function testWriteFailsOnIfFwriteReturnsFalse()
{ {
$this->setMockHandler(array('fwrite')); $this->setMockHandler(array('fwrite'));
$map = array( $map = array(
array('Hello world', 6), array('Hello world', 6),
array('world', false), array('world', false),
); );
$this->handler->expects($this->exactly(2)) $this->handler->expects($this->exactly(2))
->method('fwrite') ->method('fwrite')
->will($this->returnValueMap($map)); ->will($this->returnValueMap($map));
$this->injectMemoryResource(); $this->injectMemoryResource();
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
/** /**
* @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
*/ */
public function testWriteFailsIfStreamTimesOut() public function testWriteFailsIfStreamTimesOut()
{ {
$this->setMockHandler(array('fwrite', 'stream_get_meta_data')); $this->setMockHandler(array('fwrite', 'stream_get_meta_data'));
$map = array( $map = array(
array('Hello world', 6), array('Hello world', 6),
array('world', 5), array('world', 5),
); );
$this->handler->expects($this->exactly(1)) $this->handler->expects($this->exactly(1))
->method('fwrite') ->method('fwrite')
->will($this->returnValueMap($map)); ->will($this->returnValueMap($map));
$this->handler->expects($this->exactly(1)) $this->handler->expects($this->exactly(1))
->method('stream_get_meta_data') ->method('stream_get_meta_data')
->will($this->returnValue(array('timed_out' => true))); ->will($this->returnValue(array('timed_out' => true)));
$this->injectMemoryResource(); $this->injectMemoryResource();
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
/** /**
* @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException * @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
*/ */
public function testWriteFailsOnIncompleteWrite() public function testWriteFailsOnIncompleteWrite()
{ {
$this->setMockHandler(array('fwrite', 'isConnected')); $this->setMockHandler(array('fwrite', 'isConnected'));
$map = array( $map = array(
array('Hello world', 6), array('Hello world', 6),
array('world', 5), array('world', 5),
); );
$this->handler->expects($this->exactly(1)) $this->handler->expects($this->exactly(1))
->method('fwrite') ->method('fwrite')
->will($this->returnValueMap($map)); ->will($this->returnValueMap($map));
$this->handler->expects($this->at(0)) $this->handler->expects($this->at(0))
->method('isConnected') ->method('isConnected')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$this->handler->expects($this->at(1)) $this->handler->expects($this->at(1))
->method('isConnected') ->method('isConnected')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$this->handler->expects($this->at(2)) $this->handler->expects($this->at(2))
->method('isConnected') ->method('isConnected')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$this->injectMemoryResource(); $this->injectMemoryResource();
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
public function testWriteWithMemoryFile() public function testWriteWithMemoryFile()
{ {
$this->createHandler('localhost:54321'); $this->createHandler('localhost:54321');
@@ -183,24 +186,24 @@ class SocketHandlerTest extends TestCase
fseek($this->res, 0); fseek($this->res, 0);
$this->assertEquals('test1test2test3', fread($this->res, 1024)); $this->assertEquals('test1test2test3', fread($this->res, 1024));
} }
public function testWriteWithMock() public function testWriteWithMock()
{ {
$this->setMockHandler(array('fwrite')); $this->setMockHandler(array('fwrite'));
$map = array( $map = array(
array('Hello world', 6), array('Hello world', 6),
array('world', 5), array('world', 5),
); );
$this->handler->expects($this->exactly(2)) $this->handler->expects($this->exactly(2))
->method('fwrite') ->method('fwrite')
->will($this->returnValueMap($map)); ->will($this->returnValueMap($map));
$this->injectMemoryResource(); $this->injectMemoryResource();
$this->writeRecord('Hello world'); $this->writeRecord('Hello world');
} }
public function testClose() public function testClose()
{ {
$this->createHandler('localhost:54321'); $this->createHandler('localhost:54321');
@@ -210,7 +213,7 @@ class SocketHandlerTest extends TestCase
$this->handler->close(); $this->handler->close();
$this->assertFalse(is_resource($this->res)); $this->assertFalse(is_resource($this->res));
} }
public function testCloseDoesNotClosePersistentSocket() public function testCloseDoesNotClosePersistentSocket()
{ {
$this->createHandler('localhost:54321'); $this->createHandler('localhost:54321');
@@ -230,31 +233,30 @@ class SocketHandlerTest extends TestCase
$this->createHandler(''); $this->createHandler('');
$this->handler->setResource(''); $this->handler->setResource('');
} }
private function createHandler($connectionString) private function createHandler($connectionString)
{ {
$this->handler = new SocketHandler($connectionString); $this->handler = new SocketHandler($connectionString);
$this->handler->setFormatter($this->getIdentityFormatter()); $this->handler->setFormatter($this->getIdentityFormatter());
} }
private function writeRecord($string) private function writeRecord($string)
{ {
$this->handler->handle($this->getRecord(Logger::WARNING, $string)); $this->handler->handle($this->getRecord(Logger::WARNING, $string));
} }
private function injectMemoryResource() private function injectMemoryResource()
{ {
$this->res = fopen('php://memory', 'a'); $this->res = fopen('php://memory', 'a');
$this->handler->setResource($this->res); $this->handler->setResource($this->res);
} }
private function setMockHandler(array $methods) private function setMockHandler(array $methods)
{ {
$this->handler = $this->getMock( $this->handler = $this->getMock(
'\Monolog\Handler\SocketHandler', '\Monolog\Handler\SocketHandler', $methods, array('localhost:1234')
$methods,
array('localhost:1234')
); );
$this->handler->setFormatter($this->getIdentityFormatter()); $this->handler->setFormatter($this->getIdentityFormatter());
} }
} }