From e9dac03fb766581e904c8e765e8d0c3bbd1c19a3 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 5 Aug 2024 13:11:54 +0200 Subject: [PATCH 1/2] add context to fixtures --- src/Data/RecordedResponse.php | 7 +++- src/Http/Faking/Fixture.php | 36 ++++++++++++++++++- tests/Unit/FixtureDataTest.php | 64 +++++++++++++++++++++++++++------- 3 files changed, 93 insertions(+), 14 deletions(-) diff --git a/src/Data/RecordedResponse.php b/src/Data/RecordedResponse.php index 0c524cd1..68ec71a6 100644 --- a/src/Data/RecordedResponse.php +++ b/src/Data/RecordedResponse.php @@ -14,11 +14,13 @@ class RecordedResponse implements JsonSerializable * Constructor * * @param array $headers + * @param array $context */ public function __construct( public int $statusCode, public array $headers = [], public mixed $data = null, + public array $context = [] ) { // } @@ -35,6 +37,7 @@ public static function fromFile(string $contents): static * statusCode: int, * headers: array, * data: mixed, + * context: array, * } $fileData */ $fileData = json_decode($contents, true, 512, JSON_THROW_ON_ERROR); @@ -48,7 +51,8 @@ public static function fromFile(string $contents): static return new static( statusCode: $fileData['statusCode'], headers: $fileData['headers'], - data: $data + data: $data, + context: $fileData['context'] ?? [], ); } @@ -97,6 +101,7 @@ public function jsonSerialize(): array 'statusCode' => $this->statusCode, 'headers' => $this->headers, 'data' => $this->data, + 'context' => $this->context, ]; if (mb_check_encoding($response['data'], 'UTF-8') === false) { diff --git a/src/Http/Faking/Fixture.php b/src/Http/Faking/Fixture.php index c35d36c7..5292b639 100644 --- a/src/Http/Faking/Fixture.php +++ b/src/Http/Faking/Fixture.php @@ -8,8 +8,10 @@ use Saloon\Helpers\Storage; use Saloon\Data\RecordedResponse; use Saloon\Helpers\FixtureHelper; +use Saloon\Repositories\ArrayStore; use Saloon\Exceptions\FixtureException; use Saloon\Exceptions\FixtureMissingException; +use Saloon\Contracts\ArrayStore as ArrayStoreContract; class Fixture { @@ -28,13 +30,19 @@ class Fixture */ protected Storage $storage; + /** + * The context of the fixture + */ + protected ArrayStoreContract $context; + /** * Constructor */ - public function __construct(string $name = '', Storage $storage = null) + public function __construct(string $name = '', Storage $storage = null, ArrayStoreContract $context = null) { $this->name = $name; $this->storage = $storage ?? new Storage(MockConfig::getFixturePath(), true); + $this->context = $context ?? new ArrayStore(); } /** @@ -67,6 +75,7 @@ public function store(RecordedResponse $recordedResponse): static $recordedResponse = $this->swapSensitiveJson($recordedResponse); $recordedResponse = $this->swapSensitiveBodyWithRegex($recordedResponse); $recordedResponse = $this->beforeSave($recordedResponse); + $recordedResponse->context = $this->context->merge($recordedResponse->context)->all(); $this->storage->put($this->getFixturePath(), $recordedResponse->toFile()); @@ -198,4 +207,29 @@ protected function beforeSave(RecordedResponse $recordedResponse): RecordedRespo { return $recordedResponse; } + + /** + * Get a specific context value or return the entire context + * + * @return ($key is null ? ArrayStoreContract : mixed) + */ + public function getContext(?string $key = null): mixed + { + if ($key === null) { + return $this->context; + } + + return $this->context->get($key); + } + + /** + * Merge context values into the fixture + * @param array $context + */ + public function withContext(array $context): static + { + $this->context->merge($context); + + return $this; + } } diff --git a/tests/Unit/FixtureDataTest.php b/tests/Unit/FixtureDataTest.php index c2ed998e..7d9d45c5 100644 --- a/tests/Unit/FixtureDataTest.php +++ b/tests/Unit/FixtureDataTest.php @@ -42,21 +42,61 @@ expect($mockResponse)->toEqual(new MockResponse($data['data'], $data['statusCode'], $data['headers'])); }); -test('you can json serialize the fixture data or convert it into a file', function () { - $data = [ - 'statusCode' => 200, - 'headers' => [ - 'Content-Type' => 'application/json', - ], - 'data' => [ - 'name' => 'Sam', - ], - ]; +test('you can json serialize the fixture data or convert it into a file', function (array $data, ?array $expected = null) { + $expected ??= $data; $fixtureData = RecordedResponse::fromFile(json_encode($data, JSON_PRETTY_PRINT)); $serialized = json_encode($fixtureData, JSON_PRETTY_PRINT); - expect($serialized)->toEqual(json_encode($data, JSON_PRETTY_PRINT)); + expect($serialized)->toEqual(json_encode($expected, JSON_PRETTY_PRINT)); expect($fixtureData->toFile())->toEqual($serialized); -}); +})->with([ + 'without context key' => [ + [ + 'statusCode' => 200, + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'data' => [ + 'name' => 'Sam', + ], + ], + [ + 'statusCode' => 200, + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'data' => [ + 'name' => 'Sam', + ], + 'context' => [], + ], + ], + 'with context key' => [ + [ + 'statusCode' => 200, + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'data' => [ + 'name' => 'Sam', + ], + 'context' => [], + ], + ], + 'with context data' => [ + [ + 'statusCode' => 200, + 'headers' => [ + 'Content-Type' => 'application/json', + ], + 'data' => [ + 'name' => 'Sam', + ], + 'context' => [ + 'test' => 'you can json serialize the fixture data or convert it into a file', + ], + ], + ], +]); From afec902392503d1f1708b34a365cfbf6fef11483 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 5 Aug 2024 13:19:46 +0200 Subject: [PATCH 2/2] allow to set a single context value --- src/Http/Faking/Fixture.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Http/Faking/Fixture.php b/src/Http/Faking/Fixture.php index 5292b639..c5374623 100644 --- a/src/Http/Faking/Fixture.php +++ b/src/Http/Faking/Fixture.php @@ -222,6 +222,16 @@ public function getContext(?string $key = null): mixed return $this->context->get($key); } + /** + * Set a specific context value + */ + public function setContext(string $key, mixed $value): static + { + $this->context->add($key, $value); + + return $this; + } + /** * Merge context values into the fixture * @param array $context