From 1a68616646c7f834dab19b9c1b6412db69bd9893 Mon Sep 17 00:00:00 2001 From: Aleksei Volkov Date: Thu, 8 Feb 2024 15:34:46 +0300 Subject: [PATCH] test: fix tests on PHP 7 Changes to tests introduced in #89 make them fail on PHP 7.x. This patch fixes failing tests. --- src/Connection/StreamConnection.php | 3 +- src/Exception/CommunicationFailed.php | 6 +- tests/Integration/ClientMiddlewareTest.php | 15 ++++- .../Integration/Connection/ConnectionTest.php | 11 ++-- .../Connection/ParseGreetingTest.php | 11 ++-- tests/Integration/Connection/ReadTest.php | 60 ++++++++++++------- 6 files changed, 67 insertions(+), 39 deletions(-) diff --git a/src/Connection/StreamConnection.php b/src/Connection/StreamConnection.php index e859ea8..f15927c 100644 --- a/src/Connection/StreamConnection.php +++ b/src/Connection/StreamConnection.php @@ -151,7 +151,7 @@ public function send(string $data) : string } if (!\fwrite($this->stream, $data)) { - throw CommunicationFailed::withLastPhpError("Error writing request"); + throw CommunicationFailed::withLastPhpError('Error writing request'); } $length = $this->read(PacketLength::SIZE_BYTES, 'Error reading response length'); @@ -173,7 +173,6 @@ private function read(int $length, string $errorMessage) : string throw new CommunicationFailed('Read timed out'); } - throw CommunicationFailed::withLastPhpError($errorMessage); } } diff --git a/src/Exception/CommunicationFailed.php b/src/Exception/CommunicationFailed.php index d0ae250..bda6af4 100644 --- a/src/Exception/CommunicationFailed.php +++ b/src/Exception/CommunicationFailed.php @@ -15,10 +15,10 @@ final class CommunicationFailed extends \RuntimeException implements ClientException { - public static function withLastPhpError(string $errorMessage): self + public static function withLastPhpError(string $errorMessage) : self { - $error = error_get_last(); + $error = \error_get_last(); - return new self($error ? \sprintf("%s: %s", $errorMessage, $error['message']) : $errorMessage); + return new self($error ? \sprintf('%s: %s', $errorMessage, $error['message']) : $errorMessage); } } diff --git a/tests/Integration/ClientMiddlewareTest.php b/tests/Integration/ClientMiddlewareTest.php index 4fcfaaa..4d77f99 100644 --- a/tests/Integration/ClientMiddlewareTest.php +++ b/tests/Integration/ClientMiddlewareTest.php @@ -139,9 +139,18 @@ public function testThrowOnBrokenConnection() : void $client->ping(); - $this->expectException(CommunicationFailed::class); - $this->expectExceptionMessage('Error writing request: fwrite(): Send of 15 bytes failed with errno=32 Broken pipe'); - $client->ping(); + try { + $client->ping(); + } catch (CommunicationFailed $e) { + self::assertEqualsIgnoringCase( + 'Error writing request: fwrite(): Send of 15 bytes failed with errno=32 Broken pipe', + $e->getMessage() + ); + + return; + } + + self::fail(); } private static function createBrokenConnectionMiddleware() : Middleware diff --git a/tests/Integration/Connection/ConnectionTest.php b/tests/Integration/Connection/ConnectionTest.php index 222588c..d7dbc30 100644 --- a/tests/Integration/Connection/ConnectionTest.php +++ b/tests/Integration/Connection/ConnectionTest.php @@ -206,11 +206,12 @@ public function testOpenConnectionHandlesTheMissingGreetingCorrectly() : void $connection->open(); self::fail('Connection not established'); } catch (CommunicationFailed $e) { - self::assertSame( - sprintf('Error reading greeting: ' . - 'stream_socket_client(): Unable to connect to %s ' . - '(Connection refused)', $uri) - , $e->getMessage()); + self::assertStringContainsStringIgnoringCase( + \sprintf('Error reading greeting: '. + 'stream_socket_client(): Unable to connect to %s', + $uri), + $e->getMessage() + ); // At that point the connection was successfully established, // but the greeting message was not read } diff --git a/tests/Integration/Connection/ParseGreetingTest.php b/tests/Integration/Connection/ParseGreetingTest.php index c7feb78..433ce58 100644 --- a/tests/Integration/Connection/ParseGreetingTest.php +++ b/tests/Integration/Connection/ParseGreetingTest.php @@ -38,11 +38,12 @@ public function testParseGreetingWithInvalidServerName(string $greeting) : void try { $client->ping(); } catch (CommunicationFailed $e) { - self::assertSame( - sprintf("Error reading greeting: " . - "stream_socket_client(): Unable to connect to %s " . - "(Connection refused)", $uri), - $e->getMessage()); + self::assertStringContainsStringIgnoringCase( + sprintf('Error reading greeting: '. + 'stream_socket_client(): Unable to connect to %s ', + $uri), + $e->getMessage() + ); return; } catch (\RuntimeException $e) { diff --git a/tests/Integration/Connection/ReadTest.php b/tests/Integration/Connection/ReadTest.php index d8cfc94..d5c9b09 100644 --- a/tests/Integration/Connection/ReadTest.php +++ b/tests/Integration/Connection/ReadTest.php @@ -43,14 +43,20 @@ public function testReadEmptyGreeting() : void $client = $clientBuilder->build(); - $this->expectException(CommunicationFailed::class); - $this->expectExceptionMessage( - \sprintf('Error reading greeting: ' . - 'stream_socket_client(): Unable to connect to %s ' . - '(Connection refused)', $uri) - ); + try { + $client->ping(); + } catch (CommunicationFailed $e) { + self::assertStringContainsStringIgnoringCase( + \sprintf('Error reading greeting: '. + 'stream_socket_client(): Unable to connect to %s ', + $uri), + $e->getMessage() + ); - $client->ping(); + return; + } + + self::fail(); } public function testUnableToReadResponseLength() : void @@ -67,14 +73,20 @@ public function testUnableToReadResponseLength() : void $client = $clientBuilder->build(); - $this->expectException(CommunicationFailed::class); - $this->expectExceptionMessage( - \sprintf('Error reading response length: ' . - 'stream_socket_client(): Unable to connect to %s ' . - '(Connection refused)', $uri) - ); + try { + $client->ping(); + } catch (CommunicationFailed $e) { + self::assertStringContainsStringIgnoringCase( + \sprintf('Error reading response length: '. + 'stream_socket_client(): Unable to connect to %s ', + $uri), + $e->getMessage() + ); - $client->ping(); + return; + } + + self::fail(); } public function testReadResponseLengthTimedOut() : void @@ -112,14 +124,20 @@ public function testUnableToReadResponse() : void $client = $clientBuilder->build(); - $this->expectException(CommunicationFailed::class); - $this->expectExceptionMessage( - \sprintf('Error reading response: ' . - 'stream_socket_client(): Unable to connect to %s ' . - '(Connection refused)', $uri) - ); + try { + $client->ping(); + } catch (CommunicationFailed $e) { + self::assertStringContainsStringIgnoringCase( + \sprintf('Error reading response: '. + 'stream_socket_client(): Unable to connect to %s ', + $uri), + $e->getMessage() + ); - $client->ping(); + return; + } + + self::fail(); } public function testSocketReadTimedOut() : void