Skip to content

Commit d7333a4

Browse files
authored
Add URI matching into endpoint test case trait (#17)
1 parent 9e05030 commit d7333a4

File tree

3 files changed

+99
-7
lines changed

3 files changed

+99
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [3.0.2] - 2018-01-03
8+
- 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.
9+
710
## [3.0.1] - 2017-12-01
811

912
### Changed

src/Traits/EndpointTestCases.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,63 @@ protected function getValidation(): ValidationInterface
3535
return $this->getEndpoint();
3636
}
3737

38-
/** @covers ::getUri */
39-
public function testGetUri()
38+
/**
39+
* @covers ::getUri
40+
* @dataProvider uris
41+
*
42+
* @param string $uri The URI to match against
43+
* @param bool $match Whether or not the provied URI should match
44+
* @param array $expectedMatches Named captures in a positive match
45+
*/
46+
public function testGetUri(string $uri, bool $match, array $expectedMatches)
4047
{
4148
$endpoint = $this->getEndpoint();
42-
$uri = $endpoint->getUri();
4349
$this->assertInternalType(
4450
'string',
45-
$uri,
51+
$endpoint->getUri(),
4652
'getUri did not return a string'
4753
);
54+
55+
$pattern = '#^' . $endpoint->getUri() . '$#';
56+
57+
$this->assertSame($match, (bool) preg_match($pattern, $uri, $matches));
58+
foreach ($expectedMatches as $key => $value) {
59+
$this->assertTrue(array_key_exists($key, $matches));
60+
$this->assertSame($value, $matches[$key]);
61+
}
62+
}
63+
64+
public function uris(): array
65+
{
66+
$good = $this->goodUris();
67+
$bad = $this->badUris();
68+
if (!$good || !$bad) {
69+
$message = <<<'TEXT'
70+
No URIs provided to validate. To provide URIs, add methods `goodUris()` and
71+
`badUris()` to your test case class. `goodUris()` should return a map of URI to
72+
named captures; e.g. ['/some/uri' => ['paramName' => 'uri']]. `badURIs()`
73+
should return an array of strings; e.g. ['/some/non/matching/path'].
74+
TEXT;
75+
$this->markTestSkipped($message);
76+
}
77+
return array_merge(
78+
array_map(function ($uri, $matches) {
79+
return [$uri, true, $matches];
80+
}, array_keys($good), array_values($good)),
81+
array_map(function ($uri) {
82+
return [$uri, false, []];
83+
}, $bad)
84+
);
85+
}
86+
87+
protected function goodUris(): array
88+
{
89+
return [];
90+
}
91+
92+
protected function badUris(): array
93+
{
94+
return [];
4895
}
4996

5097
/** @covers ::getMethod */

tests/Traits/EndpointTestCasesTest.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
* @covers Firehed\API\Traits\EndpointTestCases::testGetMethod
1818
* @covers Firehed\API\Traits\EndpointTestCases::testHandleException
1919
*/
20-
class EndpointTestTraitTest extends \PHPUnit\Framework\TestCase
20+
class EndpointTestCasesTest extends \PHPUnit\Framework\TestCase
2121
{
2222

23-
use EndpointTestCases;
23+
use EndpointTestCases {
24+
goodUris as baseGoodUris;
25+
badUris as baseBadUris;
26+
}
2427

2528
protected function getEndpoint(): EndpointInterface
2629
{
@@ -35,7 +38,46 @@ public function testExceptionsToHandle()
3538
$data = $this->exceptionsToHandle();
3639
foreach ($data as $testCase) {
3740
list($testParam) = $testCase;
38-
$this->assertInstanceOf(Throwable::Class, $testParam);
41+
$this->assertInstanceOf(Throwable::class, $testParam);
42+
}
43+
}
44+
45+
/**
46+
* @covers Firehed\API\Traits\EndpointTestCases::uris
47+
*/
48+
public function testUris()
49+
{
50+
$data = $this->uris();
51+
foreach ($data as $testCase) {
52+
list($uri, $shouldMatch, $matches) = $testCase;
53+
$this->assertInternalType('string', $uri);
54+
$this->assertInternalType('bool', $shouldMatch);
55+
$this->assertInternalType('array', $matches);
3956
}
4057
}
58+
59+
public function goodUris(): array
60+
{
61+
return $this->baseGoodUris() + [
62+
'/user/1' => ['id' => '1'],
63+
'/user/10' => ['id' => '10'],
64+
'/user/3' => ['id' => '3'],
65+
'/user/134098435089225' => ['id' => '134098435089225'],
66+
];
67+
}
68+
69+
public function badUris(): array
70+
{
71+
return $this->baseBadUris() + [
72+
'/',
73+
'/0/user/',
74+
'/3/user',
75+
'user/3',
76+
'/user',
77+
'/user/',
78+
'/user/0',
79+
'/user/01234',
80+
'/user/username',
81+
];
82+
}
4183
}

0 commit comments

Comments
 (0)