Passed
Push — master ( 3c5346...dd6497 )
by Alexander
17:04 queued 14:30
created

RedirectTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 117
rs 10
c 3
b 0
f 0
eloc 47
wmc 6
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests\Middleware;
4
5
use Nyholm\Psr7\Factory\Psr17Factory;
6
use Nyholm\Psr7\Response;
7
use Nyholm\Psr7\ServerRequest;
8
use PHPUnit\Framework\TestCase;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Yiisoft\Http\Method;
12
use Yiisoft\Router\UrlGeneratorInterface;
13
use Yiisoft\Yii\Web\Middleware\Redirect;
14
15
final class RedirectTest extends TestCase
16
{
17
    public function testInvalidArguments(): void
18
    {
19
        $this->expectException(\InvalidArgumentException::class);
20
        $this->createRedirectMiddleware()->process($this->createRequest(), $this->createRequestHandler());
21
    }
22
23
    public function testGenerateUri(): void
24
    {
25
        $middleware = $this->createRedirectMiddleware()
26
            ->toRoute('test/route', [
27
                'param1' => 1,
28
                'param2' => 2,
29
            ]);
30
31
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
32
        $header = $response->getHeader('Location');
33
34
        $this->assertSame($header[0], 'test/route?param1=1&param2=2');
35
    }
36
37
    public function testTemporaryReturnCode303(): void
38
    {
39
        $middleware = $this->createRedirectMiddleware()
40
            ->toRoute('test/route')
41
            ->temporary();
42
43
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
44
45
        $this->assertSame($response->getStatusCode(), 303);
46
    }
47
48
    public function testPermanentReturnCode301(): void
49
    {
50
        $middleware = $this->createRedirectMiddleware()
51
            ->toRoute('test/route')
52
            ->permanent();
53
54
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
55
56
        $this->assertSame($response->getStatusCode(), 301);
57
    }
58
59
    public function testStatusReturnCode400(): void
60
    {
61
        $middleware = $this->createRedirectMiddleware()
62
            ->toRoute('test/route')
63
            ->status(400);
64
65
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
66
67
        $this->assertSame($response->getStatusCode(), 400);
68
    }
69
70
    public function testSetUri(): void
71
    {
72
        $middleware = $this->createRedirectMiddleware()
73
            ->toUrl('test/custom/route');
74
75
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
76
        $header   = $response->getHeader('Location');
77
78
        $this->assertSame($header[0], 'test/custom/route');
79
    }
80
81
    private function createRequestHandler(): RequestHandlerInterface
82
    {
83
        $requestHandler = $this->createMock(RequestHandlerInterface::class);
84
        $requestHandler
85
            ->method('handle')
86
            ->willReturn(new Response(200));
87
88
        return $requestHandler;
89
    }
90
91
    private function createRequest(string $method = Method::GET, string $uri = '/'): ServerRequestInterface
92
    {
93
        return new ServerRequest($method, $uri);
94
    }
95
96
    private function createUrlGenerator(): UrlGeneratorInterface
97
    {
98
        return new class() implements UrlGeneratorInterface {
99
            private $prefix = '';
100
            public function generate(string $name, array $parameters = []): string
101
            {
102
                return $name . '?' . http_build_query($parameters);
103
            }
104
            public function getUriPrefix(): string
105
            {
106
                return $this->prefix;
107
            }
108
            public function setUriPrefix(string $prefix): void
109
            {
110
                $this->prefix = $prefix;
111
            }
112
113
            public function generateAbsolute(
114
                string $name,
115
                array $parameters = [],
116
                string $scheme = null,
117
                string $host = null
118
            ): string {
119
                return $name . '?' . http_build_query($parameters);
120
            }
121
        };
122
    }
123
124
    private function createRedirectMiddleware(): Redirect
125
    {
126
        $urlGenerator = $this->createMock(UrlGeneratorInterface::class);
127
        $urlGenerator
128
            ->method('generate')
129
            ->willReturnCallback(fn ($name, $params) => $name . '?' . http_build_query($params));
130
131
        return new Redirect(new Psr17Factory(), $urlGenerator);
132
    }
133
}
134