Passed
Pull Request — main (#6)
by Chema
02:26
created

RoutingRedirectTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 46
dl 0
loc 109
rs 10
c 6
b 0
f 0
wmc 6

6 Methods

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