-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathFixture.php
More file actions
274 lines (223 loc) · 7.13 KB
/
Fixture.php
File metadata and controls
274 lines (223 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
declare(strict_types=1);
namespace Saloon\Http\Faking;
use Closure;
use JsonException;
use Saloon\MockConfig;
use Saloon\Helpers\Storage;
use const JSON_THROW_ON_ERROR;
use Saloon\Helpers\ArrayHelpers;
use Saloon\Data\RecordedResponse;
use Saloon\Helpers\FixtureHelper;
use Saloon\Exceptions\FixtureException;
use Saloon\Exceptions\FixtureMissingException;
use Saloon\Repositories\Body\StringBodyRepository;
class Fixture
{
/**
* The extension used by the fixture
*/
protected static string $fixtureExtension = 'json';
/**
* The name of the fixture
*/
protected string $name = '';
/**
* The storage helper
*/
protected Storage $storage;
/**
* Data to merge in the mocked response.
*
* @var array<array-key, mixed>|null
*/
protected ?array $merge = null;
/**
* Closure to modify the returned data with.
*/
protected ?Closure $through = null;
/**
* Constructor
*/
public function __construct(string $name = '', ?Storage $storage = null)
{
$this->name = $name;
$this->storage = $storage ?? new Storage(MockConfig::getFixturePath(), true);
}
/**
* Specify data to merge with the mock response data.
*
* @param array<array-key, mixed> $merge
*/
public function merge(array $merge = []): static
{
$this->merge = $merge;
return $this;
}
/**
* Specify a closure to modify the mock response data with.
*/
public function through(Closure $through): static
{
$this->through = $through;
return $this;
}
/**
* Attempt to get the mock response from the fixture.
*/
public function getMockResponse(): ?MockResponse
{
$storage = $this->storage;
$fixturePath = $this->getFixturePath();
if ($storage->exists($fixturePath)) {
$response = RecordedResponse::fromFile($storage->get($fixturePath))->toMockResponse();
if (is_null($this->merge) && is_null($this->through)) {
return $response;
}
// First, we get the body as an array. If we're dealing with
// a `StringBodyRepository`, we have to encode it first.
if (! is_array($body = $response->body()->all())) {
$body = json_decode($body ?: '[]', associative: true, flags: JSON_THROW_ON_ERROR);
}
// We can then merge the data in the body usingthrough
// the ArrayHelpers for dot-notation support.
if (is_array($this->merge)) {
foreach ($this->merge as $key => $value) {
ArrayHelpers::set($body, $key, $value);
}
}
// If specified, we pass the body through a function that
// may modify the mock response data.
if (! is_null($this->through)) {
$body = call_user_func($this->through, $body);
}
// We then set the mutated data back in the repository. If we're dealing
// with a `StringBodyRepository`, we need to encode it back to string.
$response->body()->set(
$response->body() instanceof StringBodyRepository
? json_encode($body)
: $body
);
return $response;
}
if (MockConfig::isThrowingOnMissingFixtures() === true) {
throw new FixtureMissingException($fixturePath);
}
return null;
}
/**
* Store data as the fixture.
*
* @return $this
*/
public function store(RecordedResponse $recordedResponse): static
{
$recordedResponse = $this->swapSensitiveHeaders($recordedResponse);
$recordedResponse = $this->swapSensitiveJson($recordedResponse);
$recordedResponse = $this->swapSensitiveBodyWithRegex($recordedResponse);
$recordedResponse = $this->beforeSave($recordedResponse);
$this->storage->put($this->getFixturePath(), $recordedResponse->toFile());
return $this;
}
/**
* Get the fixture path
*
* @throws \Saloon\Exceptions\FixtureException
*/
public function getFixturePath(): string
{
$name = $this->name;
if (empty($name)) {
$name = $this->defineName();
}
if (empty($name)) {
throw new FixtureException('The fixture must have a name');
}
return sprintf('%s.%s', $name, $this::$fixtureExtension);
}
/**
* Define the fixture name
*/
protected function defineName(): string
{
return '';
}
/**
* Swap any sensitive headers
*/
protected function swapSensitiveHeaders(RecordedResponse $recordedResponse): RecordedResponse
{
$sensitiveHeaders = $this->defineSensitiveHeaders();
if (empty($sensitiveHeaders)) {
return $recordedResponse;
}
$recordedResponse->headers = FixtureHelper::recursivelyReplaceAttributes($recordedResponse->headers, $sensitiveHeaders, false);
return $recordedResponse;
}
/**
* Swap any sensitive JSON data
*
* @throws JsonException
*/
protected function swapSensitiveJson(RecordedResponse $recordedResponse): RecordedResponse
{
$body = json_decode($recordedResponse->data, true);
if (empty($body) || json_last_error() !== JSON_ERROR_NONE) {
return $recordedResponse;
}
$sensitiveJsonParameters = $this->defineSensitiveJsonParameters();
if (empty($sensitiveJsonParameters)) {
return $recordedResponse;
}
$redactedData = FixtureHelper::recursivelyReplaceAttributes($body, $sensitiveJsonParameters);
$recordedResponse->data = json_encode($redactedData, JSON_THROW_ON_ERROR);
return $recordedResponse;
}
/**
* Swap sensitive body with regex patterns
*/
protected function swapSensitiveBodyWithRegex(RecordedResponse $recordedResponse): RecordedResponse
{
$sensitiveRegexPatterns = $this->defineSensitiveRegexPatterns();
if (empty($sensitiveRegexPatterns)) {
return $recordedResponse;
}
$redactedData = FixtureHelper::replaceSensitiveRegexPatterns($recordedResponse->data, $sensitiveRegexPatterns);
$recordedResponse->data = $redactedData;
return $recordedResponse;
}
/**
* Swap any sensitive headers
*
* @return array<string, string|callable>
*/
protected function defineSensitiveHeaders(): array
{
return [];
}
/**
* Swap any sensitive JSON parameters
*
* @return array<string, string|callable>
*/
protected function defineSensitiveJsonParameters(): array
{
return [];
}
/**
* Define regex patterns that should be replaced
*
* @return array<string, string>
*/
protected function defineSensitiveRegexPatterns(): array
{
return [];
}
/**
* Hook to use before saving
*/
protected function beforeSave(RecordedResponse $recordedResponse): RecordedResponse
{
return $recordedResponse;
}
}