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 destinationProvider |
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'] = 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 destinationProvider |
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'] = 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 destinationProvider |
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 destinationProvider |
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'] = 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 destinationProvider(): iterable |
118
|
|
|
{ |
119
|
|
|
yield ['https://gacela-project.com/', 301, Request::METHOD_GET]; |
120
|
|
|
yield ['https://chemaclass.com/', 308, Request::METHOD_POST]; |
121
|
|
|
yield ['https://katarn.es/', 302, Request::METHOD_PUT]; |
122
|
|
|
yield ['https://jesusvalera.github.io/', 303, Request::METHOD_DELETE]; |
123
|
|
|
yield ['https://github.com/ImanolRP', 307, Request::METHOD_PATCH]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|