Passed
Push — master ( dbfb5e...e760f7 )
by Alexander
11:28
created

RouteTest.php$1 ➔ getRequestHandler()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A RouteTest.php$1 ➔ handle() 0 3 1
1
<?php
2
3
4
namespace Yiisoft\Router\Tests;
5
6
7
use Nyholm\Psr7\Request;
8
use Nyholm\Psr7\Response;
9
use Nyholm\Psr7\ServerRequest;
10
use Nyholm\Psr7\Uri;
11
use PHPUnit\Framework\TestCase;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Server\MiddlewareInterface;
15
use Psr\Http\Server\RequestHandlerInterface;
16
use Yiisoft\Router\Method;
17
use Yiisoft\Router\Route;
18
19
class RouteTest extends TestCase
20
{
21
    public function testName(): void
22
    {
23
        $route = Route::get('/')->name('test.route');
24
25
        $this->assertSame('test.route', $route->getName());
26
    }
27
28
    public function testNameDefault(): void
29
    {
30
        $route = Route::get('/');
31
32
        $this->assertSame('GET /', $route->getName());
33
    }
34
35
    public function testMethods(): void
36
    {
37
        $route = Route::methods([Method::POST, Method::HEAD], '/');
38
39
        $this->assertSame([Method::POST, Method::HEAD], $route->getMethods());
40
    }
41
42
    public const PATCH = 'PATCH';
43
    public const HEAD = 'HEAD';
44
    public const OPTIONS = 'OPTIONS';
45
46
    public function testGetMethod(): void
47
    {
48
        $route = Route::get('/');
49
50
        $this->assertSame([Method::GET], $route->getMethods());
51
    }
52
53
    public function testPostMethod(): void
54
    {
55
        $route = Route::post('/');
56
57
        $this->assertSame([Method::POST], $route->getMethods());
58
    }
59
60
    public function testPutMethod(): void
61
    {
62
        $route = Route::put('/');
63
64
        $this->assertSame([Method::PUT], $route->getMethods());
65
    }
66
67
    public function testDeleteMethod(): void
68
    {
69
        $route = Route::delete('/');
70
71
        $this->assertSame([Method::DELETE], $route->getMethods());
72
    }
73
74
    public function testPatchMethod(): void
75
    {
76
        $route = Route::patch('/');
77
78
        $this->assertSame([Method::PATCH], $route->getMethods());
79
    }
80
81
    public function testHeadMethod(): void
82
    {
83
        $route = Route::head('/');
84
85
        $this->assertSame([Method::HEAD], $route->getMethods());
86
    }
87
88
    public function testOptionsMethod(): void
89
    {
90
        $route = Route::options('/');
91
92
        $this->assertSame([Method::OPTIONS], $route->getMethods());
93
    }
94
95
    public function testAnyMethod(): void
96
    {
97
        $route = Route::anyMethod('/');
98
99
        $this->assertSame(Method::ANY, $route->getMethods());
100
    }
101
102
    public function testPattern(): void
103
    {
104
        $route = Route::get('/test');
105
106
        $this->assertSame('/test', $route->getPattern());
107
    }
108
109
    public function testHost(): void
110
    {
111
        $route = Route::get('/')->host('https://yiiframework.com/');
112
113
        $this->assertSame('https://yiiframework.com', $route->getHost());
114
    }
115
116
    public function testParameters(): void
117
    {
118
        $route = Route::get('/{language}')
119
            ->parameters(['language' => 'en']);
120
121
        $this->assertSame(['language' => 'en'], $route->getParameters());
122
    }
123
124
    public function testDefaults(): void
125
    {
126
        $route = Route::get('/{language}')
127
                      ->defaults(['language' => 'en']);
128
129
        $this->assertSame(['language' => 'en'], $route->getDefaults());
130
    }
131
132
    public function testToString(): void
133
    {
134
        $route = Route::methods([Method::GET, Method::POST], '/')
135
            ->name('test.route')
136
            ->host('yiiframework.com');
137
138
        $this->assertSame('[test.route] GET,POST yiiframework.com/', $route->__toString());
139
    }
140
141
    public function testInvalidTo(): void
142
    {
143
        $this->expectException(\InvalidArgumentException::class);
144
        Route::get('/')->to(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::to(). ( Ignorable by Annotation )

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

144
        Route::get('/')->to(/** @scrutinizer ignore-type */ new \StdClass());
Loading history...
145
    }
146
147
    public function testToMiddleware(): void
148
    {
149
        $request = new ServerRequest('GET', '/');
150
151
        $route = Route::get('/')->to(new class implements MiddlewareInterface {
152
            public function process(
153
              ServerRequestInterface $request,
154
              RequestHandlerInterface $handler
155
            ): ResponseInterface
156
            {
157
                return (new Response())->withStatus(418);
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
          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
    private function getRequestHandler(): RequestHandlerInterface
180
    {
181
        return new class implements RequestHandlerInterface {
182
            public function handle(ServerRequestInterface $request): ResponseInterface
183
            {
184
                return new Response(404);
185
            }
186
        };
187
    }
188
}