Skip to content

Commit

Permalink
Add URI matching into endpoint test case trait (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Firehed authored Jan 3, 2018
1 parent 9e05030 commit d7333a4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [3.0.2] - 2018-01-03
- Added URI matching tests into `EndpointTestCases`. Updating to this version will result in existing passing tests using said trait being skipped until good and bad URI matches are added into the test case.

## [3.0.1] - 2017-12-01

### Changed
Expand Down
55 changes: 51 additions & 4 deletions src/Traits/EndpointTestCases.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,63 @@ protected function getValidation(): ValidationInterface
return $this->getEndpoint();
}

/** @covers ::getUri */
public function testGetUri()
/**
* @covers ::getUri
* @dataProvider uris
*
* @param string $uri The URI to match against
* @param bool $match Whether or not the provied URI should match
* @param array $expectedMatches Named captures in a positive match
*/
public function testGetUri(string $uri, bool $match, array $expectedMatches)
{
$endpoint = $this->getEndpoint();
$uri = $endpoint->getUri();
$this->assertInternalType(
'string',
$uri,
$endpoint->getUri(),
'getUri did not return a string'
);

$pattern = '#^' . $endpoint->getUri() . '$#';

$this->assertSame($match, (bool) preg_match($pattern, $uri, $matches));
foreach ($expectedMatches as $key => $value) {
$this->assertTrue(array_key_exists($key, $matches));
$this->assertSame($value, $matches[$key]);
}
}

public function uris(): array
{
$good = $this->goodUris();
$bad = $this->badUris();
if (!$good || !$bad) {
$message = <<<'TEXT'
No URIs provided to validate. To provide URIs, add methods `goodUris()` and
`badUris()` to your test case class. `goodUris()` should return a map of URI to
named captures; e.g. ['/some/uri' => ['paramName' => 'uri']]. `badURIs()`
should return an array of strings; e.g. ['/some/non/matching/path'].
TEXT;
$this->markTestSkipped($message);
}
return array_merge(
array_map(function ($uri, $matches) {
return [$uri, true, $matches];
}, array_keys($good), array_values($good)),
array_map(function ($uri) {
return [$uri, false, []];
}, $bad)
);
}

protected function goodUris(): array
{
return [];
}

protected function badUris(): array
{
return [];
}

/** @covers ::getMethod */
Expand Down
48 changes: 45 additions & 3 deletions tests/Traits/EndpointTestCasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
* @covers Firehed\API\Traits\EndpointTestCases::testGetMethod
* @covers Firehed\API\Traits\EndpointTestCases::testHandleException
*/
class EndpointTestTraitTest extends \PHPUnit\Framework\TestCase
class EndpointTestCasesTest extends \PHPUnit\Framework\TestCase
{

use EndpointTestCases;
use EndpointTestCases {
goodUris as baseGoodUris;
badUris as baseBadUris;
}

protected function getEndpoint(): EndpointInterface
{
Expand All @@ -35,7 +38,46 @@ public function testExceptionsToHandle()
$data = $this->exceptionsToHandle();
foreach ($data as $testCase) {
list($testParam) = $testCase;
$this->assertInstanceOf(Throwable::Class, $testParam);
$this->assertInstanceOf(Throwable::class, $testParam);
}
}

/**
* @covers Firehed\API\Traits\EndpointTestCases::uris
*/
public function testUris()
{
$data = $this->uris();
foreach ($data as $testCase) {
list($uri, $shouldMatch, $matches) = $testCase;
$this->assertInternalType('string', $uri);
$this->assertInternalType('bool', $shouldMatch);
$this->assertInternalType('array', $matches);
}
}

public function goodUris(): array
{
return $this->baseGoodUris() + [
'/user/1' => ['id' => '1'],
'/user/10' => ['id' => '10'],
'/user/3' => ['id' => '3'],
'/user/134098435089225' => ['id' => '134098435089225'],
];
}

public function badUris(): array
{
return $this->baseBadUris() + [
'/',
'/0/user/',
'/3/user',
'user/3',
'/user',
'/user/',
'/user/0',
'/user/01234',
'/user/username',
];
}
}

0 comments on commit d7333a4

Please sign in to comment.