Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Connection/StreamConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -173,7 +173,6 @@ private function read(int $length, string $errorMessage) : string
throw new CommunicationFailed('Read timed out');
}


throw CommunicationFailed::withLastPhpError($errorMessage);
}
}
6 changes: 3 additions & 3 deletions src/Exception/CommunicationFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
15 changes: 12 additions & 3 deletions tests/Integration/ClientMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions tests/Integration/Connection/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 6 additions & 5 deletions tests/Integration/Connection/ParseGreetingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
60 changes: 39 additions & 21 deletions tests/Integration/Connection/ReadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down