/
RequestMatcherTest.php
66 lines (53 loc) · 1.93 KB
/
RequestMatcherTest.php
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
<?php
declare(strict_types=1);
namespace N1215\Http\RequestMatcher;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\Uri;
class RequestMatcherTest extends TestCase
{
/**
* @param bool $expectedIsSuccess
* @param array $expectedParams
* @param string $path
* @param string $method
*
* @dataProvider dataProvider_requests
*/
public function test(bool $expectedIsSuccess, array $expectedParams, string $path, string $method)
{
$matcher = new SampleMethodAndPathMatcher();
$request = new ServerRequest([], [], new Uri($path), $method);
$result = $matcher->match($request);
$this->assertEquals($expectedIsSuccess, $result->isSuccess());
$this->assertEquals($expectedParams, $result->getParams());
}
public function dataProvider_requests(): array
{
return [
[true, ['resource_name' => 'posts', 'id' =>'12345'], '/resources/posts/12345', 'GET'],
[false, [], '/resources/posts/fuga', 'GET'],
[false, [], '/resources/posts/12345', 'POST'],
];
}
}
class SampleMethodAndPathMatcher implements RequestMatcherInterface {
/** @var string */
private $method = 'GET';
/** @var string */
private $pathRegex = '|/resources/(?<resource_name>[a-z_]+)/(?<id>[0-9]+)|';
public function match(RequestInterface $request): RequestMatchResultInterface
{
if (strtolower($this->method) !== strtolower($request->getMethod())) {
return RequestMatchResult::failure();
}
if (!preg_match($this->pathRegex, $request->getUri()->getPath(), $matches)) {
return RequestMatchResult::failure();
}
$params = array_filter($matches, function($key) {
return is_string($key);
}, ARRAY_FILTER_USE_KEY);
return RequestMatchResult::success($params);
}
}