diff --git a/composer.json b/composer.json index 1bfff6f..9605c78 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,8 @@ "require-dev": { "phpunit/phpunit": "^9.6|^10.1", "squizlabs/php_codesniffer": "^3.6", - "vimeo/psalm": "^5.11" + "vimeo/psalm": "^5.11", + "ext-zlib": "*" }, "provide": { "psr/log-implementation": "3.0.0" diff --git a/tests/Gelf/Test/Encoder/CompressedJsonEncoderTest.php b/tests/Gelf/Test/Encoder/CompressedJsonEncoderTest.php index ebba1a0..13fc346 100644 --- a/tests/Gelf/Test/Encoder/CompressedJsonEncoderTest.php +++ b/tests/Gelf/Test/Encoder/CompressedJsonEncoderTest.php @@ -36,7 +36,7 @@ public function testEncode() $this->message ->expects($this->once()) ->method('toArray') - ->will($this->returnValue($testData)); + ->willReturn($testData); $bytes = $this->encoder->encode($this->message); diff --git a/tests/Gelf/Test/Encoder/JsonEncoderTest.php b/tests/Gelf/Test/Encoder/JsonEncoderTest.php index 0b79dfa..fcd4c7b 100644 --- a/tests/Gelf/Test/Encoder/JsonEncoderTest.php +++ b/tests/Gelf/Test/Encoder/JsonEncoderTest.php @@ -35,7 +35,7 @@ public function testEncode(): void $this->message ->expects($this->once()) ->method('toArray') - ->will($this->returnValue($testData)); + ->willReturn($testData); $json = $this->encoder->encode($this->message); @@ -53,7 +53,7 @@ public function testUnicodeEncode(): void $this->message ->expects($this->once()) ->method('toArray') - ->will($this->returnValue($testData)); + ->willReturn($testData); $json = $this->encoder->encode($this->message); diff --git a/tests/Gelf/Test/LoggerTest.php b/tests/Gelf/Test/LoggerTest.php index 9b8c5a5..033222a 100644 --- a/tests/Gelf/Test/LoggerTest.php +++ b/tests/Gelf/Test/LoggerTest.php @@ -166,8 +166,8 @@ function (MessageInterface $message) { private function validatePublish(Closure $validator): void { - $this->publisher->expects($this->once())->method('publish')->will( - $this->returnCallback($validator) + $this->publisher->expects($this->once())->method('publish')->willReturnCallback( + $validator ); } diff --git a/tests/Gelf/Test/MessageTest.php b/tests/Gelf/Test/MessageTest.php index bc56746..e405820 100644 --- a/tests/Gelf/Test/MessageTest.php +++ b/tests/Gelf/Test/MessageTest.php @@ -66,13 +66,13 @@ public function testLevel(): void public function testLevelInvalidString(): void { - self::expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); $this->message->setLevel("invalid"); } public function testLevelInvalidInteger() { - self::expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); $this->message->setLevel(8); } @@ -85,13 +85,13 @@ public function testLogLevelToPsr(): void public function testLogLevelToPsrInvalidString(): void { - self::expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); Message::logLevelToPsr("invalid"); } public function testLogLevelToPsrInvalidInt(): void { - self::expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); Message::logLevelToPsr(-1); } @@ -134,13 +134,13 @@ public function testAdditionals(): void public function testSetAdditionalEmptyKey(): void { - self::expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); $this->message->setAdditional("", "test"); } public function testGetAdditionalInvalidKey(): void { - self::expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); $this->message->getAdditional("invalid"); } diff --git a/tests/Gelf/Test/MessageValidatorTest.php b/tests/Gelf/Test/MessageValidatorTest.php index 029229b..a4a1ad5 100644 --- a/tests/Gelf/Test/MessageValidatorTest.php +++ b/tests/Gelf/Test/MessageValidatorTest.php @@ -82,21 +82,19 @@ private function getMessage( ): MessageInterface { $msg = $this->createMock(MessageInterface::class); $msg->expects($this->any())->method('getHost') - ->will($this->returnValue($host)); + ->willReturn($host); $msg->expects($this->any())->method('getVersion') - ->will($this->returnValue($version)); + ->willReturn($version); $msg->expects($this->any())->method('getShortMessage') - ->will($this->returnValue($shortMessage)); + ->willReturn($shortMessage); $msg->expects($this->any())->method('getAllAdditionals') - ->will($this->returnValue($additionals)); + ->willReturn($additionals); $msg->expects($this->any())->method('hasAdditional') - ->will($this->returnCallback( - function ($key) use ($additionals) { - return isset($additionals[$key]); - } - )); + ->willReturnCallback(function ($key) use ($additionals) { + return isset($additionals[$key]); + }); return $msg; } diff --git a/tests/Gelf/Test/PublisherTest.php b/tests/Gelf/Test/PublisherTest.php index 8770a85..e1ca627 100644 --- a/tests/Gelf/Test/PublisherTest.php +++ b/tests/Gelf/Test/PublisherTest.php @@ -50,24 +50,24 @@ public function testPublish(): void $this->messageValidator->expects($this->once()) ->method('validate') - ->will($this->returnValue(true)); + ->willReturn(true); $this->publisher->publish($this->message); } public function testPublishErrorOnInvalid(): void { - self::expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); $this->messageValidator->expects($this->once()) ->method('validate') - ->will($this->returnValue(false)); + ->willReturn(false); $this->publisher->publish($this->message); } public function testMissingTransport(): void { - self::expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); $publisher = new Publisher(null, $this->messageValidator); self::assertCount(0, $publisher->getTransports()); @@ -88,7 +88,7 @@ public function testMultipleTransports(): void $this->messageValidator->expects($this->once()) ->method('validate') - ->will($this->returnValue(true)); + ->willReturn(true); $pub->publish($this->message); } diff --git a/tests/Gelf/Test/Transport/HttpTransportTest.php b/tests/Gelf/Test/Transport/HttpTransportTest.php index f9cb760..3d99e51 100644 --- a/tests/Gelf/Test/Transport/HttpTransportTest.php +++ b/tests/Gelf/Test/Transport/HttpTransportTest.php @@ -42,8 +42,8 @@ public function setUp(): void // create an encoder always return $testMessage $this->encoder = $this->createMock(EncoderInterface::class); - $this->encoder->expects($this->any())->method('encode')->will( - $this->returnValue($this->testMessage) + $this->encoder->expects($this->any())->method('encode')->willReturn( + $this->testMessage ); $this->transport = $this->getTransport(); @@ -83,13 +83,13 @@ public function testConstructor(): void public function testFromUrlConstructorInvalidUri(): void { - self::expectException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); HttpTransport::fromUrl('-://:-'); } public function testFromUrlConstructorInvalidScheme(): void { - self::expectException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); HttpTransport::fromUrl('ftp://foobar'); } @@ -152,7 +152,7 @@ public function testSslOptionsAreUsed(): void $sslOptions = $this->createMock(SslOptions::class); $sslOptions->expects($this->exactly(2)) ->method('toStreamContext') - ->will($this->returnValue(array('ssl' => null))); + ->willReturn(array('ssl' => null)); $transport = new HttpTransport("localhost", 12345, "/gelf", $sslOptions); @@ -174,8 +174,8 @@ public function testSetEncoder(): void public function testEmptyResponseException(): void { - self::expectException(RuntimeException::class); - self::expectExceptionMessage( + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage( "Graylog-Server didn't answer properly, expected 'HTTP/1.x 202 Accepted', response is ''" ); @@ -201,7 +201,7 @@ public function testSendUncompressed(): void $this->socketClient ->expects($this->once()) ->method("read") - ->will($this->returnValue("HTTP/1.1 202 Accepted\r\n\r\n")); + ->willReturn("HTTP/1.1 202 Accepted\r\n\r\n"); $this->transport->send($this->message); } @@ -212,17 +212,17 @@ public function testAuthentication(): void $this->socketClient->expects($this->once()) ->method("write") - ->will($this->returnCallback(function ($data) { + ->willReturnCallback(function ($data) { self::assertStringContainsString("Authorization: Basic " . base64_encode("test:test"), $data); return 1; - })); + }); $this->socketClient ->expects($this->once()) ->method("read") - ->will($this->returnValue("HTTP/1.1 202 Accepted\r\n\r\n")); + ->willReturn("HTTP/1.1 202 Accepted\r\n\r\n"); $this->transport->send($this->message); } @@ -265,14 +265,14 @@ public function testSendCompressed(): void $this->socketClient ->expects($this->once()) ->method("read") - ->will($this->returnValue("HTTP/1.1 202 Accepted\r\n\r\n")); + ->willReturn("HTTP/1.1 202 Accepted\r\n\r\n"); $compressedEncoder = $this->createMock(CompressedJsonEncoder::class); $compressedEncoder ->expects($this->any()) ->method('encode') - ->will( - $this->returnValue($this->testMessage) + ->willReturn( + $this->testMessage ); $this->transport->setMessageEncoder($compressedEncoder); @@ -284,7 +284,7 @@ public function testCloseSocketOnHttpOneZero(): void $this->socketClient ->expects($this->once()) ->method("read") - ->will($this->returnValue("HTTP/1.0 202 Accepted\r\n\r\n")); + ->willReturn("HTTP/1.0 202 Accepted\r\n\r\n"); $this->socketClient ->expects($this->once()) @@ -298,7 +298,7 @@ public function testCloseSocketOnConnectionClose(): void $this->socketClient ->expects($this->once()) ->method("read") - ->will($this->returnValue("HTTP/1.1 202 Accepted\r\nConnection: Close\r\n\r\n")); + ->willReturn("HTTP/1.1 202 Accepted\r\nConnection: Close\r\n\r\n"); $this->socketClient ->expects($this->once()) @@ -312,7 +312,7 @@ public function testConnectTimeout(): void $this->socketClient ->expects($this->once()) ->method('getConnectTimeout') - ->will($this->returnValue(123)); + ->willReturn(123); self::assertEquals(123, $this->transport->getConnectTimeout()); diff --git a/tests/Gelf/Test/Transport/KeepAliveRetryTransportWrapperTest.php b/tests/Gelf/Test/Transport/KeepAliveRetryTransportWrapperTest.php index b4ed92b..64447f8 100644 --- a/tests/Gelf/Test/Transport/KeepAliveRetryTransportWrapperTest.php +++ b/tests/Gelf/Test/Transport/KeepAliveRetryTransportWrapperTest.php @@ -31,7 +31,7 @@ public function testSendSuccess(): void $this->transport->expects($this->once()) ->method('send') ->with($this->message) - ->will($this->returnValue(42)); + ->willReturn(42); $bytes = $this->wrapper->send($this->message); @@ -57,8 +57,8 @@ public function testSendSuccessAfterRetry(): void public function testSendFailTwiceWithoutResponse(): void { - self::expectException(RuntimeException::class); - self::expectExceptionMessage("response is ''"); + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage("response is ''"); $expectedException1 = new RuntimeException(self::FAILURE_MESSAGE); $expectedException2 = new RuntimeException(self::FAILURE_MESSAGE); diff --git a/tests/Gelf/Test/Transport/SslOptionsTest.php b/tests/Gelf/Test/Transport/SslOptionsTest.php index 10fbe66..a46d552 100644 --- a/tests/Gelf/Test/Transport/SslOptionsTest.php +++ b/tests/Gelf/Test/Transport/SslOptionsTest.php @@ -97,7 +97,7 @@ public function testToStreamContextWithHostname(): void self::assertArrayNotHasKey('CN_match', $context['ssl']); self::assertArrayHasKey($sniPeerNameKey, $context['ssl']); - self::assertEquals(true, $context['ssl']['SNI_enabled']); + self::assertTrue($context['ssl']['SNI_enabled']); self::assertEquals($host, $context['ssl'][$sniPeerNameKey]); @@ -109,7 +109,7 @@ public function testToStreamContextWithHostname(): void self::assertArrayHasKey($peerNameKey, $context['ssl']); self::assertArrayHasKey($sniPeerNameKey, $context['ssl']); - self::assertEquals(true, $context['ssl']['SNI_enabled']); + self::assertTrue($context['ssl']['SNI_enabled']); self::assertEquals($host, $context['ssl'][$peerNameKey]); self::assertEquals($host, $context['ssl'][$sniPeerNameKey]); } diff --git a/tests/Gelf/Test/Transport/StreamSocketClientTcpTest.php b/tests/Gelf/Test/Transport/StreamSocketClientTcpTest.php index 19d0d1a..ec65772 100644 --- a/tests/Gelf/Test/Transport/StreamSocketClientTcpTest.php +++ b/tests/Gelf/Test/Transport/StreamSocketClientTcpTest.php @@ -75,11 +75,13 @@ public function testWrite(): void public function testBadWrite(): void { - self::expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); $this->socketClient->write("Hello "); - fclose($this->serverSocket); + self::assertTrue(fclose($this->serverSocket)); + $this->serverSocket = null; + usleep(100); $this->socketClient->write("world!"); } @@ -97,6 +99,9 @@ public function testMultiWrite(): void // open connection on server-socket $serverConnection = stream_socket_accept($this->serverSocket); + self::assertIsResource($serverConnection); + + usleep(100); $readData = fread($serverConnection, $numBytes); self::assertEquals($testData, $readData); @@ -108,10 +113,12 @@ public function testMultiWrite(): void $numBytes = $this->socketClient->write($testData); self::assertEquals(strlen($testData), $numBytes); + usleep(500); + $readData = fread($serverConnection, $numBytes); self::assertEquals($testData, $readData); - fclose($serverConnection); + self::assertTrue(fclose($serverConnection)); } public function testRead(): void @@ -122,14 +129,19 @@ public function testRead(): void self::assertEquals(strlen($testData), $numBytes); // lower timeout for server-socket - stream_set_timeout($this->serverSocket, 0, 100); + self::assertTrue(stream_set_timeout($this->serverSocket, 0, 100)); $connection = stream_socket_accept($this->serverSocket); // return input as output - stream_copy_to_stream($connection, $connection, strlen($testData)); + self::assertEquals(strlen($testData), stream_copy_to_stream($connection, $connection, strlen($testData))); + + usleep(300); + + self::assertTrue(fclose($connection)); + + usleep(500); - fclose($connection); $readData = $this->socketClient->read($numBytes); self::assertEquals($testData, $readData); @@ -137,20 +149,25 @@ public function testRead(): void public function testReadContents(): void { - $testData = str_repeat("0123456789", mt_rand(1, 10)); + $testData = str_repeat("0123456789", random_int(1, 10)); + #$testData = str_repeat("0123456789", 10); $numBytes = $this->socketClient->write($testData); self::assertEquals(strlen($testData), $numBytes); // lower timeout for server-socket - stream_set_timeout($this->serverSocket, 0, 100); + self::assertTrue(stream_set_timeout($this->serverSocket, 0, 100)); $connection = stream_socket_accept($this->serverSocket); // return input as output - stream_copy_to_stream($connection, $connection, strlen($testData)); + self::assertEquals(strlen($testData), stream_copy_to_stream($connection, $connection, strlen($testData))); + + usleep(300); + + self::assertTrue(fclose($connection)); - fclose($connection); + usleep(500); $readData = $this->socketClient->read(1024); @@ -221,7 +238,7 @@ public function testUpdateStreamContext(): void public function testSetContextFailsAfterConnect(): void { - self::expectException(LogicException::class); + $this->expectException(LogicException::class); // enforce connect $this->socketClient->getSocket(); @@ -230,7 +247,7 @@ public function testSetContextFailsAfterConnect(): void public function testSetConnectTimeoutFailsAfterConnect(): void { - self::expectException(LogicException::class); + $this->expectException(LogicException::class); // enforce connect $this->socketClient->getSocket(); diff --git a/tests/Gelf/Test/Transport/StreamSocketClientUdpTest.php b/tests/Gelf/Test/Transport/StreamSocketClientUdpTest.php index ac56fda..7f122f3 100644 --- a/tests/Gelf/Test/Transport/StreamSocketClientUdpTest.php +++ b/tests/Gelf/Test/Transport/StreamSocketClientUdpTest.php @@ -53,7 +53,7 @@ public function tearDown(): void public function testInvalidConstructorArguments(): void { - self::expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); $client = new StreamSocketClient("not-a-scheme", "not-a-host", -1); $client->getSocket(); diff --git a/tests/Gelf/Test/Transport/TcpTransportTest.php b/tests/Gelf/Test/Transport/TcpTransportTest.php index 57a85e8..8e9f659 100644 --- a/tests/Gelf/Test/Transport/TcpTransportTest.php +++ b/tests/Gelf/Test/Transport/TcpTransportTest.php @@ -44,8 +44,8 @@ public function setUp(): void // create an encoder always return $testMessage $this->encoder = $this->createMock(NoNullByteEncoderInterface::class); - $this->encoder->expects($this->any())->method('encode')->will( - $this->returnValue($this->testMessage) + $this->encoder->expects($this->any())->method('encode')->willReturn( + $this->testMessage ); $this->transport = $this->getTransport(); @@ -101,7 +101,7 @@ public function testSslOptionsAreUsed(): void $sslOptions = $this->createMock(SslOptions::class); $sslOptions->expects($this->exactly(2)) ->method('toStreamContext') - ->will($this->returnValue(['ssl' => null])); + ->willReturn(['ssl' => null]); $transport = new TcpTransport("localhost", 12202, $sslOptions); @@ -138,7 +138,7 @@ public function testConnectTimeout(): void $this->socketClient ->expects($this->once()) ->method('getConnectTimeout') - ->will($this->returnValue(123)); + ->willReturn(123); self::assertEquals(123, $this->transport->getConnectTimeout()); @@ -152,7 +152,7 @@ public function testConnectTimeout(): void public function testNonNullSafeEncoderFails(): void { - self::expectException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->transport->setMessageEncoder(new CompressedJsonEncoder()); } diff --git a/tests/Gelf/Test/Transport/UdpTransportTest.php b/tests/Gelf/Test/Transport/UdpTransportTest.php index 0bba39b..dce8b8f 100644 --- a/tests/Gelf/Test/Transport/UdpTransportTest.php +++ b/tests/Gelf/Test/Transport/UdpTransportTest.php @@ -40,8 +40,8 @@ public function setUp(): void // create an encoder always return $testMessage $this->encoder = $this->createMock(EncoderInterface::class); - $this->encoder->expects($this->any())->method('encode')->will( - $this->returnValue($this->testMessage) + $this->encoder->expects($this->any())->method('encode')->willReturn( + $this->testMessage ); $this->transport = $this->getTransport(0); @@ -101,7 +101,7 @@ public function testSendChunked(): void public function testInvalidChunkNumber() { - self::expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); $transport = $this->getTransport(self::CHUNK_HEADER_LENGTH + 1); $transport->send($this->message);