Passed
Push — master ( 0542bf...f1358e )
by Alexander
01:34 queued 14s
created

RouteTest::testToMiddleware()

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 6
c 3
b 1
f 0
dl 0
loc 17
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A RouteTest.php$0 ➔ process() 0 5 1
1
<?php
2
3
namespace Yiisoft\Router\Tests;
4
5
use Nyholm\Psr7\Response;
6
use Nyholm\Psr7\ServerRequest;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\Router\Method;
13
use Yiisoft\Router\Route;
14
15
final class RouteTest extends TestCase
16
{
17
    public function testName(): void
18
    {
19
        $route = Route::get('/')->name('test.route');
20
21
        $this->assertSame('test.route', $route->getName());
22
    }
23
24
    public function testNameDefault(): void
25
    {
26
        $route = Route::get('/');
27
28
        $this->assertSame('GET /', $route->getName());
29
    }
30
31
    public function testMethods(): void
32
    {
33
        $route = Route::methods([Method::POST, Method::HEAD], '/');
34
35
        $this->assertSame([Method::POST, Method::HEAD], $route->getMethods());
36
    }
37
38
    public const PATCH = 'PATCH';
39
    public const HEAD = 'HEAD';
40
    public const OPTIONS = 'OPTIONS';
41
42
    public function testGetMethod(): void
43
    {
44
        $route = Route::get('/');
45
46
        $this->assertSame([Method::GET], $route->getMethods());
47
    }
48
49
    public function testPostMethod(): void
50
    {
51
        $route = Route::post('/');
52
53
        $this->assertSame([Method::POST], $route->getMethods());
54
    }
55
56
    public function testPutMethod(): void
57
    {
58
        $route = Route::put('/');
59
60
        $this->assertSame([Method::PUT], $route->getMethods());
61
    }
62
63
    public function testDeleteMethod(): void
64
    {
65
        $route = Route::delete('/');
66
67
        $this->assertSame([Method::DELETE], $route->getMethods());
68
    }
69
70
    public function testPatchMethod(): void
71
    {
72
        $route = Route::patch('/');
73
74
        $this->assertSame([Method::PATCH], $route->getMethods());
75
    }
76
77
    public function testHeadMethod(): void
78
    {
79
        $route = Route::head('/');
80
81
        $this->assertSame([Method::HEAD], $route->getMethods());
82
    }
83
84
    public function testOptionsMethod(): void
85
    {
86
        $route = Route::options('/');
87
88
        $this->assertSame([Method::OPTIONS], $route->getMethods());
89
    }
90
91
    public function testAnyMethod(): void
92
    {
93
        $route = Route::anyMethod('/');
94
95
        $this->assertSame(Method::ANY, $route->getMethods());
96
    }
97
98
    public function testPattern(): void
99
    {
100
        $route = Route::get('/test')->pattern('/test2');
101
102
        $this->assertSame('/test2', $route->getPattern());
103
    }
104
105
    public function testHost(): void
106
    {
107
        $route = Route::get('/')->host('https://yiiframework.com/');
108
109
        $this->assertSame('https://yiiframework.com', $route->getHost());
110
    }
111
112
    public function testParameters(): void
113
    {
114
        $route = Route::get('/{language}')->parameters(['language' => 'en']);
115
116
        $this->assertSame(['language' => 'en'], $route->getParameters());
117
    }
118
119
    public function testDefaults(): void
120
    {
121
        $route = Route::get('/{language}')->defaults(['language' => 'en']);
122
123
        $this->assertSame(['language' => 'en'], $route->getDefaults());
124
    }
125
126
    public function testToString(): void
127
    {
128
        $route = Route::methods([Method::GET, Method::POST], '/')->name('test.route')->host('yiiframework.com');
129
130
        $this->assertSame('[test.route] GET,POST yiiframework.com/', (string)$route);
131
    }
132
133
    public function testToStringSimple(): void
134
    {
135
        $route = Route::get('/');
136
137
        $this->assertSame('GET /', (string)$route);
138
    }
139
140
    public function testInvalidTo(): void
141
    {
142
        $this->expectException(\InvalidArgumentException::class);
143
        Route::get('/')->to(new \stdClass());
144
    }
145
146
    public function testToMiddleware(): void
147
    {
148
        $request = new ServerRequest('GET', '/');
149
150
        $route = Route::get('/')->to(
151
            new class implements MiddlewareInterface {
152
                public function process(
153
                    ServerRequestInterface $request,
154
                    RequestHandlerInterface $handler
155
                ): ResponseInterface {
156
                    return (new Response())->withStatus(418);
157
                }
158
            }
159
        );
160
161
        $response = $route->process($request, $this->getRequestHandler());
162
        $this->assertSame(418, $response->getStatusCode());
163
    }
164
165
    public function testToCallable(): void
166
    {
167
        $request = new ServerRequest('GET', '/');
168
169
        $route = Route::get('/')->to(
170
            static function (): ResponseInterface {
171
                return (new Response())->withStatus(418);
172
            }
173
        );
174
175
        $response = $route->process($request, $this->getRequestHandler());
176
        $this->assertSame(418, $response->getStatusCode());
177
    }
178
179
    public function testThen(): void
180
    {
181
        $request = new ServerRequest('GET', '/');
182
183
        $route = Route::get('/');
184
185
        $middleware1 = $this->createMock(MiddlewareInterface::class);
186
        $middleware2 = $this->createMock(MiddlewareInterface::class);
187
188
        $route = $route->to($middleware1)->then($middleware2);
189
190
        $middleware1
191
            ->expects($this->at(0))
192
            ->method('process')
193
            ->with($request, $route);
194
195
        // TODO: test that second one is called as well
196
197
        $route->process($request, $this->getRequestHandler());
198
    }
199
200
    public function testBefore(): void
201
    {
202
        $request = new ServerRequest('GET', '/');
203
204
        $route = Route::get('/');
205
206
        $middleware1 = $this->createMock(MiddlewareInterface::class);
207
        $middleware2 = $this->createMock(MiddlewareInterface::class);
208
209
        $route = $route->to($middleware1)->prepend($middleware2);
210
211
        $middleware2
212
            ->expects($this->at(0))
213
            ->method('process')
214
            ->with($request, $route);
215
216
        // TODO: test that first one is called as well
217
218
        $route->process($request, $this->getRequestHandler());
219
    }
220
221
    private function getRequestHandler(): RequestHandlerInterface
222
    {
223
        return new class implements RequestHandlerInterface {
224
            public function handle(ServerRequestInterface $request): ResponseInterface
225
            {
226
                return new Response(404);
227
            }
228
        };
229
    }
230
}
231