1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GacelaTest\Unit\Router; |
6
|
|
|
|
7
|
|
|
use Gacela\Router\Routing; |
8
|
|
|
use Gacela\Router\RoutingConfigurator; |
9
|
|
|
use GacelaTest\Unit\Router\Fixtures\HeadersTearDown; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
include_once __DIR__ . '/Fake/header.php'; |
13
|
|
|
|
14
|
|
|
class RoutingRedirectTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
use HeadersTearDown; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @runInSeparateProcess |
20
|
|
|
*/ |
21
|
|
|
protected function setUp(): void |
22
|
|
|
{ |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @dataProvider provideSimpleRedirect |
27
|
|
|
*/ |
28
|
|
|
public function test_default_redirect(string $destination): void |
29
|
|
|
{ |
30
|
|
|
global $testHeaders; |
31
|
|
|
|
32
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri'; |
33
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
34
|
|
|
|
35
|
|
|
Routing::configure(static function (RoutingConfigurator $routes) use ($destination): void { |
36
|
|
|
$routes->redirect('optional/uri', $destination); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
self::assertSame([ |
40
|
|
|
[ |
41
|
|
|
'header' => 'Location: ' . $destination, |
42
|
|
|
'replace' => true, |
43
|
|
|
'response_code' => 302, |
44
|
|
|
], |
45
|
|
|
], $testHeaders); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider provideSimpleRedirect |
50
|
|
|
*/ |
51
|
|
|
public function test_simple_redirect(string $destination, int $statusCode): void |
52
|
|
|
{ |
53
|
|
|
global $testHeaders; |
54
|
|
|
|
55
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/optional/uri'; |
56
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
57
|
|
|
|
58
|
|
|
Routing::configure(static function (RoutingConfigurator $routes) use ($destination, $statusCode): void { |
59
|
|
|
$routes->redirect('optional/uri', $destination, $statusCode); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
self::assertSame([ |
63
|
|
|
[ |
64
|
|
|
'header' => 'Location: ' . $destination, |
65
|
|
|
'replace' => true, |
66
|
|
|
'response_code' => $statusCode, |
67
|
|
|
], |
68
|
|
|
], $testHeaders); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function provideSimpleRedirect(): iterable |
72
|
|
|
{ |
73
|
|
|
yield ['https://gacela-project.com/', 301]; |
74
|
|
|
yield ['https://chemaclass.com/', 308]; |
75
|
|
|
yield ['https://katarn.es/', 302]; |
76
|
|
|
yield ['https://jesusvalera.github.io/', 303]; |
77
|
|
|
yield ['https://github.com/ImanolRP', 307]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|