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\Entities\Response; |
9
|
|
|
use Gacela\Router\Router; |
10
|
|
|
use Gacela\Router\Routes; |
11
|
|
|
use GacelaTest\Feature\HeaderTestCase; |
12
|
|
|
|
13
|
|
|
final class RouterRunTest extends HeaderTestCase |
14
|
|
|
{ |
15
|
|
|
public function test_run_multiple_route_collections(): void |
16
|
|
|
{ |
17
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/uri-2'; |
18
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
19
|
|
|
|
20
|
|
|
$router = Router::create(); |
21
|
|
|
|
22
|
|
|
$router->addRoutes(static function (Routes $routes): void { |
23
|
|
|
$routes->get('uri-1', static fn () => new Response('first body')); |
24
|
|
|
}); |
25
|
|
|
|
26
|
|
|
$router->addRoutes(static function (Routes $routes): void { |
27
|
|
|
$routes->get('uri-2', static fn () => new Response('second body')); |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
$router->run(); |
31
|
|
|
|
32
|
|
|
$this->expectOutputString('second body'); |
33
|
|
|
|
34
|
|
|
self::assertSame([], $this->headers()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function test_run_same_multiple_route_collections_then_first_with_priority(): void |
38
|
|
|
{ |
39
|
|
|
$_SERVER['REQUEST_URI'] = 'https://example.org/uri'; |
40
|
|
|
$_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
41
|
|
|
|
42
|
|
|
$router = Router::create(); |
43
|
|
|
|
44
|
|
|
$router->addRoutes(static function (Routes $routes): void { |
45
|
|
|
$routes->get('uri', static fn () => new Response('first body')); |
46
|
|
|
}); |
47
|
|
|
|
48
|
|
|
$router->addRoutes(static function (Routes $routes): void { |
49
|
|
|
$routes->get('uri', static fn () => new Response('second body')); |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
$router->run(); |
53
|
|
|
|
54
|
|
|
$this->expectOutputString('first body'); |
55
|
|
|
|
56
|
|
|
self::assertSame([], $this->headers()); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|