Skip to content

Commit

Permalink
fix: Fix CS and failing tests on PHP7
Browse files Browse the repository at this point in the history
  • Loading branch information
rybakit committed Feb 9, 2024
1 parent 8c99575 commit 356c699
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 45 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015-2023 Eugene Leonovich
Copyright (c) 2015-2024 Eugene Leonovich

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
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);
}
}
2 changes: 1 addition & 1 deletion tests/Integration/ClientMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ 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');
$this->expectExceptionMessageMatches('/Error writing request:.+Send of .+ bytes failed/i');
$client->ping();
}

Expand Down
18 changes: 8 additions & 10 deletions tests/Integration/Connection/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,12 @@ public function testUnexpectedResponse() : void
public function testOpenConnectionHandlesTheMissingGreetingCorrectly() : void
{
$clientBuilder = ClientBuilder::createForFakeServer();
$uri = $clientBuilder->getUri();

FakeServerBuilder::create(
new AtConnectionHandler(1, new WriteHandler('')),
new AtConnectionHandler(2, new WriteHandler(GreetingDataProvider::generateGreeting()))
)
->setUri($uri)
->setUri($clientBuilder->getUri())
->start();

$client = $clientBuilder->build();
Expand All @@ -206,17 +205,16 @@ 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());
// At that point the connection was successfully established,
// but the greeting message was not read
self::assertMatchesRegularExpression(
'/Error reading greeting:.+Unable to connect/i',
$e->getMessage()
);
// At this point the connection was successfully established,
// but the greeting message was not read.
}

// The second call should correctly handle
// the missing greeting from the previous call
// the missing greeting from the previous call.
$connection->open();
}
}
12 changes: 5 additions & 7 deletions tests/Integration/Connection/ParseGreetingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,20 @@ final class ParseGreetingTest extends TestCase
public function testParseGreetingWithInvalidServerName(string $greeting) : void
{
$clientBuilder = ClientBuilder::createForFakeServer();
$uri = $clientBuilder->getUri();

FakeServerBuilder::create(new WriteHandler($greeting))
->setUri($uri)
->setUri($clientBuilder->getUri())
->start();

$client = $clientBuilder->build();

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::assertMatchesRegularExpression(
'/Error reading greeting:.+Unable to connect/i',
$e->getMessage()
);

return;
} catch (\RuntimeException $e) {
Expand Down
27 changes: 6 additions & 21 deletions tests/Integration/Connection/ReadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,34 @@ public function testReadLargeResponse() : void
public function testReadEmptyGreeting() : void
{
$clientBuilder = ClientBuilder::createForFakeServer();
$uri = $clientBuilder->getUri();

FakeServerBuilder::create()
->setUri($uri)
->setUri($clientBuilder->getUri())
->start();

$client = $clientBuilder->build();

$this->expectException(CommunicationFailed::class);
$this->expectExceptionMessage(
\sprintf('Error reading greeting: ' .
'stream_socket_client(): Unable to connect to %s ' .
'(Connection refused)', $uri)
);
$this->expectExceptionMessageMatches('/Error reading greeting:.+Unable to connect/i');

$client->ping();
}

public function testUnableToReadResponseLength() : void
{
$clientBuilder = ClientBuilder::createForFakeServer();
$uri = $clientBuilder->getUri();

FakeServerBuilder::create(
new WriteHandler(GreetingDataProvider::generateGreeting()),
new SleepHandler(1)
)
->setUri($uri)
->setUri($clientBuilder->getUri())
->start();

$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)
);
$this->expectExceptionMessageMatches('/Error reading response length:.+Unable to connect/i');

$client->ping();
}
Expand Down Expand Up @@ -100,24 +90,19 @@ public function testReadResponseLengthTimedOut() : void
public function testUnableToReadResponse() : void
{
$clientBuilder = ClientBuilder::createForFakeServer();
$uri = $clientBuilder->getUri();

FakeServerBuilder::create(
new WriteHandler(GreetingDataProvider::generateGreeting()),
new WriteHandler(PacketLength::pack(42)),
new SleepHandler(1)
)
->setUri($uri)
->setUri($clientBuilder->getUri())
->start();

$client = $clientBuilder->build();

$this->expectException(CommunicationFailed::class);
$this->expectExceptionMessage(
\sprintf('Error reading response: ' .
'stream_socket_client(): Unable to connect to %s ' .
'(Connection refused)', $uri)
);
$this->expectExceptionMessageMatches('/Error reading response:.+Unable to connect/i');

$client->ping();
}
Expand Down

0 comments on commit 356c699

Please sign in to comment.