-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
RouterTest.php
137 lines (114 loc) · 4.45 KB
/
RouterTest.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
declare(strict_types=1);
namespace Yiisoft\Router\Tests\Middleware;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
use Nyholm\Psr7\ServerRequest;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Yiisoft\Http\Method;
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
use Yiisoft\Middleware\Dispatcher\MiddlewareFactory;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\Router\MatchingResult;
use Yiisoft\Router\Middleware\Router;
use Yiisoft\Router\Route;
use Yiisoft\Router\UrlMatcherInterface;
final class RouterTest extends TestCase
{
private function createRouterMiddleware(?CurrentRoute $currentRoute = null): Router
{
$container = $this->createMock(ContainerInterface::class);
$dispatcher = new MiddlewareDispatcher(
new MiddlewareFactory($container),
$this->createMock(EventDispatcherInterface::class)
);
return new Router($this->getMatcher(), new Psr17Factory(), $dispatcher, $currentRoute ?? new CurrentRoute());
}
private function processWithRouter(ServerRequestInterface $request, ?CurrentRoute $currentRoute = null): ResponseInterface
{
return $this->createRouterMiddleware($currentRoute)->process($request, $this->createRequestHandler());
}
public function testProcessSuccess(): void
{
$request = new ServerRequest('GET', '/');
$response = $this->processWithRouter($request);
$this->assertSame(201, $response->getStatusCode());
}
public function testMissingRouteRespondWith404(): void
{
$request = new ServerRequest('GET', '/no-such-route');
$response = $this->processWithRouter($request);
$this->assertSame(404, $response->getStatusCode());
}
public function testMethodMismatchRespondWith405(): void
{
$request = new ServerRequest('POST', '/');
$response = $this->processWithRouter($request);
$this->assertSame(405, $response->getStatusCode());
$this->assertSame('GET, HEAD', $response->getHeaderLine('Allow'));
}
public function testGetCurrentRoute(): void
{
$currentRoute = new CurrentRoute();
$request = new ServerRequest('GET', '/');
$this->processWithRouter($request, $currentRoute);
$this->assertInstanceOf(Route::class, $currentRoute->getRoute());
$this->assertEquals('GET /', $currentRoute->getRoute()->getName());
}
public function testGetCurrentUri(): void
{
$currentRoute = new CurrentRoute();
$request = new ServerRequest('GET', '/');
$this->processWithRouter($request, $currentRoute);
$this->assertSame($request->getUri(), $currentRoute->getUri());
}
private function getMatcher(): UrlMatcherInterface
{
$middleware = $this->createRouteMiddleware();
return new class($middleware) implements UrlMatcherInterface {
private $middleware;
public function __construct($middleware)
{
$this->middleware = $middleware;
}
/**
* Emulates router with a single `GET /` route
*
* @param ServerRequestInterface $request
*
* @return MatchingResult
*/
public function match(ServerRequestInterface $request): MatchingResult
{
if ($request->getUri()->getPath() !== '/') {
return MatchingResult::fromFailure(Method::ALL);
}
if ($request->getMethod() === 'GET') {
$route = Route::get('/')->middleware($this->middleware);
return MatchingResult::fromSuccess($route, ['parameter' => 'value']);
}
return MatchingResult::fromFailure([Method::GET, Method::HEAD]);
}
};
}
private function createRequestHandler(): RequestHandlerInterface
{
return new class() implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new Response(404);
}
};
}
private function createRouteMiddleware(): callable
{
return static function () {
return new Response(201);
};
}
}