-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndpointTestCases.php
More file actions
108 lines (95 loc) · 3.08 KB
/
EndpointTestCases.php
File metadata and controls
108 lines (95 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
declare(strict_types=1);
namespace Firehed\API\Traits;
use Firehed\API\Interfaces\EndpointInterface;
use Firehed\Input\Interfaces\ValidationInterface;
use Firehed\Input\ValidationTestTrait;
/**
* Default test cases to be run against any object implementing
* EndpointInterface. This amounts to glorified type-hinting, but still
* provides valuable automated coverage that would otherwise only be available
* at runtime
*/
trait EndpointTestCases
{
use HandlesOwnErrorsTestCases;
use Testing\PHPUnit8Shim;
use ValidationTestTrait;
/**
* Get the endpoint under test
* @return Interfaces\EndpointInterface
*/
abstract protected function getEndpoint(): EndpointInterface;
/**
* Because EndpointInterface extends ValidationInterface, provide the same
* object to the parent handler
*
* @return \Firehed\Input\Interfaces\ValidationInterface
*/
protected function getValidation(): ValidationInterface
{
return $this->getEndpoint();
}
/**
* @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();
$this->assertIsString(
$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 */
public function testGetMethod()
{
$method = $this->getEndpoint()->getMethod();
$this->assertInstanceOf(
'Firehed\API\Enums\HTTPMethod',
$method,
'getMethod did not return an HTTPMethod enum'
);
}
}