Skip to content

Commit 87bde73

Browse files
authored
Merge pull request #444 from Gummibeer/add-fixture-context
Feature | Add Context To Fixtures
2 parents d046fa3 + 21c548c commit 87bde73

File tree

3 files changed

+103
-14
lines changed

3 files changed

+103
-14
lines changed

src/Data/RecordedResponse.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ class RecordedResponse implements JsonSerializable
1414
* Constructor
1515
*
1616
* @param array<string, mixed> $headers
17+
* @param array<string, mixed> $context
1718
*/
1819
public function __construct(
1920
public int $statusCode,
2021
public array $headers = [],
2122
public mixed $data = null,
23+
public array $context = []
2224
) {
2325
//
2426
}
@@ -35,6 +37,7 @@ public static function fromFile(string $contents): static
3537
* statusCode: int,
3638
* headers: array<string, mixed>,
3739
* data: mixed,
40+
* context: array<string, mixed>,
3841
* } $fileData
3942
*/
4043
$fileData = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
@@ -48,7 +51,8 @@ public static function fromFile(string $contents): static
4851
return new static(
4952
statusCode: $fileData['statusCode'],
5053
headers: $fileData['headers'],
51-
data: $data
54+
data: $data,
55+
context: $fileData['context'] ?? [],
5256
);
5357
}
5458

@@ -97,6 +101,7 @@ public function jsonSerialize(): array
97101
'statusCode' => $this->statusCode,
98102
'headers' => $this->headers,
99103
'data' => $this->data,
104+
'context' => $this->context,
100105
];
101106

102107
if (mb_check_encoding($response['data'], 'UTF-8') === false) {

src/Http/Faking/Fixture.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
use Saloon\Helpers\ArrayHelpers;
1010
use Saloon\Data\RecordedResponse;
1111
use Saloon\Helpers\FixtureHelper;
12+
use Saloon\Repositories\ArrayStore;
1213
use Saloon\Exceptions\FixtureException;
1314
use Saloon\Exceptions\FixtureMissingException;
1415
use Saloon\Repositories\Body\StringBodyRepository;
16+
use Saloon\Contracts\ArrayStore as ArrayStoreContract;
1517

1618
class Fixture
1719
{
@@ -30,6 +32,11 @@ class Fixture
3032
*/
3133
protected Storage $storage;
3234

35+
/**
36+
* The context of the fixture
37+
*/
38+
protected ArrayStoreContract $context;
39+
3340
/**
3441
* Data to merge in the mocked response.
3542
*
@@ -45,10 +52,11 @@ class Fixture
4552
/**
4653
* Constructor
4754
*/
48-
public function __construct(string $name = '', ?Storage $storage = null)
55+
public function __construct(string $name = '', ?Storage $storage = null, ?ArrayStoreContract $context = null)
4956
{
5057
$this->name = $name;
5158
$this->storage = $storage ?? new Storage(MockConfig::getFixturePath(), true);
59+
$this->context = $context ?? new ArrayStore();
5260
}
5361

5462
/**
@@ -137,6 +145,7 @@ public function store(RecordedResponse $recordedResponse): static
137145
$recordedResponse = $this->swapSensitiveJson($recordedResponse);
138146
$recordedResponse = $this->swapSensitiveBodyWithRegex($recordedResponse);
139147
$recordedResponse = $this->beforeSave($recordedResponse);
148+
$recordedResponse->context = $this->context->merge($recordedResponse->context)->all();
140149

141150
$this->storage->put($this->getFixturePath(), $recordedResponse->toFile());
142151

@@ -268,4 +277,39 @@ protected function beforeSave(RecordedResponse $recordedResponse): RecordedRespo
268277
{
269278
return $recordedResponse;
270279
}
280+
281+
/**
282+
* Get a specific context value or return the entire context
283+
*
284+
* @return ($key is null ? ArrayStoreContract : mixed)
285+
*/
286+
public function getContext(?string $key = null): mixed
287+
{
288+
if ($key === null) {
289+
return $this->context;
290+
}
291+
292+
return $this->context->get($key);
293+
}
294+
295+
/**
296+
* Set a specific context value
297+
*/
298+
public function setContext(string $key, mixed $value): static
299+
{
300+
$this->context->add($key, $value);
301+
302+
return $this;
303+
}
304+
305+
/**
306+
* Merge context values into the fixture
307+
* @param array<string, mixed> $context
308+
*/
309+
public function withContext(array $context): static
310+
{
311+
$this->context->merge($context);
312+
313+
return $this;
314+
}
271315
}

tests/Unit/FixtureDataTest.php

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,64 @@
4545
expect($mockResponse)->toEqual(new MockResponse($data['data'], $data['statusCode'], $data['headers']));
4646
});
4747

48-
test('you can json serialize the fixture data or convert it into a file', function () {
49-
$data = [
50-
'statusCode' => 200,
51-
'headers' => [
52-
'Content-Type' => 'application/json',
53-
],
54-
'data' => [
55-
'name' => 'Sam',
56-
],
57-
];
48+
test('you can json serialize the fixture data or convert it into a file', function (array $data, ?array $expected = null) {
49+
$expected ??= $data;
5850

5951
$fixtureData = RecordedResponse::fromFile(json_encode($data, JSON_PRETTY_PRINT));
6052

6153
$serialized = json_encode($fixtureData, JSON_PRETTY_PRINT);
6254

63-
expect($serialized)->toEqual(json_encode($data, JSON_PRETTY_PRINT));
55+
expect($serialized)->toEqual(json_encode($expected, JSON_PRETTY_PRINT));
6456
expect($fixtureData->toFile())->toEqual($serialized);
65-
});
57+
})->with([
58+
'without context key' => [
59+
[
60+
'statusCode' => 200,
61+
'headers' => [
62+
'Content-Type' => 'application/json',
63+
],
64+
'data' => [
65+
'name' => 'Sam',
66+
],
67+
],
68+
[
69+
'statusCode' => 200,
70+
'headers' => [
71+
'Content-Type' => 'application/json',
72+
],
73+
'data' => [
74+
'name' => 'Sam',
75+
],
76+
'context' => [],
77+
],
78+
],
79+
'with context key' => [
80+
[
81+
'statusCode' => 200,
82+
'headers' => [
83+
'Content-Type' => 'application/json',
84+
],
85+
'data' => [
86+
'name' => 'Sam',
87+
],
88+
'context' => [],
89+
],
90+
],
91+
'with context data' => [
92+
[
93+
'statusCode' => 200,
94+
'headers' => [
95+
'Content-Type' => 'application/json',
96+
],
97+
'data' => [
98+
'name' => 'Sam',
99+
],
100+
'context' => [
101+
'test' => 'you can json serialize the fixture data or convert it into a file',
102+
],
103+
],
104+
],
105+
]);
66106

67107
test('arbitrary data can be merged in the fixture', function () {
68108
$response = connector()->send(new DTORequest, new MockClient([

0 commit comments

Comments
 (0)