From cd499130c54da33c1498e5b3a4786bb49e452e96 Mon Sep 17 00:00:00 2001 From: tienvx Date: Mon, 6 Mar 2023 16:26:26 +0700 Subject: [PATCH] Move endpoint from config to parameter --- README.md | 5 ++--- UPGRADE-9.0.md | 7 +++++++ .../StubService/Service/StubServerHttpService.php | 4 ++-- .../Service/StubServerHttpServiceInterface.php | 2 +- .../Standalone/StubService/StubServerConfig.php | 15 --------------- .../StubService/StubServerConfigInterface.php | 4 ---- .../Service/StubServerHttpServiceTest.php | 11 +++++------ .../Standalone/StubServer/StubServerTest.php | 8 +++----- 8 files changed, 20 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index f4c998c6..5df03992 100644 --- a/README.md +++ b/README.md @@ -400,13 +400,12 @@ $endpoint = 'test'; $config = (new StubServerConfig()) ->setFiles($files) - ->setPort($port) - ->setEndpoint($endpoint); + ->setPort($port); $stubServer = new StubServer($config); $stubServer->start(); $service = new StubServerHttpService(new GuzzleClient(), $config); -echo $service->getJson(); // output: {"results":[{"name":"Games"}]} +echo $service->getJson($endpoint); // output: {"results":[{"name":"Games"}]} ``` diff --git a/UPGRADE-9.0.md b/UPGRADE-9.0.md index aca9d300..323b034a 100644 --- a/UPGRADE-9.0.md +++ b/UPGRADE-9.0.md @@ -50,3 +50,10 @@ UPGRADE FROM 8.x to 9.0 $this->assertTrue($verifyResult); ``` + +* Stub Server + * Endpoint now can be set by: + ```php + $service = new StubServerHttpService(new GuzzleClient(), $this->config); + $service->getJson($endpoint); + ``` diff --git a/src/PhpPact/Standalone/StubService/Service/StubServerHttpService.php b/src/PhpPact/Standalone/StubService/Service/StubServerHttpService.php index 81f5f726..f40100a1 100644 --- a/src/PhpPact/Standalone/StubService/Service/StubServerHttpService.php +++ b/src/PhpPact/Standalone/StubService/Service/StubServerHttpService.php @@ -26,9 +26,9 @@ public function __construct(ClientInterface $client, StubServerConfigInterface $ * {@inheritdoc} * @throws \JsonException */ - public function getJson(): string + public function getJson(string $endpoint): string { - $uri = $this->config->getBaseUri()->withPath('/' . $this->config->getEndpoint()); + $uri = $this->config->getBaseUri()->withPath('/' . $endpoint); $response = $this->client->get($uri, [ 'headers' => [ 'Content-Type' => 'application/json', diff --git a/src/PhpPact/Standalone/StubService/Service/StubServerHttpServiceInterface.php b/src/PhpPact/Standalone/StubService/Service/StubServerHttpServiceInterface.php index e1b84aa0..325637e2 100644 --- a/src/PhpPact/Standalone/StubService/Service/StubServerHttpServiceInterface.php +++ b/src/PhpPact/Standalone/StubService/Service/StubServerHttpServiceInterface.php @@ -7,5 +7,5 @@ interface StubServerHttpServiceInterface /** * Get the current state of the PACT JSON file and write it to disk. */ - public function getJson(): string; + public function getJson(string $endpoint): string; } diff --git a/src/PhpPact/Standalone/StubService/StubServerConfig.php b/src/PhpPact/Standalone/StubService/StubServerConfig.php index beb55ee4..42d62315 100644 --- a/src/PhpPact/Standalone/StubService/StubServerConfig.php +++ b/src/PhpPact/Standalone/StubService/StubServerConfig.php @@ -46,8 +46,6 @@ class StubServerConfig implements StubServerConfigInterface private bool $emptyProviderState = false; private bool $insecureTls = false; - private string $endpoint; - public function getBrokerUrl(): ?UriInterface { return $this->brokerUrl; @@ -256,17 +254,4 @@ public function getBaseUri(): UriInterface { return new Uri("http://localhost:{$this->getPort()}"); } - - - public function getEndpoint(): string - { - return $this->endpoint; - } - - public function setEndpoint(string $endpoint): StubServerConfigInterface - { - $this->endpoint = $endpoint; - - return $this; - } } diff --git a/src/PhpPact/Standalone/StubService/StubServerConfigInterface.php b/src/PhpPact/Standalone/StubService/StubServerConfigInterface.php index ecfe26c0..25d0decb 100644 --- a/src/PhpPact/Standalone/StubService/StubServerConfigInterface.php +++ b/src/PhpPact/Standalone/StubService/StubServerConfigInterface.php @@ -150,8 +150,4 @@ public function setProviderNames(array $providerNames): self; public function getProviderNames(): array; public function getBaseUri(): UriInterface; - - public function getEndpoint(): string; - - public function setEndpoint(string $endpoint): self; } diff --git a/tests/PhpPact/Standalone/StubServer/Service/StubServerHttpServiceTest.php b/tests/PhpPact/Standalone/StubServer/Service/StubServerHttpServiceTest.php index 9a9769b0..734e978c 100644 --- a/tests/PhpPact/Standalone/StubServer/Service/StubServerHttpServiceTest.php +++ b/tests/PhpPact/Standalone/StubServer/Service/StubServerHttpServiceTest.php @@ -28,14 +28,12 @@ class StubServerHttpServiceTest extends TestCase */ protected function setUp(): void { - $files = [__DIR__ . '/../../../../_resources/someconsumer-someprovider.json']; - $port = 7201; - $endpoint = 'test'; + $files = [__DIR__ . '/../../../../_resources/someconsumer-someprovider.json']; + $port = 7201; $this->config = (new StubServerConfig()) ->setFiles($files) - ->setPort($port) - ->setEndpoint($endpoint); + ->setPort($port); $this->stubServer = new StubServer($this->config); $this->stubServer->start(); @@ -49,7 +47,8 @@ protected function tearDown(): void public function testGetJson() { - $result = $this->service->getJson(); + $endpoint = 'test'; + $result = $this->service->getJson($endpoint); $this->assertEquals('{"results":[{"name":"Games"}]}', $result); } } diff --git a/tests/PhpPact/Standalone/StubServer/StubServerTest.php b/tests/PhpPact/Standalone/StubServer/StubServerTest.php index 687e5800..4ea6cb51 100644 --- a/tests/PhpPact/Standalone/StubServer/StubServerTest.php +++ b/tests/PhpPact/Standalone/StubServer/StubServerTest.php @@ -14,14 +14,12 @@ class StubServerTest extends TestCase public function testStartAndStop() { try { - $files = [__DIR__ . '/../../../_resources/someconsumer-someprovider.json']; - $port = 7201; - $endpoint= 'test'; + $files = [__DIR__ . '/../../../_resources/someconsumer-someprovider.json']; + $port = 7201; $subject = (new StubServerConfig()) ->setFiles($files) - ->setPort($port) - ->setEndpoint($endpoint); + ->setPort($port); $stubServer = new StubServer($subject); $pid = $stubServer->start();