Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature | Add Context To Fixtures #444

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 45 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,39 @@ 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);
}

/**
* 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<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',
],
],
],
]);
Loading