mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-21 08:36:33 +02:00
Fix a ton of tests for latest phpunit and turn faked-stream tests into real network tests with a local server
This commit is contained in:
@@ -26,11 +26,10 @@ class HipChatHandlerTest extends TestCase
|
||||
|
||||
public function testWriteV2()
|
||||
{
|
||||
$this->createHandler('myToken', 'room1', 'Monolog', false, 'hipchat.foo.bar', 'v2');
|
||||
$this->initHandlerAndSocket('myToken', 'room1', 'Monolog', false, 'hipchat.foo.bar', 'v2');
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
|
||||
$content = $this->socket->getOutput();
|
||||
$this->assertRegexp('/POST \/v2\/room\/room1\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
|
||||
|
||||
return $content;
|
||||
@@ -38,10 +37,9 @@ class HipChatHandlerTest extends TestCase
|
||||
|
||||
public function testWriteV2Notify()
|
||||
{
|
||||
$this->createHandler('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar', 'v2');
|
||||
$this->initHandlerAndSocket('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar', 'v2');
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
$content = $this->socket->getOutput();
|
||||
|
||||
$this->assertRegexp('/POST \/v2\/room\/room1\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
|
||||
|
||||
@@ -50,10 +48,9 @@ class HipChatHandlerTest extends TestCase
|
||||
|
||||
public function testRoomSpaces()
|
||||
{
|
||||
$this->createHandler('myToken', 'room name', 'Monolog', false, 'hipchat.foo.bar', 'v2');
|
||||
$this->initHandlerAndSocket('myToken', 'room name', 'Monolog', false, 'hipchat.foo.bar', 'v2');
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
$content = $this->socket->getOutput();
|
||||
|
||||
$this->assertRegexp('/POST \/v2\/room\/room%20name\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
|
||||
|
||||
@@ -94,10 +91,9 @@ class HipChatHandlerTest extends TestCase
|
||||
|
||||
public function testWriteContentV2WithoutName()
|
||||
{
|
||||
$this->createHandler('myToken', 'room1', null, false, 'hipchat.foo.bar', 'v2');
|
||||
$this->initHandlerAndSocket('myToken', 'room1', null, false, 'hipchat.foo.bar', 'v2');
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
$content = $this->socket->getOutput();
|
||||
|
||||
$this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content);
|
||||
|
||||
@@ -106,21 +102,19 @@ class HipChatHandlerTest extends TestCase
|
||||
|
||||
public function testWriteWithComplexMessage()
|
||||
{
|
||||
$this->createHandler();
|
||||
$this->initHandlerAndSocket();
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
|
||||
$content = $this->socket->getOutput();
|
||||
$this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
|
||||
}
|
||||
|
||||
public function testWriteTruncatesLongMessage()
|
||||
{
|
||||
$this->createHandler();
|
||||
$this->initHandlerAndSocket();
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, str_repeat('abcde', 2000)));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 12000);
|
||||
|
||||
$content = $this->socket->getOutput();
|
||||
$this->assertRegexp('/message='.str_repeat('abcde', 1900).'\+%5Btruncated%5D/', $content);
|
||||
}
|
||||
|
||||
@@ -129,11 +123,10 @@ class HipChatHandlerTest extends TestCase
|
||||
*/
|
||||
public function testWriteWithErrorLevelsAndColors($level, $expectedColor)
|
||||
{
|
||||
$this->createHandler();
|
||||
$this->initHandlerAndSocket();
|
||||
$this->handler->handle($this->getRecord($level, 'Backup of database "example" finished in 16 minutes.'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
|
||||
$content = $this->socket->getOutput();
|
||||
$this->assertRegexp('/color='.$expectedColor.'/', $content);
|
||||
}
|
||||
|
||||
@@ -156,13 +149,10 @@ class HipChatHandlerTest extends TestCase
|
||||
*/
|
||||
public function testHandleBatch($records, $expectedColor)
|
||||
{
|
||||
$this->createHandler();
|
||||
|
||||
$this->initHandlerAndSocket();
|
||||
$this->handler->handleBatch($records);
|
||||
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
|
||||
$content = $this->socket->getOutput();
|
||||
$this->assertRegexp('/color='.$expectedColor.'/', $content);
|
||||
}
|
||||
|
||||
@@ -200,36 +190,55 @@ class HipChatHandlerTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
private function createHandler($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com', $version = 'v1')
|
||||
{
|
||||
$constructorArgs = [$token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host, $version];
|
||||
$this->res = fopen('php://memory', 'a');
|
||||
$this->handler = $this->getMock(
|
||||
'\Monolog\Handler\HipChatHandler',
|
||||
['fsockopen', 'streamSetTimeout', 'closeSocket'],
|
||||
$constructorArgs
|
||||
);
|
||||
|
||||
$reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
|
||||
$reflectionProperty->setAccessible(true);
|
||||
$reflectionProperty->setValue($this->handler, 'localhost:1234');
|
||||
|
||||
$this->handler->expects($this->any())
|
||||
->method('fsockopen')
|
||||
->will($this->returnValue($this->res));
|
||||
$this->handler->expects($this->any())
|
||||
->method('streamSetTimeout')
|
||||
->will($this->returnValue(true));
|
||||
$this->handler->expects($this->any())
|
||||
->method('closeSocket')
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->handler->setFormatter($this->getIdentityFormatter());
|
||||
}
|
||||
|
||||
public function testCreateWithTooLongNameV2()
|
||||
{
|
||||
// creating a handler with too long of a name but using the v2 api doesn't matter.
|
||||
$hipChatHandler = new HipChatHandler('token', 'room', 'SixteenCharsHere', false, Logger::CRITICAL, true, true, 'test', 'api.hipchat.com', 'v2');
|
||||
}
|
||||
|
||||
private function initHandlerAndSocket($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com', $version = 'v1')
|
||||
{
|
||||
$tmpFile = sys_get_temp_dir().'/monolog-test-socket.php';
|
||||
file_put_contents($tmpFile, <<<'SCRIPT'
|
||||
<?php
|
||||
|
||||
$sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
|
||||
socket_bind($sock, '127.0.0.1', 51984);
|
||||
socket_listen($sock);
|
||||
|
||||
while (true) {
|
||||
$res = socket_accept($sock);
|
||||
socket_set_option($res, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 0, "usec" => 500));
|
||||
while ($read = socket_read($res, 1024)) {
|
||||
echo $read;
|
||||
}
|
||||
socket_close($res);
|
||||
}
|
||||
SCRIPT
|
||||
);
|
||||
|
||||
$this->socket = new \Symfony\Component\Process\Process(escapeshellarg(PHP_BINARY).' '.escapeshellarg($tmpFile));
|
||||
$this->socket->start();
|
||||
|
||||
$this->handler = new HipChatHandler($token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host, $version);
|
||||
|
||||
$reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString');
|
||||
$reflectionProperty->setAccessible(true);
|
||||
$reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
|
||||
|
||||
$this->handler->setFormatter($this->getIdentityFormatter());
|
||||
}
|
||||
|
||||
private function closeSocket()
|
||||
{
|
||||
$this->socket->stop();
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
if (isset($this->socket)) {
|
||||
$this->closeSocket();
|
||||
unset($this->socket);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user