Passed
Pull Request — main (#8)
by Jesús
02:11
created

RouterRedirectTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Unit\Router;
6
7
use Gacela\Router\Entities\Request;
8
use Gacela\Router\Router;
9
use Gacela\Router\Routes;
10
use GacelaTest\Unit\Router\Fixtures\HeadersTearDown;
11
use PHPUnit\Framework\TestCase;
12
13
include_once __DIR__ . '/Fake/header.php';
14
15
final class RouterRedirectTest extends TestCase
16
{
17
    use HeadersTearDown;
18
19
    /**
20
     * @runInSeparateProcess
21
     */
22
    protected function setUp(): void
23
    {
24
    }
25
26
    /**
27
     * @dataProvider destinationProvider
28
     */
29
    public function test_simple_redirect(string $destination): void
30
    {
31
        global $testHeaders;
32
33
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
34
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
35
36
        Router::configure(static function (Routes $routes) use ($destination): void {
37
            $routes->redirect('optional/uri', $destination);
38
        });
39
40
        self::assertSame([
41
            [
42
                'header' => 'Location: ' . $destination,
43
                'replace' => true,
44
                'response_code' => 302,
45
            ],
46
        ], $testHeaders);
47
    }
48
49
    /**
50
     * @dataProvider destinationProvider
51
     */
52
    public function test_redirect_with_status_code(string $destination, int $statusCode): void
53
    {
54
        global $testHeaders;
55
56
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
57
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
58
59
        Router::configure(static function (Routes $routes) use ($destination, $statusCode): void {
60
            $routes->redirect('optional/uri', $destination, $statusCode);
61
        });
62
63
        self::assertSame([
64
            [
65
                'header' => 'Location: ' . $destination,
66
                'replace' => true,
67
                'response_code' => $statusCode,
68
            ],
69
        ], $testHeaders);
70
    }
71
72
    /**
73
     * @dataProvider destinationProvider
74
     */
75
    public function test_redirect_with_custom_method(string $destination, int $statusCode, string $method): void
76
    {
77
        global $testHeaders;
78
79
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
80
        $_SERVER['REQUEST_METHOD'] = $method;
81
82
        Router::configure(
83
            static function (Routes $routes) use ($destination, $statusCode, $method): void {
84
                $routes->redirect('optional/uri', $destination, $statusCode, $method);
85
            },
86
        );
87
88
        self::assertSame([
89
            [
90
                'header' => 'Location: ' . $destination,
91
                'replace' => true,
92
                'response_code' => $statusCode,
93
            ],
94
        ], $testHeaders);
95
    }
96
97
    /**
98
     * @dataProvider destinationProvider
99
     */
100
    public function test_not_redirect_non_registered_method(string $destination, int $statusCode, string $method): void
101
    {
102
        global $testHeaders;
103
104
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
105
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_OPTIONS;
106
107
        Router::configure(
108
            static function (Routes $routes) use ($destination, $statusCode, $method): void {
109
                $routes->redirect('optional/uri', $destination, $statusCode, $method);
110
            },
111
        );
112
113
        self::assertNull($testHeaders);
114
    }
115
116
    public function destinationProvider(): iterable
117
    {
118
        yield ['https://gacela-project.com/', 301, Request::METHOD_GET];
119
        yield ['https://chemaclass.com/', 308, Request::METHOD_POST];
120
        yield ['https://katarn.es/', 302, Request::METHOD_PUT];
121
        yield ['https://jesusvalera.github.io/', 303, Request::METHOD_DELETE];
122
        yield ['https://github.com/ImanolRP', 307, Request::METHOD_PATCH];
123
    }
124
}
125