Skip to content

Commit

Permalink
#30 prototype for the quer param matcher API
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed Apr 27, 2016
1 parent 623f10e commit 4b65243
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ 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 callback(Closure $callback)
{
$this->appendMatcher($this->matcherFactory->closure($callback));
Expand Down
16 changes: 14 additions & 2 deletions src/Matcher/ExtractorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public function __construct($basePath = '')
$this->basePath = rtrim($basePath, '/');
}

/** @return Closure */
public function createPathExtractor()
{
$basePath = $this->basePath;
Expand All @@ -22,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);
};
}
}
27 changes: 27 additions & 0 deletions tests/PHPUnit/HttpMockPHPUnitIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,33 @@ 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')
->then()
->body('response')
->end();
$this->http->setUp();

$this->assertSame('response', (string) $this->http->client->get('/?p1=&p2=v2')->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

0 comments on commit 4b65243

Please sign in to comment.