Skip to content

Commit

Permalink
add context to fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Aug 5, 2024
1 parent c266550 commit e9dac03
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 14 deletions.
7 changes: 6 additions & 1 deletion src/Data/RecordedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ class RecordedResponse implements JsonSerializable
* Constructor
*
* @param array<string, mixed> $headers
* @param array<string, mixed> $context
*/
public function __construct(
public int $statusCode,
public array $headers = [],
public mixed $data = null,
public array $context = []
) {
//
}
Expand All @@ -35,6 +37,7 @@ public static function fromFile(string $contents): static
* statusCode: int,
* headers: array<string, mixed>,
* data: mixed,
* context: array<string, mixed>,
* } $fileData
*/
$fileData = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
Expand All @@ -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'] ?? [],
);
}

Expand Down Expand Up @@ -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) {
Expand Down
36 changes: 35 additions & 1 deletion src/Http/Faking/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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<string, mixed> $context
*/
public function withContext(array $context): static
{
$this->context->merge($context);

return $this;
}
}
64 changes: 52 additions & 12 deletions tests/Unit/FixtureDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
],
]);

0 comments on commit e9dac03

Please sign in to comment.