Passed
Push — master ( 138cac...b16689 )
by Alexander
02:23 queued 39s
created

RouteTest::testInvalidMiddlewareMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
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\Http\Method;
13
use Yiisoft\Router\Middleware\Callback;
14
use Yiisoft\Router\Route;
15
16
final class RouteTest extends TestCase
17
{
18
    public function testName(): void
19
    {
20
        $route = Route::get('/')->name('test.route');
21
22
        $this->assertSame('test.route', $route->getName());
23
    }
24
25
    public function testNameDefault(): void
26
    {
27
        $route = Route::get('/');
28
29
        $this->assertSame('GET /', $route->getName());
30
    }
31
32
    public function testMethods(): void
33
    {
34
        $route = Route::methods([Method::POST, Method::HEAD], '/');
35
36
        $this->assertSame([Method::POST, Method::HEAD], $route->getMethods());
37
    }
38
39
    public const PATCH = 'PATCH';
40
    public const HEAD = 'HEAD';
41
    public const OPTIONS = 'OPTIONS';
42
43
    public function testGetMethod(): void
44
    {
45
        $route = Route::get('/');
46
47
        $this->assertSame([Method::GET], $route->getMethods());
48
    }
49
50
    public function testPostMethod(): void
51
    {
52
        $route = Route::post('/');
53
54
        $this->assertSame([Method::POST], $route->getMethods());
55
    }
56
57
    public function testPutMethod(): void
58
    {
59
        $route = Route::put('/');
60
61
        $this->assertSame([Method::PUT], $route->getMethods());
62
    }
63
64
    public function testDeleteMethod(): void
65
    {
66
        $route = Route::delete('/');
67
68
        $this->assertSame([Method::DELETE], $route->getMethods());
69
    }
70
71
    public function testPatchMethod(): void
72
    {
73
        $route = Route::patch('/');
74
75
        $this->assertSame([Method::PATCH], $route->getMethods());
76
    }
77
78
    public function testHeadMethod(): void
79
    {
80
        $route = Route::head('/');
81
82
        $this->assertSame([Method::HEAD], $route->getMethods());
83
    }
84
85
    public function testOptionsMethod(): void
86
    {
87
        $route = Route::options('/');
88
89
        $this->assertSame([Method::OPTIONS], $route->getMethods());
90
    }
91
92
    public function testAnyMethod(): void
93
    {
94
        $route = Route::anyMethod('/');
95
96
        $this->assertSame(Method::ANY, $route->getMethods());
97
    }
98
99
    public function testPattern(): void
100
    {
101
        $route = Route::get('/test')->pattern('/test2');
102
103
        $this->assertSame('/test2', $route->getPattern());
104
    }
105
106
    public function testHost(): void
107
    {
108
        $route = Route::get('/')->host('https://yiiframework.com/');
109
110
        $this->assertSame('https://yiiframework.com', $route->getHost());
111
    }
112
113
    public function testDefaults(): void
114
    {
115
        $route = Route::get('/{language}')->defaults(['language' => 'en']);
116
117
        $this->assertSame(['language' => 'en'], $route->getDefaults());
118
    }
119
120
    public function testToString(): void
121
    {
122
        $route = Route::methods([Method::GET, Method::POST], '/')->name('test.route')->host('yiiframework.com');
123
124
        $this->assertSame('[test.route] GET,POST yiiframework.com/', (string)$route);
125
    }
126
127
    public function testToStringSimple(): void
128
    {
129
        $route = Route::get('/');
130
131
        $this->assertSame('GET /', (string)$route);
132
    }
133
134
    public function testInvalidMiddlewareMethod(): void
135
    {
136
        $this->expectException(\InvalidArgumentException::class);
137
        Route::get('/', new \stdClass());
0 ignored issues
show
Bug introduced by
new stdClass() of type stdClass is incompatible with the type Psr\Http\Server\MiddlewareInterface|callable|null expected by parameter $middleware of Yiisoft\Router\Route::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
        Route::get('/', /** @scrutinizer ignore-type */ new \stdClass());
Loading history...
138
    }
139
140
    public function testInvalidMiddlewareAdd(): void
141
    {
142
        $this->expectException(\InvalidArgumentException::class);
143
        Route::get('/')->addMiddleware(new \stdClass());
0 ignored issues
show
Bug introduced by
new stdClass() of type stdClass is incompatible with the type Psr\Http\Server\MiddlewareInterface|callable expected by parameter $middleware of Yiisoft\Router\Route::addMiddleware(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

143
        Route::get('/')->addMiddleware(/** @scrutinizer ignore-type */ new \stdClass());
Loading history...
144
    }
145
146
    public function testAddMiddleware(): void
147
    {
148
        $request = new ServerRequest('GET', '/');
149
150
        $route = Route::get('/')->addMiddleware(
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 testAddCallableMiddleware(): void
166
    {
167
        $request = new ServerRequest('GET', '/');
168
169
        $route = Route::get('/')->addMiddleware(
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 testMiddlewareFullStackCalled(): void
180
    {
181
        $request = new ServerRequest('GET', '/');
182
183
        $routeOne = Route::get('/');
184
185
        $middleware1 = new Callback(function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
186
            return $handler->handle($request);
187
        });
188
        $middleware2 = new Callback(function () {
189
            return new Response(200);
190
        });
191
192
        $routeOne = $routeOne->addMiddleware($middleware2)->addMiddleware($middleware1);
193
194
        $response = $routeOne->process($request, $this->getRequestHandler());
195
        $this->assertSame(200, $response->getStatusCode());
196
    }
197
198
    public function testMiddlewareStackInterrupted(): void
199
    {
200
        $request = new ServerRequest('GET', '/');
201
202
        $routeTwo = Route::get('/');
203
204
        $middleware1 = new Callback(function () {
205
            return new Response(404);
206
        });
207
        $middleware2 = new Callback(function () {
208
            return new Response(200);
209
        });
210
211
        $routeTwo = $routeTwo->addMiddleware($middleware2)->addMiddleware($middleware1);
212
213
        $response = $routeTwo->process($request, $this->getRequestHandler());
214
        $this->assertSame(404, $response->getStatusCode());
215
    }
216
217
    private function getRequestHandler(): RequestHandlerInterface
218
    {
219
        return new class() implements RequestHandlerInterface {
220
            public function handle(ServerRequestInterface $request): ResponseInterface
221
            {
222
                return new Response(404);
223
            }
224
        };
225
    }
226
}
227