mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-07 13:46:38 +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:
@@ -40,36 +40,32 @@ class SlackHandlerTest extends TestCase
|
||||
|
||||
public function testWriteHeader()
|
||||
{
|
||||
$this->createHandler();
|
||||
$this->initHandlerAndSocket();
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
|
||||
$content = $this->closeSocket();
|
||||
$this->assertRegexp('/POST \/api\/chat.postMessage HTTP\/1.1\\r\\nHost: slack.com\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
|
||||
}
|
||||
|
||||
public function testWriteContent()
|
||||
{
|
||||
$this->createHandler();
|
||||
$this->initHandlerAndSocket();
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
|
||||
$content = $this->closeSocket();
|
||||
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
|
||||
}
|
||||
|
||||
public function testWriteContentUsesFormatterIfProvided()
|
||||
{
|
||||
$this->createHandler('myToken', 'channel1', 'Monolog', false);
|
||||
$this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', false);
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
$content = $this->closeSocket();
|
||||
|
||||
$this->createHandler('myToken', 'channel1', 'Monolog', false);
|
||||
$this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', false);
|
||||
$this->handler->setFormatter(new LineFormatter('foo--%message%'));
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test2'));
|
||||
fseek($this->res, 0);
|
||||
$content2 = fread($this->res, 1024);
|
||||
$content2 = $this->closeSocket();
|
||||
|
||||
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=test1.*$/', $content);
|
||||
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=foo--test2.*$/', $content2);
|
||||
@@ -77,11 +73,10 @@ class SlackHandlerTest extends TestCase
|
||||
|
||||
public function testWriteContentWithEmoji()
|
||||
{
|
||||
$this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
|
||||
$this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', true, 'alien');
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
|
||||
$content = $this->closeSocket();
|
||||
$this->assertRegexp('/icon_emoji=%3Aalien%3A$/', $content);
|
||||
}
|
||||
|
||||
@@ -90,21 +85,19 @@ class SlackHandlerTest extends TestCase
|
||||
*/
|
||||
public function testWriteContentWithColors($level, $expectedColor)
|
||||
{
|
||||
$this->createHandler();
|
||||
$this->initHandlerAndSocket();
|
||||
$this->handler->handle($this->getRecord($level, 'test1'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
|
||||
$content = $this->closeSocket();
|
||||
$this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content);
|
||||
}
|
||||
|
||||
public function testWriteContentWithPlainTextMessage()
|
||||
{
|
||||
$this->createHandler('myToken', 'channel1', 'Monolog', false);
|
||||
$this->initHandlerAndSocket('myToken', 'channel1', 'Monolog', false);
|
||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||
fseek($this->res, 0);
|
||||
$content = fread($this->res, 1024);
|
||||
|
||||
$content = $this->closeSocket();
|
||||
$this->assertRegexp('/text=test1/', $content);
|
||||
}
|
||||
|
||||
@@ -122,30 +115,51 @@ class SlackHandlerTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
|
||||
private function initHandlerAndSocket($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeExtra = false)
|
||||
{
|
||||
$constructorArgs = [$token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true, $useShortAttachment, $includeExtra];
|
||||
$this->res = fopen('php://memory', 'a');
|
||||
$this->handler = $this->getMock(
|
||||
'\Monolog\Handler\SlackHandler',
|
||||
['fsockopen', 'streamSetTimeout', 'closeSocket'],
|
||||
$constructorArgs
|
||||
);
|
||||
$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 SlackHandler($token, $channel, $username, $useAttachment, $iconEmoji, Logger::DEBUG, true, $useShortAttachment, $includeExtra);
|
||||
|
||||
$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));
|
||||
$reflectionProperty->setValue($this->handler, '127.0.0.1:51984');
|
||||
|
||||
$this->handler->setFormatter($this->getIdentityFormatter());
|
||||
}
|
||||
|
||||
private function closeSocket()
|
||||
{
|
||||
$this->socket->stop();
|
||||
|
||||
return $this->socket->getOutput();
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
if (isset($this->socket)) {
|
||||
$this->closeSocket();
|
||||
unset($this->socket);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user