-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
RouteTest.php
191 lines (148 loc) · 5.11 KB
/
RouteTest.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
declare(strict_types=1);
namespace Yiisoft\Router\Tests;
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\Route;
use Yiisoft\Router\Tests\Support\Container;
use Yiisoft\Router\Tests\Support\TestController;
final class RouteTest extends TestCase
{
public function testName(): void
{
$route = Route::get('/')->name('test.route');
$this->assertSame('test.route', $route->getName());
}
public function testNameDefault(): void
{
$route = Route::get('/');
$this->assertSame('GET /', $route->getName());
}
public function testMethods(): void
{
$route = Route::methods([Method::POST, Method::HEAD], '/');
$this->assertSame([Method::POST, Method::HEAD], $route->getMethods());
}
public const PATCH = 'PATCH';
public const HEAD = 'HEAD';
public const OPTIONS = 'OPTIONS';
public function testGetMethod(): void
{
$route = Route::get('/');
$this->assertSame([Method::GET], $route->getMethods());
}
public function testPostMethod(): void
{
$route = Route::post('/');
$this->assertSame([Method::POST], $route->getMethods());
}
public function testPutMethod(): void
{
$route = Route::put('/');
$this->assertSame([Method::PUT], $route->getMethods());
}
public function testDeleteMethod(): void
{
$route = Route::delete('/');
$this->assertSame([Method::DELETE], $route->getMethods());
}
public function testPatchMethod(): void
{
$route = Route::patch('/');
$this->assertSame([Method::PATCH], $route->getMethods());
}
public function testHeadMethod(): void
{
$route = Route::head('/');
$this->assertSame([Method::HEAD], $route->getMethods());
}
public function testOptionsMethod(): void
{
$route = Route::options('/');
$this->assertSame([Method::OPTIONS], $route->getMethods());
}
public function testAnyMethod(): void
{
$route = Route::anyMethod('/');
$this->assertSame(Method::ANY, $route->getMethods());
}
public function testPattern(): void
{
$route = Route::get('/test')->pattern('/test2');
$this->assertSame('/test2', $route->getPattern());
}
public function testHost(): void
{
$route = Route::get('/')->host('https://yiiframework.com/');
$this->assertSame('https://yiiframework.com', $route->getHost());
}
public function testDefaults(): void
{
$route = Route::get('/{language}')->defaults(['language' => 'en']);
$this->assertSame(['language' => 'en'], $route->getDefaults());
}
public function testOverride(): void
{
$route = Route::get('/')->override();
$this->assertTrue($route->isOverride());
}
public function testToString(): void
{
$route = Route::methods([Method::GET, Method::POST], '/')->name('test.route')->host('yiiframework.com');
$this->assertSame('[test.route] GET,POST yiiframework.com/', (string)$route);
}
public function testToStringSimple(): void
{
$route = Route::get('/');
$this->assertSame('GET /', (string)$route);
}
public function testDispatcherInjecting(): void
{
$request = new ServerRequest('GET', '/');
$container = $this->getContainer(
[
TestController::class => new TestController(),
]
);
$dispatcher = $this->getDispatcher($container);
$route = Route::get('/')->action([TestController::class, 'index']);
$route->injectDispatcher($dispatcher);
$response = $route->getDispatcherWithMiddlewares()->dispatch($request, $this->getRequestHandler());
$this->assertSame(200, $response->getStatusCode());
}
private function getRequestHandler(): RequestHandlerInterface
{
return new class() implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new Response(404);
}
};
}
private function getDispatcher(ContainerInterface $container = null): MiddlewareDispatcher
{
if ($container === null) {
return new MiddlewareDispatcher(
new MiddlewareFactory($this->getContainer()),
$this->createMock(EventDispatcherInterface::class)
);
}
return new MiddlewareDispatcher(
new MiddlewareFactory($container),
$this->createMock(EventDispatcherInterface::class)
);
}
private function getContainer(array $instances = []): ContainerInterface
{
return new Container($instances);
}
}