Passed
Push — master ( 7a7650...26e364 )
by Alexander
11:46
created

RedirectTest::permanentReturnCode301()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 9
rs 10
eloc 5
nc 1
nop 0
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\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\Router\Method;
13
use Yiisoft\Router\UrlGeneratorInterface;
14
use Yiisoft\Yii\Web\Middleware\Redirect;
15
16
final class RedirectTest extends TestCase
17
{
18
    /**
19
     * @test
20
     */
21
    public function invalidArguments()
22
    {
23
        $this->expectException(\InvalidArgumentException::class);
24
        $this->createRedirectMiddleware()->process($this->createRequest(), $this->createRequestHandler());
25
    }
26
27
    /**
28
     * @test
29
     */
30
    public function generateUri()
31
    {
32
        $middleware = $this->createRedirectMiddleware()
33
            ->toRoute('test/route', [
34
                'param1' => 1,
35
                'param2' => 2,
36
            ]);
37
38
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
39
        $header = $response->getHeader('Location');
40
41
        $this->assertSame($header[0], 'test/route?param1=1&param2=2');
42
    }
43
44
    /**
45
     * @test
46
     */
47
    public function temporaryReturnCode302()
48
    {
49
        $middleware = $this->createRedirectMiddleware()
50
            ->toRoute('test/route')
51
            ->temporary();
52
53
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
54
55
        $this->assertSame($response->getStatusCode(), 302);
56
    }
57
58
    /**
59
     * @test
60
     */
61
    public function permanentReturnCode301()
62
    {
63
        $middleware = $this->createRedirectMiddleware()
64
            ->toRoute('test/route')
65
            ->permanent();
66
67
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
68
69
        $this->assertSame($response->getStatusCode(), 301);
70
    }
71
72
    /**
73
     * @test
74
     */
75
    public function statusReturnCode400()
76
    {
77
        $middleware = $this->createRedirectMiddleware()
78
            ->toRoute('test/route')
79
            ->status(400);
80
81
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
82
83
        $this->assertSame($response->getStatusCode(), 400);
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function setUri()
90
    {
91
        $middleware = $this->createRedirectMiddleware()
92
            ->toUrl('test/custom/route');
93
94
        $response = $middleware->process($this->createRequest(), $this->createRequestHandler());
95
        $header   = $response->getHeader('Location');
96
97
        $this->assertSame($header[0], 'test/custom/route');
98
    }
99
100
    private function createRequestHandler(): RequestHandlerInterface
101
    {
102
        return new class implements RequestHandlerInterface {
103
            public function handle(ServerRequestInterface $request): ResponseInterface
104
            {
105
                return new Response(200);
106
            }
107
        };
108
    }
109
110
    private function createRequest(string $method = Method::GET, string $uri = '/'): ServerRequestInterface
111
    {
112
        return new ServerRequest($method, $uri);
113
    }
114
115
    private function createUrlGenerator(): UrlGeneratorInterface
116
    {
117
        return new class implements UrlGeneratorInterface {
118
            public function generate(string $name, array $parameters = []): string
119
            {
120
                return $name . '?' . http_build_query($parameters);
121
            }
122
        };
123
    }
124
125
    private function createRedirectMiddleware(): Redirect
126
    {
127
        return new Redirect(new Psr17Factory(), $this->createUrlGenerator());
128
    }
129
}
130