Passed
Push — master ( 5e617a...4a8966 )
by Alexander
02:51 queued 01:26
created

RouterTest.php$1 ➔ handle()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
<?php
2
3
namespace Yiisoft\Router\Tests\Middleware;
4
5
use Nyholm\Psr7\Response;
6
use Nyholm\Psr7\Factory\Psr17Factory;
7
use Nyholm\Psr7\ServerRequest;
8
use PHPUnit\Framework\TestCase;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\MiddlewareInterface;
12
use Psr\Http\Server\RequestHandlerInterface;
13
use Yiisoft\Router\MatchingResult;
14
use Yiisoft\Http\Method;
15
use Yiisoft\Router\Middleware\Router;
16
use Yiisoft\Router\Route;
17
use Yiisoft\Router\Group;
18
use Yiisoft\Router\RouteCollection;
19
use Yiisoft\Router\RouteCollectionInterface;
20
use Yiisoft\Router\UrlMatcherInterface;
21
22
final class RouterTest extends TestCase
23
{
24
    private function createRouterMiddleware(): Router
25
    {
26
        return new Router($this->getMatcher(), new Psr17Factory());
27
    }
28
29
    private function processWithRouter(ServerRequestInterface $request): ResponseInterface
30
    {
31
        return $this->createRouterMiddleware()->process($request, $this->createRequestHandler());
32
    }
33
34
    public function testProcessSuccess(): void
35
    {
36
        $request = new ServerRequest('GET', '/');
37
        $response = $this->processWithRouter($request);
38
        $this->assertSame(201, $response->getStatusCode());
39
    }
40
41
    public function testMissingRouteRespondWith404(): void
42
    {
43
        $request = new ServerRequest('GET', '/no-such-route');
44
        $response = $this->processWithRouter($request);
45
        $this->assertSame(404, $response->getStatusCode());
46
    }
47
48
    public function testMethodMismatchRespondWith405(): void
49
    {
50
        $request = new ServerRequest('POST', '/');
51
        $response = $this->processWithRouter($request);
52
        $this->assertSame(405, $response->getStatusCode());
53
        $this->assertSame('GET, HEAD', $response->getHeaderLine('Allow'));
54
    }
55
56
    private function getMatcher(): UrlMatcherInterface
57
    {
58
        $middleware = $this->createRouteMiddleware();
59
60
        return new class($middleware) implements UrlMatcherInterface {
61
            private $middleware;
62
63
            public function __construct(MiddlewareInterface $middleware)
64
            {
65
                $this->middleware = $middleware;
66
            }
67
68
            public function getCurrentRoute(): ?Route
69
            {
70
            }
71
72
            public function getLastMatchedRequest(): ?ServerRequestInterface
73
            {
74
            }
75
76
            public function getRouteCollection(): RouteCollectionInterface
77
            {
78
                $collector = new Group();
79
                return new RouteCollection($collector);
80
            }
81
82
            /**
83
             * Emulates router with a single `GET /` route
84
             * @param ServerRequestInterface $request
85
             * @return MatchingResult
86
             */
87
            public function match(ServerRequestInterface $request): MatchingResult
88
            {
89
                if ($request->getUri()->getPath() !== '/') {
90
                    return MatchingResult::fromFailure(Method::ANY);
91
                }
92
93
                if ($request->getMethod() === 'GET') {
94
                    $route = Route::get('/')->addMiddleware($this->middleware);
95
                    return MatchingResult::fromSuccess($route, ['parameter' => 'value']);
96
                }
97
98
                return MatchingResult::fromFailure([Method::GET, Method::HEAD]);
99
            }
100
        };
101
    }
102
103
    private function createRequestHandler(): RequestHandlerInterface
104
    {
105
        return new class() implements RequestHandlerInterface {
106
            public function handle(ServerRequestInterface $request): ResponseInterface
107
            {
108
                return new Response(404);
109
            }
110
        };
111
    }
112
113
    private function createRouteMiddleware(): MiddlewareInterface
114
    {
115
        return new class() implements MiddlewareInterface {
116
            public function process(
117
                ServerRequestInterface $request,
118
                RequestHandlerInterface $handler
119
            ): ResponseInterface {
120
                return (new Response())->withStatus(201);
121
            }
122
        };
123
    }
124
}
125