From a03051e3ed5d370b9fe6326a38b3a049d0e11a75 Mon Sep 17 00:00:00 2001 From: Pavel Kirpitsov Date: Fri, 3 Feb 2023 20:15:43 +0200 Subject: [PATCH 01/10] fix: display stderr on pact publish process failure --- src/PhpPact/Standalone/Broker/Broker.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/PhpPact/Standalone/Broker/Broker.php b/src/PhpPact/Standalone/Broker/Broker.php index 910706be..5f7b47dd 100644 --- a/src/PhpPact/Standalone/Broker/Broker.php +++ b/src/PhpPact/Standalone/Broker/Broker.php @@ -245,13 +245,18 @@ public function publish(): void $this->getArguments() ) ); - $runner->runBlocking(); - if ($runner->getExitCode() !== 0) { - $this->logger->error($runner->getStderr()); - } + try { + $runner->runBlocking(); - $this->logger->debug($runner->getOutput()); + $this->logger->info('out > ' . $runner->getOutput()); + $this->logger->error('err > ' . $runner->getStderr()); + } catch (\Exception $e) { + $this->logger->info('out > ' . $runner->getOutput()); + $this->logger->error('err > ' . $runner->getStderr()); + + throw $e; + } } public function testWebhook() From d7c1bdef7075d11ff422983e5c7a3f2542e5e73a Mon Sep 17 00:00:00 2001 From: Pavel Kirpitsov Date: Mon, 13 Feb 2023 08:51:13 +0200 Subject: [PATCH 02/10] feat: update phpstan/phpstan analyze tool --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 8b040008..531cab22 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "amphp/cache": "^1.4.0", "amphp/windows-registry": "v0.3.3", "guzzlehttp/guzzle": "^6.5.8|^7.4.5", - "phpunit/phpunit": ">=8.5.23", + "phpunit/phpunit": "^8.5|^9.0", "tienvx/composer-downloads-plugin": "^1.1.0" }, "require-dev": { @@ -45,7 +45,7 @@ "slim/psr7": "^1.2.0", "friendsofphp/php-cs-fixer": "^3.0", "php-amqplib/php-amqplib": "^3.0", - "phpstan/phpstan": "^0.12.90" + "phpstan/phpstan": "^1.9" }, "autoload": { "psr-4": { From 6601c04d9d0662e2fc00e5b0ab7ee6f0dc0b8126 Mon Sep 17 00:00:00 2001 From: Pavel Kirpitsov Date: Mon, 13 Feb 2023 08:57:00 +0200 Subject: [PATCH 03/10] feat: update phpstan/phpstan analyze tool --- src/PhpPact/Standalone/MockService/MockServer.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/PhpPact/Standalone/MockService/MockServer.php b/src/PhpPact/Standalone/MockService/MockServer.php index 41492b59..b4b559e0 100644 --- a/src/PhpPact/Standalone/MockService/MockServer.php +++ b/src/PhpPact/Standalone/MockService/MockServer.php @@ -3,12 +3,12 @@ namespace PhpPact\Standalone\MockService; use Exception; -use GuzzleHttp\Exception\ConnectException; use PhpPact\Http\GuzzleClient; use PhpPact\Standalone\Exception\HealthCheckFailedException; use PhpPact\Standalone\Installer\Model\Scripts; use PhpPact\Standalone\MockService\Service\MockServerHttpService; use PhpPact\Standalone\Runner\ProcessRunner; +use PhpPact\Exception\ConnectionException; /** * Ruby Standalone Mock Server Wrapper @@ -135,15 +135,12 @@ private function verifyHealthCheck(): bool ++$tries; try { - $status = $service->healthCheck(); - - return $status; - } catch (ConnectException $e) { + return $service->healthCheck(); + } catch (ConnectionException $e) { \sleep($retrySec); } } while ($tries <= $maxTries); - // @phpstan-ignore-next-line throw new HealthCheckFailedException("Failed to make connection to Mock Server in {$maxTries} attempts."); } } From 4905417b353a5e72d4961c8717c86790ed1ff656 Mon Sep 17 00:00:00 2001 From: Pavel Kirpitsov Date: Mon, 13 Feb 2023 09:21:48 +0200 Subject: [PATCH 04/10] chore: fix mess with namespaces to match psr-4 --- tests/PhpPact/Consumer/InteractionBuilderTest.php | 2 +- tests/PhpPact/Consumer/Matcher/MatcherTest.php | 3 ++- tests/PhpPact/Consumer/Model/ConsumerRequestTest.php | 3 ++- tests/PhpPact/Consumer/Model/ProviderResponseTest.php | 3 ++- tests/PhpPact/Standalone/Broker/BrokerConfigTest.php | 2 +- tests/PhpPact/Standalone/Broker/BrokerTest.php | 4 +++- tests/PhpPact/Standalone/MockServer/MockServerConfigTest.php | 2 +- tests/PhpPact/Standalone/MockServer/MockServerTest.php | 2 +- .../MockServer/Service/MockServerHttpServiceTest.php | 4 +++- tests/PhpPact/Standalone/ProviderVerifier/VerifierTest.php | 5 ++++- tests/PhpPact/Standalone/Runner/ProcessRunnerTest.php | 3 ++- .../StubServer/Service/StubServerHttpServiceTest.php | 4 +++- tests/PhpPact/Standalone/StubServer/StubServerConfigTest.php | 2 +- tests/PhpPact/Standalone/StubServer/StubServerTest.php | 2 +- 14 files changed, 27 insertions(+), 14 deletions(-) diff --git a/tests/PhpPact/Consumer/InteractionBuilderTest.php b/tests/PhpPact/Consumer/InteractionBuilderTest.php index 06567a76..50649065 100644 --- a/tests/PhpPact/Consumer/InteractionBuilderTest.php +++ b/tests/PhpPact/Consumer/InteractionBuilderTest.php @@ -1,6 +1,6 @@ Date: Mon, 13 Feb 2023 09:38:40 +0200 Subject: [PATCH 05/10] chore: catch proper exceptions --- src/PhpPact/Exception/ConnectionException.php | 4 +-- .../Service/MockServerHttpService.php | 29 +++++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/PhpPact/Exception/ConnectionException.php b/src/PhpPact/Exception/ConnectionException.php index 0cb54cf7..813f6603 100644 --- a/src/PhpPact/Exception/ConnectionException.php +++ b/src/PhpPact/Exception/ConnectionException.php @@ -10,8 +10,8 @@ */ class ConnectionException extends Exception { - public function __construct(string $message) + public function __construct(string $message, \Exception $previous = null) { - parent::__construct($message, 0, null); + parent::__construct($message, 0, $previous); } } diff --git a/src/PhpPact/Standalone/MockService/Service/MockServerHttpService.php b/src/PhpPact/Standalone/MockService/Service/MockServerHttpService.php index 3d2ac348..6c3ae86e 100644 --- a/src/PhpPact/Standalone/MockService/Service/MockServerHttpService.php +++ b/src/PhpPact/Standalone/MockService/Service/MockServerHttpService.php @@ -7,6 +7,7 @@ use PhpPact\Exception\ConnectionException; use PhpPact\Http\ClientInterface; use PhpPact\Standalone\MockService\MockServerConfigInterface; +use GuzzleHttp\Exception\ConnectException as GuzzleConnectionException; /** * Http Service that interacts with the Ruby Standalone Mock Server. @@ -45,18 +46,22 @@ public function healthCheck(): bool { $uri = $this->config->getBaseUri()->withPath('/'); - $response = $this->client->get($uri, [ - 'headers' => [ - 'Content-Type' => 'application/json', - 'X-Pact-Mock-Service' => true, - ], - ]); - - $body = $response->getBody()->getContents(); - - if ($response->getStatusCode() !== 200 - || $body !== "Mock service running\n") { - throw new ConnectionException('Failed to receive a successful response from the Mock Server.'); + try { + $response = $this->client->get($uri, [ + 'headers' => [ + 'Content-Type' => 'application/json', + 'X-Pact-Mock-Service' => true, + ], + ]); + + $body = $response->getBody()->getContents(); + + if ($response->getStatusCode() !== 200 + || $body !== "Mock service running\n") { + throw new ConnectionException('Failed to receive a successful response from the Mock Server.'); + } + } catch (GuzzleConnectionException $e) { + throw new ConnectionException('Failed to receive a successful response from the Mock Server.', $e); } return true; From bbef6f30bd9e3920dd60798ead74fa3bda104e02 Mon Sep 17 00:00:00 2001 From: Pavel Kirpitsov Date: Mon, 13 Feb 2023 09:43:14 +0200 Subject: [PATCH 06/10] chore: catch proper exceptions --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 531cab22..6f3aeae7 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "amphp/cache": "^1.4.0", "amphp/windows-registry": "v0.3.3", "guzzlehttp/guzzle": "^6.5.8|^7.4.5", - "phpunit/phpunit": "^8.5|^9.0", + "phpunit/phpunit": ">=8.5.23 <10", "tienvx/composer-downloads-plugin": "^1.1.0" }, "require-dev": { From 90890820185cba8403bbe5c55f0f4dabc839ca8a Mon Sep 17 00:00:00 2001 From: Pavel Kirpitsov Date: Mon, 13 Feb 2023 09:47:44 +0200 Subject: [PATCH 07/10] chore: add missing import --- tests/PhpPact/Consumer/InteractionBuilderTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/PhpPact/Consumer/InteractionBuilderTest.php b/tests/PhpPact/Consumer/InteractionBuilderTest.php index 50649065..3bdd9a14 100644 --- a/tests/PhpPact/Consumer/InteractionBuilderTest.php +++ b/tests/PhpPact/Consumer/InteractionBuilderTest.php @@ -2,6 +2,7 @@ namespace PhpPactTest\Consumer; +use PhpPact\Consumer\InteractionBuilder; use PhpPact\Consumer\Matcher\Matcher; use PhpPact\Consumer\Model\ConsumerRequest; use PhpPact\Consumer\Model\ProviderResponse; From a70d3ed6bce337473caabe8f003b31656deb0639 Mon Sep 17 00:00:00 2001 From: Pavel Kirpitsov Date: Mon, 13 Feb 2023 09:53:52 +0200 Subject: [PATCH 08/10] chore: add missing import --- tests/PhpPact/Standalone/MockServer/MockServerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/PhpPact/Standalone/MockServer/MockServerTest.php b/tests/PhpPact/Standalone/MockServer/MockServerTest.php index 6bbd21d5..6d356742 100644 --- a/tests/PhpPact/Standalone/MockServer/MockServerTest.php +++ b/tests/PhpPact/Standalone/MockServer/MockServerTest.php @@ -2,7 +2,7 @@ namespace PhpPactTest\Standalone\MockServer; -use GuzzleHttp\Exception\ConnectException; +use PhpPact\Exception\ConnectionException; use PhpPact\Standalone\Exception\HealthCheckFailedException; use PhpPact\Standalone\Exception\MissingEnvVariableException; use PhpPact\Standalone\MockService\MockServer; @@ -43,7 +43,7 @@ public function testStartAndStopWithRecognizedTimeout() ->disableOriginalConstructor() ->getMock(); - $connectionException = $this->getMockBuilder(ConnectException::class) + $connectionException = $this->getMockBuilder(ConnectionException::class) ->disableOriginalConstructor() ->getMock(); From 7698d36701b3402771038db996fac76d31221945 Mon Sep 17 00:00:00 2001 From: Pavel Kirpitsov Date: Mon, 13 Feb 2023 10:19:34 +0200 Subject: [PATCH 09/10] chore: test fixes --- .../Standalone/MockService/Service/MockServerHttpService.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/PhpPact/Standalone/MockService/Service/MockServerHttpService.php b/src/PhpPact/Standalone/MockService/Service/MockServerHttpService.php index 6c3ae86e..2c13e83a 100644 --- a/src/PhpPact/Standalone/MockService/Service/MockServerHttpService.php +++ b/src/PhpPact/Standalone/MockService/Service/MockServerHttpService.php @@ -2,6 +2,7 @@ namespace PhpPact\Standalone\MockService\Service; +use GuzzleHttp\Exception\RequestException; use PhpPact\Consumer\Model\Interaction; use PhpPact\Consumer\Model\Message; use PhpPact\Exception\ConnectionException; @@ -60,6 +61,8 @@ public function healthCheck(): bool || $body !== "Mock service running\n") { throw new ConnectionException('Failed to receive a successful response from the Mock Server.'); } + } catch (RequestException $e) { + throw new ConnectionException('Failed to receive a successful response from the Mock Server.', $e); } catch (GuzzleConnectionException $e) { throw new ConnectionException('Failed to receive a successful response from the Mock Server.', $e); } From 16c3b6edc6034cd38d7fb70644f76f9022c12553 Mon Sep 17 00:00:00 2001 From: Pavel Kirpitsov Date: Tue, 14 Feb 2023 11:57:51 +0200 Subject: [PATCH 10/10] chore: simplify code --- src/PhpPact/Standalone/Broker/Broker.php | 7 +------ .../Standalone/ProviderVerifier/VerifierProcess.php | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/PhpPact/Standalone/Broker/Broker.php b/src/PhpPact/Standalone/Broker/Broker.php index 5f7b47dd..0cbc35ac 100644 --- a/src/PhpPact/Standalone/Broker/Broker.php +++ b/src/PhpPact/Standalone/Broker/Broker.php @@ -248,14 +248,9 @@ public function publish(): void try { $runner->runBlocking(); - - $this->logger->info('out > ' . $runner->getOutput()); - $this->logger->error('err > ' . $runner->getStderr()); - } catch (\Exception $e) { + } finally { $this->logger->info('out > ' . $runner->getOutput()); $this->logger->error('err > ' . $runner->getStderr()); - - throw $e; } } diff --git a/src/PhpPact/Standalone/ProviderVerifier/VerifierProcess.php b/src/PhpPact/Standalone/ProviderVerifier/VerifierProcess.php index 712a54f4..447b5189 100644 --- a/src/PhpPact/Standalone/ProviderVerifier/VerifierProcess.php +++ b/src/PhpPact/Standalone/ProviderVerifier/VerifierProcess.php @@ -61,14 +61,9 @@ public function run(array $arguments, $processTimeout, $processIdleTimeout) try { $processRunner->runBlocking(); - - $logger->info('out > ' . $processRunner->getOutput()); - $logger->error('err > ' . $processRunner->getStderr()); - } catch (\Exception $e) { + } finally { $logger->info('out > ' . $processRunner->getOutput()); $logger->error('err > ' . $processRunner->getStderr()); - - throw $e; } }