Skip to content

Commit 8cf93c2

Browse files
committed
Add an option to ignore certain query parameters
(cherry picked from commit c20a6c6)
1 parent d6aa5d7 commit 8cf93c2

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
66

77
## Unreleased
88

9-
- Nothing
9+
### Added
10+
- Add an option to ignore certain query parameters. See readme for more info.
1011

1112
## [2.3.2] - 2021-08-30
1213

README.md

+10-3
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

+47-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ class ResponseBuilder implements ResponseBuilderInterface
4545
*/
4646
private $strictMode = false;
4747

48+
/**
49+
* @var array
50+
*/
51+
private $ignoredQueryParameters = [];
52+
4853
/**
4954
* @param string $fixturesPath
5055
* @param array $domainAliases
@@ -77,6 +82,26 @@ public function setStrictMode(bool $strictMode): self
7782
return $this;
7883
}
7984

85+
/**
86+
* @return array
87+
*/
88+
public function getIgnoredQueryParameters(): array
89+
{
90+
return $this->ignoredQueryParameters;
91+
}
92+
93+
/**
94+
* @param array $ignoredQueryParameters
95+
*
96+
* @return self
97+
*/
98+
public function setIgnoredQueryParameters(array $ignoredQueryParameters): self
99+
{
100+
$this->ignoredQueryParameters = $ignoredQueryParameters;
101+
102+
return $this;
103+
}
104+
80105
/**
81106
* @param \Psr\Http\Message\RequestInterface $request
82107
*
@@ -256,7 +281,12 @@ protected function getMethodFromRequest(RequestInterface $request): string
256281
protected function getQueryFromRequest(RequestInterface $request, string $replacement = '-'): string
257282
{
258283
$query = urldecode($request->getUri()->getQuery());
259-
$parts = explode('&', $query);
284+
$parts = array_filter(
285+
explode('&', $query),
286+
function (string $part) {
287+
return !$this->isQueryPartIgnored($part);
288+
}
289+
);
260290
sort($parts);
261291
$query = implode('&', $parts);
262292

@@ -267,6 +297,22 @@ protected function getQueryFromRequest(RequestInterface $request, string $replac
267297
->removeRight($replacement);
268298
}
269299

300+
/**
301+
* @param string $part
302+
*
303+
* @return bool
304+
*/
305+
protected function isQueryPartIgnored(string $part): bool
306+
{
307+
foreach ($this->getIgnoredQueryParameters() as $parameter) {
308+
if ($part === $parameter || strncmp($part, $parameter.'=', strlen($parameter) + 1) === 0) {
309+
return true;
310+
}
311+
}
312+
313+
return false;
314+
}
315+
270316
/**
271317
* @return string
272318
*/

tests/ResponseBuilderTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ public function itCanBuildAResponseUsingDomainAliases()
126126
$this->assertEquals($expectedResponse->getBody()->__toString(), $actualResponse->getBody()->__toString());
127127
}
128128

129+
/**
130+
* @test
131+
*/
132+
public function itCanBuildAResponseWithIgnoredParameters(): void
133+
{
134+
// arrange
135+
$messageFactory = MessageFactoryDiscovery::find();
136+
$builder = $this->getBuilder()->setIgnoredQueryParameters(['ignore-me', 'ignore-me-too']);
137+
138+
$expectedResponse = $messageFactory->createResponse()
139+
->withBody(Utils::streamFor(file_get_contents($this->getFixturesPath().'/example.com/api/articles.mock')));
140+
141+
// act
142+
$actualResponse = $builder->build($messageFactory->createRequest('GET', 'https://example.com/api/articles?ignore-me=true&ignore-me-too'));
143+
144+
// assert
145+
$this->assertEquals($expectedResponse->getBody()->__toString(), $actualResponse->getBody()->__toString());
146+
}
147+
129148
/**
130149
* @test
131150
*/

0 commit comments

Comments
 (0)