Skip to content
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

Query param matcher API #31

Merged
merged 4 commits into from
May 20, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/stubbing.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ $this->http->mock
->end();`
```

Additional matching methods are `queryParamExists(string $param)`, `queryParamNotExists(string $param)`,
`queryParamIs(string $param, mixed $value)`, `queryParamsExist(array $params)`, `queryParamsNotExist(array $params)`
and `queryParamsAre(array $paramMap)`.

If you have more ideas for syntactic sugar, feel free to open a pull requests.

## Pattern matching
Expand Down
48 changes: 48 additions & 0 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,54 @@ public function methodIs($matcher)
return $this;
}

public function queryParamIs($param, $matcher)
{
$this->appendMatcher($matcher, $this->extractorFactory->createParamExtractor($param));

return $this;
}

public function queryParamExists($param)
{
$this->appendMatcher(true, $this->extractorFactory->createParamExistsExtractor($param));

return $this;
}

public function queryParamNotExists($param)
{
$this->appendMatcher(false, $this->extractorFactory->createParamExistsExtractor($param));

return $this;
}

public function queryParamsAre(array $paramMap)
{
foreach ($paramMap as $param => $value) {
$this->queryParamIs($param, $value);
}

return $this;
}

public function queryParamsExist(array $params)
{
foreach ($params as $param) {
$this->queryParamExists($param);
}

return $this;
}

public function queryParamsNotExist(array $params)
{
foreach ($params as $param) {
$this->queryParamNotExists($param);
}

return $this;
}

public function callback(Closure $callback)
{
$this->appendMatcher($this->matcherFactory->closure($callback));
Expand Down
17 changes: 14 additions & 3 deletions src/Matcher/ExtractorFactory.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace InterNations\Component\HttpMock\Matcher;

use Closure;
use Symfony\Component\HttpFoundation\Request;

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

/** @return Closure */
public function createPathExtractor()
{
$basePath = $this->basePath;
Expand All @@ -23,11 +21,24 @@ public function createPathExtractor()
};
}

/** @return Closure */
public function createMethodExtractor()
{
return static function (Request $request) {
return $request->getMethod();
};
}

public function createParamExtractor($param)
{
return static function (Request $request) use ($param) {
return $request->query->get($param);
};
}

public function createParamExistsExtractor($param)
{
return static function (Request $request) use ($param) {
return $request->query->has($param);
};
}
}
33 changes: 33 additions & 0 deletions tests/PHPUnit/HttpMockPHPUnitIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,39 @@ public function testMatchRegex()
$this->assertSame('response', (string) $this->http->client->get('/')->send()->getBody());
}

public function testMatchQueryParams()
{
$this->http->mock
->when()
->queryParamExists('p1')
->queryParamIs('p2', 'v2')
->queryParamNotExists('p3')
->queryParamsExist(['p4'])
->queryParamsAre(['p5' => 'v5', 'p6' => 'v6'])
->queryParamsNotExist(['p7'])
->then()
->body('response')
->end();
$this->http->setUp();

$this->assertSame(
'response',
(string) $this->http->client->get('/?p1=&p2=v2&p4=any&p5=v5&p6=v6')->send()->getBody()
);
$this->assertEquals(
Response::HTTP_NOT_FOUND,
(string) $this->http->client->get('/?p1=&p2=v2&p3=foo')->send()->getStatusCode()
);
$this->assertEquals(
Response::HTTP_NOT_FOUND,
(string) $this->http->client->get('/?p1=')->send()->getStatusCode()
);
$this->assertEquals(
Response::HTTP_NOT_FOUND,
(string) $this->http->client->get('/?p3=foo')->send()->getStatusCode()
);
}

public function testFatalError()
{
$this->markTestSkipped('Comment in to test if fatal errors are properly handled');
Expand Down