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

RouterTest.php$0 ➔ __construct()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
1
<?php
2
namespace Yiisoft\Router\Tests\Middleware;
3
4
use Nyholm\Psr7\Response;
5
use Nyholm\Psr7\ServerRequest;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\MiddlewareInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Yiisoft\Router\MatchingResult;
12
use Yiisoft\Router\Method;
13
use Yiisoft\Router\Middleware\Router;
14
use Yiisoft\Router\Route;
15
use Yiisoft\Router\UrlMatcherInterface;
16
17
class RouterTest extends TestCase
18
{
19
    public function testProcessSuccess(): void
20
    {
21
        $request = new ServerRequest('GET', '/');
22
23
        $middleware = new Router($this->getMatcher());
24
        $response = $middleware->process($request, $this->getRequestHandler());
25
        $this->assertSame(418, $response->getStatusCode());
26
    }
27
28
    public function testProcessFailure(): void
29
    {
30
        $request = new ServerRequest('POST', '/');
31
32
        $middleware = new Router($this->getMatcher());
33
        $response = $middleware->process($request, $this->getRequestHandler());
34
        $this->assertSame(404, $response->getStatusCode());
35
    }
36
37
    private function getMatcher(): UrlMatcherInterface
38
    {
39
        $middleware = $this->getRouteMiddleware();
40
41
        return new class ($middleware) implements UrlMatcherInterface
42
        {
43
            private $middleware;
44
45
            public function __construct(MiddlewareInterface $middleware)
46
            {
47
                $this->middleware = $middleware;
48
            }
49
50
            public function match(ServerRequestInterface $request): MatchingResult
51
            {
52
                if ($request->getMethod() === 'GET') {
53
                    $route = Route::get('/')->to($this->middleware);
54
                    return MatchingResult::fromSuccess($route, ['parameter' => 'value']);
55
                }
56
57
                return MatchingResult::fromFailure([Method::GET, Method::HEAD]);
58
            }
59
        };
60
    }
61
62
    private function getRequestHandler(): RequestHandlerInterface
63
    {
64
        return new class implements RequestHandlerInterface {
65
            public function handle(ServerRequestInterface $request): ResponseInterface
66
            {
67
                return new Response(404);
68
            }
69
        };
70
    }
71
72
    private function getRouteMiddleware(): MiddlewareInterface
73
    {
74
        return new class implements MiddlewareInterface {
75
            public function process(
76
              ServerRequestInterface $request,
77
              RequestHandlerInterface $handler
78
            ): ResponseInterface
79
            {
80
                return (new Response())->withStatus(418);
81
            }
82
        };
83
    }
84
}