diff --git a/README.md b/README.md index 72eaf2f..659b427 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,11 @@ Please see the following table for some examples. | | | /path/to/fixtures/example.com/api/comments.get.mock | | | | /path/to/fixtures/example.com/api/comments.mock | +### Strict mode +The `ReponseBuilder` can be set to strict mode using `setStrictMode(true)`. +When in strict mode, only the first possible fixture path will be used. +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. + ### Helper Please see https://swisnl.github.io/php-http-fixture-client/#helper for the URL helper. diff --git a/docs/.vuepress/components/UrlHelper.vue b/docs/.vuepress/components/UrlHelper.vue index cb3c2e6..50ae8af 100644 --- a/docs/.vuepress/components/UrlHelper.vue +++ b/docs/.vuepress/components/UrlHelper.vue @@ -15,6 +15,8 @@ +

Strict mode

+

Possible fixtures (in order of specificity)

  1. /path/to/fixtures/{{ fixture }}
  2. @@ -196,7 +198,8 @@ data() { return { url: '', - method: 'get' + method: 'get', + strictMode: false }; }, @@ -223,6 +226,10 @@ fixtures.push(`${url.hostname}${pathname}.${this.method}.mock`); fixtures.push(`${url.hostname}${pathname}.mock`); + if (this.strictMode) { + fixtures = fixtures.slice(0, 1); + } + return fixtures; } }, diff --git a/src/ResponseBuilder.php b/src/ResponseBuilder.php index 3c08609..0c5defe 100644 --- a/src/ResponseBuilder.php +++ b/src/ResponseBuilder.php @@ -40,6 +40,11 @@ class ResponseBuilder implements ResponseBuilderInterface */ private $responseFactory; + /** + * @var bool + */ + private $strictMode = false; + /** * @param string $fixturesPath * @param array $domainAliases @@ -52,6 +57,26 @@ public function __construct(string $fixturesPath, array $domainAliases = [], Res $this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find(); } + /** + * @return bool + */ + public function useStrictMode(): bool + { + return $this->strictMode; + } + + /** + * @param bool $strictMode + * + * @return self + */ + public function setStrictMode(bool $strictMode): self + { + $this->strictMode = $strictMode; + + return $this; + } + /** * @param \Psr\Http\Message\RequestInterface $request * @@ -184,6 +209,10 @@ protected function getPossibleMockFilePathsForRequest(RequestInterface $request, $possibleFiles[] = implode('.', [$basePathToFile, $method, $type]); $possibleFiles[] = implode('.', [$basePathToFile, $type]); + if ($this->useStrictMode()) { + $possibleFiles = array_slice($possibleFiles, 0, 1); + } + return $possibleFiles; } diff --git a/tests/ResponseBuilderTest.php b/tests/ResponseBuilderTest.php index bc8dea4..d929bd2 100644 --- a/tests/ResponseBuilderTest.php +++ b/tests/ResponseBuilderTest.php @@ -62,6 +62,29 @@ public function getResponses(): array ]; } + /** + * @test + */ + public function it_can_be_set_to_strict_mode() + { + $builder = $this->getBuilder(); + + // Strict mode off + $this->assertFalse($builder->useStrictMode()); + + $messageFactory = MessageFactoryDiscovery::find(); + $builder->build($messageFactory->createRequest('POST', 'http://example.com/api/articles?foo=bar')); + + // Strict mode on + $builder->setStrictMode(true); + $this->assertTrue($builder->useStrictMode()); + + $this->expectException(MockNotFoundException::class); + + $messageFactory = MessageFactoryDiscovery::find(); + $builder->build($messageFactory->createRequest('POST', 'http://example.com/api/articles?foo=bar')); + } + /** * @test */