Skip to content

Strict mode #3

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

Merged
merged 3 commits into from
Oct 5, 2018
Merged
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<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>

Expand Down
9 changes: 8 additions & 1 deletion docs/.vuepress/components/UrlHelper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<option value="trace">TRACE</option>
<option value="patch">PATCH</option>
</select>
<h4>Strict mode</h4>
<label><input type="checkbox" v-model="strictMode"> Use strict mode</label>
<h4>Possible fixtures (in order of specificity)</h4>
<ol v-if="fixtures.length">
<li v-for="fixture in fixtures">/path/to/fixtures/{{ fixture }}</li>
Expand Down Expand Up @@ -196,7 +198,8 @@
data() {
return {
url: '',
method: 'get'
method: 'get',
strictMode: false
};
},

Expand All @@ -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;
}
},
Expand Down
29 changes: 29 additions & 0 deletions src/ResponseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class ResponseBuilder implements ResponseBuilderInterface
*/
private $responseFactory;

/**
* @var bool
*/
private $strictMode = false;

/**
* @param string $fixturesPath
* @param array $domainAliases
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
}

Expand Down
23 changes: 23 additions & 0 deletions tests/ResponseBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down