1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GacelaTest\Feature\Router; |
6
|
|
|
|
7
|
|
|
use Gacela\Router\Entities\Request; |
8
|
|
|
use Gacela\Router\Router; |
9
|
|
|
use Gacela\Router\Routes; |
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::configure(static function (Routes $routes) use ($destination): void { |
23
|
|
|
$routes->redirect('optional/uri', $destination); |
24
|
|
|
}); |
25
|
|
|
|
26
|
|
|
self::assertSame([ |
27
|
|
|
[ |
28
|
|
|
'header' => 'Location: ' . $destination, |
29
|
|
|
'replace' => true, |
30
|
|
|
'response_code' => 302, |
31
|
|
|
], |
32
|
|
|
], $this->headers()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @dataProvider destinationProvider |
37
|
|
|
*/ |
38
|
|
|
public function test_redirect_with_status_code(string $destination, int $statusCode): void |
39
|
|
|
{ |
40
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri'; |
41
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
42
|
|
|
|
43
|
|
|
Router::configure(static function (Routes $routes) use ($destination, $statusCode): void { |
44
|
|
|
$routes->redirect('optional/uri', $destination, $statusCode); |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
self::assertSame([ |
48
|
|
|
[ |
49
|
|
|
'header' => 'Location: ' . $destination, |
50
|
|
|
'replace' => true, |
51
|
|
|
'response_code' => $statusCode, |
52
|
|
|
], |
53
|
|
|
], $this->headers()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @dataProvider destinationProvider |
58
|
|
|
*/ |
59
|
|
|
public function test_redirect_with_custom_method(string $destination, int $statusCode, string $method): void |
60
|
|
|
{ |
61
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri'; |
62
|
|
|
$_SERVER['REQUEST_METHOD'] = $method; |
63
|
|
|
|
64
|
|
|
Router::configure( |
65
|
|
|
static function (Routes $routes) use ($destination, $statusCode, $method): void { |
66
|
|
|
$routes->redirect('optional/uri', $destination, $statusCode, $method); |
67
|
|
|
}, |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
self::assertSame([ |
71
|
|
|
[ |
72
|
|
|
'header' => 'Location: ' . $destination, |
73
|
|
|
'replace' => true, |
74
|
|
|
'response_code' => $statusCode, |
75
|
|
|
], |
76
|
|
|
], $this->headers()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function destinationProvider(): iterable |
80
|
|
|
{ |
81
|
|
|
yield ['https://gacela-project.com/', 301, Request::METHOD_GET]; |
82
|
|
|
yield ['https://chemaclass.com/', 308, Request::METHOD_POST]; |
83
|
|
|
yield ['https://katarn.es/', 302, Request::METHOD_PUT]; |
84
|
|
|
yield ['https://jesusvalera.github.io/', 303, Request::METHOD_DELETE]; |
85
|
|
|
yield ['https://github.com/ImanolRP', 307, Request::METHOD_PATCH]; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|