mirror of
https://github.com/Seldaek/monolog.git
synced 2025-07-31 18:30:15 +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:
@@ -21,28 +21,28 @@ class UdpSocketTest extends TestCase
|
||||
{
|
||||
public function testWeDoNotTruncateShortMessages()
|
||||
{
|
||||
$socket = $this->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', ['send'], ['lol', 'lol']);
|
||||
|
||||
$socket->expects($this->at(0))
|
||||
->method('send')
|
||||
->with("HEADER: The quick brown fox jumps over the lazy dog");
|
||||
$this->initSocket();
|
||||
|
||||
$socket = new UdpSocket('127.0.0.1', 51984);
|
||||
$socket->write("The quick brown fox jumps over the lazy dog", "HEADER: ");
|
||||
|
||||
$this->closeSocket();
|
||||
$this->assertEquals('HEADER: The quick brown fox jumps over the lazy dog', $this->socket->getOutput());
|
||||
}
|
||||
|
||||
public function testLongMessagesAreTruncated()
|
||||
{
|
||||
$socket = $this->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', ['send'], ['lol', 'lol']);
|
||||
$this->initSocket();
|
||||
|
||||
$socket = new UdpSocket('127.0.0.1', 51984);
|
||||
|
||||
$longString = str_repeat("derp", 20000);
|
||||
$socket->write($longString, "HEADER");
|
||||
|
||||
$truncatedString = str_repeat("derp", 16254).'d';
|
||||
|
||||
$socket->expects($this->exactly(1))
|
||||
->method('send')
|
||||
->with("HEADER" . $truncatedString);
|
||||
|
||||
$longString = str_repeat("derp", 20000);
|
||||
|
||||
$socket->write($longString, "HEADER");
|
||||
$this->closeSocket();
|
||||
$this->assertEquals('HEADER'.$truncatedString, $this->socket->getOutput());
|
||||
}
|
||||
|
||||
public function testDoubleCloseDoesNotError()
|
||||
@@ -61,4 +61,46 @@ class UdpSocketTest extends TestCase
|
||||
$socket->close();
|
||||
$socket->write('foo', "HEADER");
|
||||
}
|
||||
|
||||
private function initSocket()
|
||||
{
|
||||
$tmpFile = sys_get_temp_dir().'/monolog-test-socket.php';
|
||||
file_put_contents($tmpFile, <<<'SCRIPT'
|
||||
<?php
|
||||
|
||||
$sock = socket_create(AF_INET, SOCK_DGRAM, getprotobyname('udp'));
|
||||
socket_bind($sock, '127.0.0.1', 51984);
|
||||
echo 'INIT';
|
||||
while (true) {
|
||||
socket_recvfrom($sock, $read, 100*1024, 0, $ip, $port);
|
||||
echo $read;
|
||||
}
|
||||
SCRIPT
|
||||
);
|
||||
|
||||
$this->socket = new \Symfony\Component\Process\Process(escapeshellarg(PHP_BINARY).' '.escapeshellarg($tmpFile));
|
||||
$this->socket->start();
|
||||
while (true) {
|
||||
if ($this->socket->getOutput() === 'INIT') {
|
||||
$this->socket->clearOutput();
|
||||
break;
|
||||
}
|
||||
usleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
private function closeSocket()
|
||||
{
|
||||
usleep(100);
|
||||
$this->socket->stop();
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
if (isset($this->socket)) {
|
||||
$this->closeSocket();
|
||||
unset($this->socket);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user