Skip to content

Commit

Permalink
Add support stubs endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
demyan112rv committed Jul 17, 2024
1 parent a9852ad commit bf29077
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 30 deletions.
39 changes: 16 additions & 23 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,37 @@
*/
class Formatter
{
private Imposter $imposter;

public function __construct(Imposter $imposter)
{
$this->imposter = $imposter;
}

/**
* @return array<string, mixed>
*/
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;
Expand All @@ -59,7 +52,7 @@ public function toArray(): array
/**
* @return array<string, mixed>
*/
private function stubToArray(Stub $stub): array
public function stubToArray(Stub $stub): array
{
$array = [];
$responses = [];
Expand Down
54 changes: 53 additions & 1 deletion src/Mountebank.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 => [
Expand All @@ -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
*/
Expand Down
12 changes: 6 additions & 6 deletions tests/Unit/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/MountebankTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit bf29077

Please sign in to comment.