Skip to content

Commit eaca31b

Browse files
authored
Merge pull request #3 from swisnl/feature/strict-mode
Strict mode
2 parents ba9ed53 + 7de990a commit eaca31b

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ Please see the following table for some examples.
5555
| | | /path/to/fixtures/example.com/api/comments.get.mock |
5656
| | | /path/to/fixtures/example.com/api/comments.mock |
5757

58+
### Strict mode
59+
The `ReponseBuilder` can be set to strict mode using `setStrictMode(true)`.
60+
When in strict mode, only the first possible fixture path will be used.
61+
This means that both the method and query params must be present in the fixture file name and it does not fall back to other fixture files.
62+
5863
### Helper
5964
<UrlHelper>Please see <a href="https://swisnl.github.io/php-http-fixture-client/#helper">https://swisnl.github.io/php-http-fixture-client/#helper</a> for the URL helper.</UrlHelper>
6065

docs/.vuepress/components/UrlHelper.vue

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<option value="trace">TRACE</option>
1616
<option value="patch">PATCH</option>
1717
</select>
18+
<h4>Strict mode</h4>
19+
<label><input type="checkbox" v-model="strictMode"> Use strict mode</label>
1820
<h4>Possible fixtures (in order of specificity)</h4>
1921
<ol v-if="fixtures.length">
2022
<li v-for="fixture in fixtures">/path/to/fixtures/{{ fixture }}</li>
@@ -196,7 +198,8 @@
196198
data() {
197199
return {
198200
url: '',
199-
method: 'get'
201+
method: 'get',
202+
strictMode: false
200203
};
201204
},
202205
@@ -223,6 +226,10 @@
223226
fixtures.push(`${url.hostname}${pathname}.${this.method}.mock`);
224227
fixtures.push(`${url.hostname}${pathname}.mock`);
225228
229+
if (this.strictMode) {
230+
fixtures = fixtures.slice(0, 1);
231+
}
232+
226233
return fixtures;
227234
}
228235
},

src/ResponseBuilder.php

+29
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class ResponseBuilder implements ResponseBuilderInterface
4040
*/
4141
private $responseFactory;
4242

43+
/**
44+
* @var bool
45+
*/
46+
private $strictMode = false;
47+
4348
/**
4449
* @param string $fixturesPath
4550
* @param array $domainAliases
@@ -52,6 +57,26 @@ public function __construct(string $fixturesPath, array $domainAliases = [], Res
5257
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
5358
}
5459

60+
/**
61+
* @return bool
62+
*/
63+
public function useStrictMode(): bool
64+
{
65+
return $this->strictMode;
66+
}
67+
68+
/**
69+
* @param bool $strictMode
70+
*
71+
* @return self
72+
*/
73+
public function setStrictMode(bool $strictMode): self
74+
{
75+
$this->strictMode = $strictMode;
76+
77+
return $this;
78+
}
79+
5580
/**
5681
* @param \Psr\Http\Message\RequestInterface $request
5782
*
@@ -184,6 +209,10 @@ protected function getPossibleMockFilePathsForRequest(RequestInterface $request,
184209
$possibleFiles[] = implode('.', [$basePathToFile, $method, $type]);
185210
$possibleFiles[] = implode('.', [$basePathToFile, $type]);
186211

212+
if ($this->useStrictMode()) {
213+
$possibleFiles = array_slice($possibleFiles, 0, 1);
214+
}
215+
187216
return $possibleFiles;
188217
}
189218

tests/ResponseBuilderTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,29 @@ public function getResponses(): array
6262
];
6363
}
6464

65+
/**
66+
* @test
67+
*/
68+
public function it_can_be_set_to_strict_mode()
69+
{
70+
$builder = $this->getBuilder();
71+
72+
// Strict mode off
73+
$this->assertFalse($builder->useStrictMode());
74+
75+
$messageFactory = MessageFactoryDiscovery::find();
76+
$builder->build($messageFactory->createRequest('POST', 'http://example.com/api/articles?foo=bar'));
77+
78+
// Strict mode on
79+
$builder->setStrictMode(true);
80+
$this->assertTrue($builder->useStrictMode());
81+
82+
$this->expectException(MockNotFoundException::class);
83+
84+
$messageFactory = MessageFactoryDiscovery::find();
85+
$builder->build($messageFactory->createRequest('POST', 'http://example.com/api/articles?foo=bar'));
86+
}
87+
6588
/**
6689
* @test
6790
*/

0 commit comments

Comments
 (0)