From bf2907747a78a906f0af81b474b43c22ae0d08c4 Mon Sep 17 00:00:00 2001 From: Ruslan Demyanovsky Date: Wed, 17 Jul 2024 18:36:31 +0200 Subject: [PATCH] Add support stubs endpoints --- src/Formatter.php | 39 +++++++++++-------------- src/Mountebank.php | 54 ++++++++++++++++++++++++++++++++++- tests/Unit/FormatterTest.php | 12 ++++---- tests/Unit/MountebankTest.php | 1 + 4 files changed, 76 insertions(+), 30 deletions(-) diff --git a/src/Formatter.php b/src/Formatter.php index 3e1d1b4..74405d8 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -13,44 +13,37 @@ */ class Formatter { - private Imposter $imposter; - - public function __construct(Imposter $imposter) - { - $this->imposter = $imposter; - } - /** * @return array */ - public function toArray(): array + public function imposterToArray(Imposter $imposter): array { $stubs = []; - foreach ($this->imposter->getStubs() as $s => $stub) { + foreach ($imposter->getStubs() as $s => $stub) { $stubs[$s] = $this->stubToArray($stub); } $array = [ - 'port' => $this->imposter->getPort(), - 'protocol' => $this->imposter->getProtocol(), - 'name' => $this->imposter->getName(), + 'port' => $imposter->getPort(), + 'protocol' => $imposter->getProtocol(), + 'name' => $imposter->getName(), 'stubs' => $stubs, - 'allowCORS' => $this->imposter->isAllowCORS(), - 'recordRequests' => $this->imposter->isRecordRequests(), + 'allowCORS' => $imposter->isAllowCORS(), + 'recordRequests' => $imposter->isRecordRequests(), ]; - if ($this->imposter->getProtocol() === Imposter::PROTOCOL_HTTPS) { - $array['key'] = $this->imposter->getKey(); - $array['cert'] = $this->imposter->getCert(); - $array['mutualAuth'] = $this->imposter->isMutualAuth(); + if ($imposter->getProtocol() === Imposter::PROTOCOL_HTTPS) { + $array['key'] = $imposter->getKey(); + $array['cert'] = $imposter->getCert(); + $array['mutualAuth'] = $imposter->isMutualAuth(); } - if ($this->imposter->getDefaultResponse() !== null) { - $array['defaultResponse'] = $this->imposter->getDefaultResponse()->getConfig(); + if ($imposter->getDefaultResponse() !== null) { + $array['defaultResponse'] = $imposter->getDefaultResponse()->getConfig(); } - if ($this->imposter->getSchema() !== null) { - $array['schema'] = $this->imposter->getSchema(); + if ($imposter->getSchema() !== null) { + $array['schema'] = $imposter->getSchema(); } return $array; @@ -59,7 +52,7 @@ public function toArray(): array /** * @return array */ - private function stubToArray(Stub $stub): array + public function stubToArray(Stub $stub): array { $array = []; $responses = []; diff --git a/src/Mountebank.php b/src/Mountebank.php index 6563022..d8a3ed6 100644 --- a/src/Mountebank.php +++ b/src/Mountebank.php @@ -16,6 +16,7 @@ class Mountebank { public const URI_IMPOSTERS = 'imposters'; + public const URI_STUBS = 'stubs'; public const URI_CONFIG = 'config'; public const URI_LOGS = 'logs'; @@ -68,6 +69,11 @@ public function getImpostersUrl(): string return $this->host . ':' . $this->port . '/' . static::URI_IMPOSTERS; } + public function getStubsUrl(): string + { + return $this->getImpostersUrl() . '/' . $this->port . '/' . static::URI_STUBS; + } + public function getLogsUrl(): string { return $this->host . ':' . $this->port . '/' . static::URI_LOGS; @@ -99,7 +105,7 @@ public function getImposters(): ResponseInterface */ public function addImposter(Imposter $imposter): ResponseInterface { - $formattedImposter = (new Formatter($imposter))->toArray(); + $formattedImposter = (new Formatter())->imposterToArray($imposter); return $this->client->request('POST', $this->getImpostersUrl(), [ RequestOptions::BODY => \json_encode($formattedImposter), RequestOptions::HEADERS => [ @@ -125,6 +131,52 @@ public function removeImposters(): ResponseInterface return $this->client->request('DELETE', $this->getImpostersUrl()); } + /** + * @codeCoverageIgnore + */ + public function addStub(Stub $stub, int $index): ResponseInterface + { + $formattedStub = (new Formatter())->stubToArray($stub); + return $this->client->request('POST', $this->getStubsUrl(), [ + RequestOptions::BODY => \json_encode([ + 'index' => $index, + 'stub' => $formattedStub, + ]), + RequestOptions::HEADERS => [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json' + ], + ]); + } + + /** + * @codeCoverageIgnore + */ + public function updateStub(Stub $stub, int $index): ResponseInterface + { + $formattedStub = (new Formatter())->stubToArray($stub); + return $this->client->request('PUT', $this->getStubsUrl() . '/' . $index, [ + RequestOptions::BODY => \json_encode($formattedStub), + RequestOptions::HEADERS => [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json' + ], + ]); + } + + /** + * @codeCoverageIgnore + */ + public function deleteStub(int $index): ResponseInterface + { + return $this->client->request('DELETE', $this->getStubsUrl() . '/' . $index, [ + RequestOptions::HEADERS => [ + 'Content-Type' => 'application/json', + 'Accept' => 'application/json' + ], + ]); + } + /** * @codeCoverageIgnore */ diff --git a/tests/Unit/FormatterTest.php b/tests/Unit/FormatterTest.php index 5fc1b7b..c0512b0 100644 --- a/tests/Unit/FormatterTest.php +++ b/tests/Unit/FormatterTest.php @@ -65,8 +65,8 @@ public function testHttps(): void ->setAllowCORS(true) ->setRecordRequests(true); - $formatter = new Formatter($imposter); - $array = $formatter->toArray(); + $formatter = new Formatter(); + $array = $formatter->imposterToArray($imposter); $this->assertNotEmpty($array); $this->assertArrayHasKey('port', $array); $this->assertArrayHasKey('protocol', $array); @@ -126,8 +126,8 @@ public function testHttp(): void ->setAllowCORS(true) ->setRecordRequests(true); - $formatter = new Formatter($imposter); - $array = $formatter->toArray(); + $formatter = new Formatter(); + $array = $formatter->imposterToArray($imposter); $this->assertNotEmpty($array); $this->assertArrayHasKey('port', $array); $this->assertArrayHasKey('protocol', $array); @@ -143,8 +143,8 @@ public function testSchemaAttached(): void $imposter = new Imposter('Test imposter', 1234, Imposter::PROTOCOL_HTTP); $imposter->setSchema('Test Schema'); - $formatter = new Formatter($imposter); - $array = $formatter->toArray(); + $formatter = new Formatter(); + $array = $formatter->imposterToArray($imposter); $this->assertArrayHasKey('schema', $array); } diff --git a/tests/Unit/MountebankTest.php b/tests/Unit/MountebankTest.php index f57e247..4b60b18 100644 --- a/tests/Unit/MountebankTest.php +++ b/tests/Unit/MountebankTest.php @@ -26,5 +26,6 @@ public function testFill(): void $this->assertEquals('http://test.com:1234/' . Mountebank::URI_CONFIG, $mb->getConfigUrl()); $this->assertEquals('http://test.com:1234/' . Mountebank::URI_LOGS, $mb->getLogsUrl()); $this->assertEquals('http://test.com:1234/' . Mountebank::URI_IMPOSTERS, $mb->getImpostersUrl()); + $this->assertEquals('http://test.com:1234/' . Mountebank::URI_IMPOSTERS . '/1234/' . Mountebank::URI_STUBS, $mb->getStubsUrl()); } } \ No newline at end of file