Skip to content

Commit c20a6c6

Browse files
committed
Add an option to ignore certain query parameters
1 parent aa303b6 commit c20a6c6

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
66

77
## Unreleased
88

9+
### Added
10+
- Add an option to ignore certain query parameters. See readme for more info.
11+
912
### Changed
1013
- The `ResponseBuilder` now requires PSR-17 factories instead of a PHP-HTTP factory, to align it with the `Client`. This is only a breaking change in the rare case where you provide your own factory to the `ResponseBuilder`.
1114

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![PHP from Packagist](https://img.shields.io/packagist/php-v/swisnl/php-http-fixture-client.svg)](https://packagist.org/packages/swisnl/php-http-fixture-client)
44
[![Latest Version on Packagist](https://img.shields.io/packagist/v/swisnl/php-http-fixture-client.svg)](https://packagist.org/packages/swisnl/php-http-fixture-client)
5-
[![Software License](https://img.shields.io/packagist/l/swisnl/php-http-fixture-client.svg)](https://github.com/swisnl/php-http-fixture-client/blob/master/LICENSE)
5+
[![Software License](https://img.shields.io/packagist/l/swisnl/php-http-fixture-client.svg)](https://github.com/swisnl/php-http-fixture-client/blob/master/LICENSE)
66
[![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen.svg)](https://plant.treeware.earth/swisnl/php-http-fixture-client)
77
[![Build Status](https://travis-ci.org/swisnl/php-http-fixture-client.svg?branch=master)](https://travis-ci.org/swisnl/php-http-fixture-client)
88
[![Scrutinizer Coverage](https://img.shields.io/scrutinizer/coverage/g/swisnl/php-http-fixture-client.svg)](https://scrutinizer-ci.com/g/swisnl/php-http-fixture-client/?branch=master)
@@ -57,6 +57,13 @@ Please see the following table for some examples.
5757
| | | /path/to/fixtures/example.com/api/comments.get.mock |
5858
| | | /path/to/fixtures/example.com/api/comments.mock |
5959

60+
### Ignored query parameters
61+
The `ReponseBuilder` can be instructed to ignore certain query parameters using `setIgnoredQueryParameters([...])`.
62+
When configured, the provided parameters will be ignored when transforming requests to file paths.
63+
You should only provide the parameter name, not the value.
64+
This allows you to ignore 'dynamic' parameters that change in each test execution.
65+
Parameters are matched strictly, so 'foo' will match 'foo=bar', but not 'foo[]=bar'.
66+
6067
### Strict mode
6168
The `ReponseBuilder` can be set to strict mode using `setStrictMode(true)`.
6269
When in strict mode, only the first possible fixture path will be used.
@@ -68,7 +75,7 @@ This means that both the method and query params must be present in the fixture
6875
### Body
6976

7077
The body of a request is loaded directly from a fixture with the file extension _.mock_.
71-
The contents of this file can be anything that is a valid HTTP response, e.g. HTML, JSON or even images.
78+
The contents of this file can be anything that is a valid HTTP response, e.g. HTML, JSON or even images.
7279
If a fixture can not be found, a `MockNotFoundException` will be thrown.
7380
This exception has a convenience method `getPossiblePaths()` which lists all file paths that were checked, in order of specificity.
7481

@@ -120,4 +127,4 @@ This package is [Treeware](https://treeware.earth). If you use it in production,
120127

121128
## SWIS :heart: Open Source
122129

123-
[SWIS](https://www.swis.nl) is a web agency from Leiden, the Netherlands. We love working with open source software.
130+
[SWIS](https://www.swis.nl) is a web agency from Leiden, the Netherlands. We love working with open source software.

src/ResponseBuilder.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class ResponseBuilder implements ResponseBuilderInterface
5252
*/
5353
private $strictMode = false;
5454

55+
/**
56+
* @var array
57+
*/
58+
private $ignoredQueryParameters = [];
59+
5560
/**
5661
* @param string $fixturesPath
5762
* @param array $domainAliases
@@ -90,6 +95,26 @@ public function setStrictMode(bool $strictMode): self
9095
return $this;
9196
}
9297

98+
/**
99+
* @return array
100+
*/
101+
public function getIgnoredQueryParameters(): array
102+
{
103+
return $this->ignoredQueryParameters;
104+
}
105+
106+
/**
107+
* @param array $ignoredQueryParameters
108+
*
109+
* @return self
110+
*/
111+
public function setIgnoredQueryParameters(array $ignoredQueryParameters): self
112+
{
113+
$this->ignoredQueryParameters = $ignoredQueryParameters;
114+
115+
return $this;
116+
}
117+
93118
/**
94119
* @param \Psr\Http\Message\RequestInterface $request
95120
*
@@ -272,7 +297,12 @@ protected function getMethodFromRequest(RequestInterface $request): string
272297
protected function getQueryFromRequest(RequestInterface $request, string $replacement = '-'): string
273298
{
274299
$query = urldecode($request->getUri()->getQuery());
275-
$parts = explode('&', $query);
300+
$parts = array_filter(
301+
explode('&', $query),
302+
function (string $part) {
303+
return !$this->isQueryPartIgnored($part);
304+
}
305+
);
276306
sort($parts);
277307
$query = implode('&', $parts);
278308

@@ -283,6 +313,22 @@ protected function getQueryFromRequest(RequestInterface $request, string $replac
283313
->removeRight($replacement);
284314
}
285315

316+
/**
317+
* @param string $part
318+
*
319+
* @return bool
320+
*/
321+
protected function isQueryPartIgnored(string $part): bool
322+
{
323+
foreach ($this->getIgnoredQueryParameters() as $parameter) {
324+
if ($part === $parameter || strncmp($part, $parameter.'=', strlen($parameter) + 1) === 0) {
325+
return true;
326+
}
327+
}
328+
329+
return false;
330+
}
331+
286332
/**
287333
* @return string
288334
*/

tests/ResponseBuilderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ public function itCanBuildAResponseUsingDomainAliases(): void
128128
$this->assertEquals($expectedResponse->getBody()->__toString(), $actualResponse->getBody()->__toString());
129129
}
130130

131+
/**
132+
* @test
133+
*/
134+
public function itCanBuildAResponseWithIgnoredParameters(): void
135+
{
136+
// arrange
137+
$requestFactory = Psr17FactoryDiscovery::findRequestFactory();
138+
$responseFactory = Psr17FactoryDiscovery::findResponseFactory();
139+
$builder = $this->getBuilder()->setIgnoredQueryParameters(['ignore-me', 'ignore-me-too']);
140+
141+
$expectedResponse = $responseFactory->createResponse()
142+
->withBody(Utils::streamFor(file_get_contents($this->getFixturesPath().'/example.com/api/articles.mock')));
143+
144+
// act
145+
$actualResponse = $builder->build($requestFactory->createRequest('GET', 'https://example.com/api/articles?ignore-me=true&ignore-me-too'));
146+
147+
// assert
148+
$this->assertEquals($expectedResponse->getBody()->__toString(), $actualResponse->getBody()->__toString());
149+
}
150+
131151
/**
132152
* @test
133153
*/

0 commit comments

Comments
 (0)