RouterRedirectTest::destinationProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Feature\Router;
6
7
use Gacela\Router\Configure\Routes;
8
use Gacela\Router\Entities\Request;
9
use Gacela\Router\Router;
10
use GacelaTest\Feature\HeaderTestCase;
11
12
final class RouterRedirectTest extends HeaderTestCase
13
{
14
    /**
15
     * @dataProvider destinationProvider
16
     */
17
    public function test_simple_redirect(string $destination): void
18
    {
19
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
20
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
21
22
        $router = new Router(static function (Routes $routes) use ($destination): void {
23
            $routes->redirect('optional/uri', $destination);
24
        });
25
        $router->run();
26
27
        self::assertSame([
28
            [
29
                'header' => 'Location: ' . $destination,
30
                'replace' => true,
31
                'response_code' => 302,
32
            ],
33
        ], $this->headers());
34
    }
35
36
    /**
37
     * @dataProvider destinationProvider
38
     */
39
    public function test_redirect_with_status_code(string $destination, int $statusCode): void
40
    {
41
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
42
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
43
44
        $router = new Router(static function (Routes $routes) use ($destination, $statusCode): void {
45
            $routes->redirect('optional/uri', $destination, $statusCode);
46
        });
47
        $router->run();
48
49
        self::assertSame([
50
            [
51
                'header' => 'Location: ' . $destination,
52
                'replace' => true,
53
                'response_code' => $statusCode,
54
            ],
55
        ], $this->headers());
56
    }
57
58
    /**
59
     * @dataProvider destinationProvider
60
     */
61
    public function test_redirect_with_custom_method(string $destination, int $statusCode, string $method): void
62
    {
63
        $_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri';
64
        $_SERVER['REQUEST_METHOD'] = $method;
65
66
        $router = new Router(
67
            static function (Routes $routes) use ($destination, $statusCode, $method): void {
68
                $routes->redirect('optional/uri', $destination, $statusCode, $method);
69
            },
70
        );
71
        $router->run();
72
73
        self::assertSame([
74
            [
75
                'header' => 'Location: ' . $destination,
76
                'replace' => true,
77
                'response_code' => $statusCode,
78
            ],
79
        ], $this->headers());
80
    }
81
82
    public function destinationProvider(): iterable
83
    {
84
        yield ['https://gacela-project.com/', 301, Request::METHOD_GET];
85
        yield ['https://chemaclass.com/', 308, Request::METHOD_POST];
86
        yield ['https://katarn.es/', 302, Request::METHOD_PUT];
87
        yield ['https://jesusvalera.github.io/', 303, Request::METHOD_DELETE];
88
        yield ['https://github.com/ImanolRP', 307, Request::METHOD_PATCH];
89
    }
90
}
91