Passed
Push — master ( a586e8...77a217 )
by Alexander
01:33
created

RouterTest.php$1 ➔ createRouteMiddleware()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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