1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-16 01:54:19 +02:00

Initial version

This commit is contained in:
Pablo Belloc
2012-02-25 20:37:04 -03:00
parent 6c15897ac7
commit 486cbb78ab
8 changed files with 516 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
<?php
namespace Monolog\Handler\SocketHandler;
class SocketTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException
*/
public function testInvalidHostname() {
$socket = new Socket('garbage://here');
$socket->write('data');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testBadConnectionTimeout()
{
$socket = new Socket('localhost:1234');
$socket->setConnectionTimeout(-1);
}
public function testSetConnectionTimeout()
{
$socket = new Socket('localhost:1234');
$socket->setConnectionTimeout(10);
$this->assertEquals(10, $socket->getConnectionTimeout());
}
/**
* @expectedException \InvalidArgumentException
*/
public function testBadTimeout()
{
$socket = new Socket('localhost:1234');
$socket->setTimeout(-1);
}
public function testSetTimeout()
{
$socket = new Socket('localhost:1234');
$socket->setTimeout(10);
$this->assertEquals(10, $socket->getTimeout());
}
public function testSetConnectionString()
{
$socket = new Socket('tcp://localhost:9090');
$this->assertEquals('tcp://localhost:9090', $socket->getConnectionString());
}
public function testConnectionRefuesed()
{
try {
$socket = new Socket('127.0.0.1:7894');
$socket->setTimeout(1);
$string = 'Hello world';
$socket->write($string);
$this->fail("Shoul not connect - are you running a server on 127.0.0.1:7894 ?");
} catch (\Monolog\Handler\SocketHandler\Exception\ConnectionException $e) {
}
}
/**
* @expectedException Monolog\Handler\SocketHandler\Exception\ConnectionException
*/
public function testConnectionTimeoutWithMock()
{
$socket = new MockSocket('localhost:54321');
$socket->setConnectionTimeout(10);
$socket->setFailConnectionTimeout(10);
$socket->write('Hello world');
}
/**
* @expectedException Monolog\Handler\SocketHandler\Exception\WriteToSocketException
*/
public function testWriteTimeoutWithMock()
{
$socket = new MockSocket('localhost:54321');
$socket->setTimeout(10);
$socket->setFailTimeout(10);
$socket->write('Hello world');
}
public function testWriteWithMock()
{
$socket = new MockSocket('localhost:54321');
$socket->write('Hello world');
$res = $socket->getResource();
fseek($res, 0);
$this->assertEquals('Hello world', fread($res, 1024));
}
public function testClose()
{
$resource = fopen('php://memory', 'a+');
$socket = new MockSocket($resource);
$this->assertTrue(is_resource($resource));
$socket->close();
$this->assertFalse(is_resource($resource));
}
/**
* @expectedException \InvalidArgumentException
*/
public function testInjectBadResourceThrowsException()
{
$socket = new Socket('');
$socket->setResource('');
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* @author Pablo de Leon Belloc <pablolb@gmail.com>
*/
namespace Monolog\Handler;
use Monolog\Handler\SocketHandler\MockSocket;
use Monolog\Handler\SocketHandler\Socket;
use Monolog\Handler\SocketHandler\PersistentSocket;
use Monolog\TestCase;
use Monolog\Logger;
class SocketHandlerTest extends TestCase
{
public function testWrite()
{
$socket = new MockSocket('localhost');
$handler = new SocketHandler('localhost');
$handler->setSocket($socket);
$handler->setFormatter($this->getIdentityFormatter());
$handler->handle($this->getRecord(Logger::WARNING, 'test'));
$handler->handle($this->getRecord(Logger::WARNING, 'test2'));
$handler->handle($this->getRecord(Logger::WARNING, 'test3'));
$handle = $socket->getResource();
fseek($handle, 0);
$this->assertEquals('testtest2test3', fread($handle, 100));
}
public function testCloseClosesNonPersistentSocket()
{
$socket = new Socket('localhost');
$res = fopen('php://memory', 'a');
$socket->setResource($res);
$handler = new SocketHandler('localhost');
$handler->setSocket($socket);
$handler->close();
$this->assertFalse($socket->isConnected());
}
public function testCloseDoesNotClosePersistentSocket()
{
$socket = new PersistentSocket('localhost');
$res = fopen('php://memory', 'a');
$socket->setResource($res);
$handler = new SocketHandler('localhost');
$handler->setSocket($socket);
$handler->close();
$this->assertTrue($socket->isConnected());
}
}