Passed
Push — master ( 21057b...383a4e )
by Alexander
01:22
created

RouteTest.php$0 ➔ testBefore()   A

Complexity

Conditions 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.6333
cc 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 testInvalidTo(): void
134
    {
135
        $this->expectException(\InvalidArgumentException::class);
136
        Route::get('/')->to(new \stdClass());
137
    }
138
139
    public function testToMiddleware(): void
140
    {
141
        $request = new ServerRequest('GET', '/');
142
143
        $route = Route::get('/')->to(
144
            new class implements MiddlewareInterface {
145
                public function process(
146
                    ServerRequestInterface $request,
147
                    RequestHandlerInterface $handler
148
                ): ResponseInterface {
149
                    return (new Response())->withStatus(418);
150
                }
151
            }
152
        );
153
154
        $response = $route->process($request, $this->getRequestHandler());
155
        $this->assertSame(418, $response->getStatusCode());
156
    }
157
158
    public function testToCallable(): void
159
    {
160
        $request = new ServerRequest('GET', '/');
161
162
        $route = Route::get('/')->to(
163
            static function (): ResponseInterface {
164
                return (new Response())->withStatus(418);
165
            }
166
        );
167
168
        $response = $route->process($request, $this->getRequestHandler());
169
        $this->assertSame(418, $response->getStatusCode());
170
    }
171
172
    public function testThen(): void
173
    {
174
        $request = new ServerRequest('GET', '/');
175
176
        $route = Route::get('/');
177
178
        $middleware1 = $this->createMock(MiddlewareInterface::class);
179
        $middleware2 = $this->createMock(MiddlewareInterface::class);
180
181
        $route = $route->to($middleware1)->then($middleware2);
182
183
        $middleware1
184
            ->expects($this->at(0))
185
            ->method('process')
186
            ->with($request, $route);
187
188
        // TODO: test that second one is called as well
189
190
        $route->process($request, $this->getRequestHandler());
191
    }
192
193
    public function testBefore(): void
194
    {
195
        $request = new ServerRequest('GET', '/');
196
197
        $route = Route::get('/');
198
199
        $middleware1 = $this->createMock(MiddlewareInterface::class);
200
        $middleware2 = $this->createMock(MiddlewareInterface::class);
201
202
        $route = $route->to($middleware1)->prepend($middleware2);
203
204
        $middleware2
205
            ->expects($this->at(0))
206
            ->method('process')
207
            ->with($request, $route);
208
209
        // TODO: test that first one is called as well
210
211
        $route->process($request, $this->getRequestHandler());
212
    }
213
214
    private function getRequestHandler(): RequestHandlerInterface
215
    {
216
        return new class implements RequestHandlerInterface {
217
            public function handle(ServerRequestInterface $request): ResponseInterface
218
            {
219
                return new Response(404);
220
            }
221
        };
222
    }
223
}
224