Skip to content

Commit d986aff

Browse files
committed
Query param matcher API (#31)
1 parent 61f064f commit d986aff

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

doc/stubbing.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ $this->http->mock
6363
->end();`
6464
```
6565

66+
Additional matching methods are `queryParamExists(string $param)`, `queryParamNotExists(string $param)`,
67+
`queryParamIs(string $param, mixed $value)`, `queryParamsExist(array $params)`, `queryParamsNotExist(array $params)`
68+
and `queryParamsAre(array $paramMap)`.
69+
6670
If you have more ideas for syntactic sugar, feel free to open a pull requests.
6771

6872
## Pattern matching

src/Expectation.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,54 @@ public function methodIs($matcher)
5555
return $this;
5656
}
5757

58+
public function queryParamIs($param, $matcher)
59+
{
60+
$this->appendMatcher($matcher, $this->extractorFactory->createParamExtractor($param));
61+
62+
return $this;
63+
}
64+
65+
public function queryParamExists($param)
66+
{
67+
$this->appendMatcher(true, $this->extractorFactory->createParamExistsExtractor($param));
68+
69+
return $this;
70+
}
71+
72+
public function queryParamNotExists($param)
73+
{
74+
$this->appendMatcher(false, $this->extractorFactory->createParamExistsExtractor($param));
75+
76+
return $this;
77+
}
78+
79+
public function queryParamsAre(array $paramMap)
80+
{
81+
foreach ($paramMap as $param => $value) {
82+
$this->queryParamIs($param, $value);
83+
}
84+
85+
return $this;
86+
}
87+
88+
public function queryParamsExist(array $params)
89+
{
90+
foreach ($params as $param) {
91+
$this->queryParamExists($param);
92+
}
93+
94+
return $this;
95+
}
96+
97+
public function queryParamsNotExist(array $params)
98+
{
99+
foreach ($params as $param) {
100+
$this->queryParamNotExists($param);
101+
}
102+
103+
return $this;
104+
}
105+
58106
public function callback(Closure $callback)
59107
{
60108
$this->appendMatcher($this->matcherFactory->closure($callback));

src/Matcher/ExtractorFactory.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace InterNations\Component\HttpMock\Matcher;
33

4-
use Closure;
54
use Symfony\Component\HttpFoundation\Request;
65

76
class ExtractorFactory
@@ -13,7 +12,6 @@ public function __construct($basePath = '')
1312
$this->basePath = rtrim($basePath, '/');
1413
}
1514

16-
/** @return Closure */
1715
public function createPathExtractor()
1816
{
1917
$basePath = $this->basePath;
@@ -23,11 +21,24 @@ public function createPathExtractor()
2321
};
2422
}
2523

26-
/** @return Closure */
2724
public function createMethodExtractor()
2825
{
2926
return static function (Request $request) {
3027
return $request->getMethod();
3128
};
3229
}
30+
31+
public function createParamExtractor($param)
32+
{
33+
return static function (Request $request) use ($param) {
34+
return $request->query->get($param);
35+
};
36+
}
37+
38+
public function createParamExistsExtractor($param)
39+
{
40+
return static function (Request $request) use ($param) {
41+
return $request->query->has($param);
42+
};
43+
}
3344
}

tests/PHPUnit/HttpMockPHPUnitIntegrationTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,39 @@ public function testMatchRegex()
293293
$this->assertSame('response', (string) $this->http->client->get('/')->send()->getBody());
294294
}
295295

296+
public function testMatchQueryParams()
297+
{
298+
$this->http->mock
299+
->when()
300+
->queryParamExists('p1')
301+
->queryParamIs('p2', 'v2')
302+
->queryParamNotExists('p3')
303+
->queryParamsExist(['p4'])
304+
->queryParamsAre(['p5' => 'v5', 'p6' => 'v6'])
305+
->queryParamsNotExist(['p7'])
306+
->then()
307+
->body('response')
308+
->end();
309+
$this->http->setUp();
310+
311+
$this->assertSame(
312+
'response',
313+
(string) $this->http->client->get('/?p1=&p2=v2&p4=any&p5=v5&p6=v6')->send()->getBody()
314+
);
315+
$this->assertEquals(
316+
Response::HTTP_NOT_FOUND,
317+
(string) $this->http->client->get('/?p1=&p2=v2&p3=foo')->send()->getStatusCode()
318+
);
319+
$this->assertEquals(
320+
Response::HTTP_NOT_FOUND,
321+
(string) $this->http->client->get('/?p1=')->send()->getStatusCode()
322+
);
323+
$this->assertEquals(
324+
Response::HTTP_NOT_FOUND,
325+
(string) $this->http->client->get('/?p3=foo')->send()->getStatusCode()
326+
);
327+
}
328+
296329
public function testFatalError()
297330
{
298331
$this->markTestSkipped('Comment in to test if fatal errors are properly handled');

0 commit comments

Comments
 (0)