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\JsonResponse; |
||||
9 | use Gacela\Router\Entities\Request; |
||||
10 | use Gacela\Router\Entities\Response; |
||||
11 | use Gacela\Router\Router; |
||||
12 | use GacelaTest\Feature\HeaderTestCase; |
||||
13 | |||||
14 | final class RouterResponseTest extends HeaderTestCase |
||||
15 | { |
||||
16 | public function test_string_response(): void |
||||
17 | { |
||||
18 | $_SERVER['REQUEST_URI'] = 'https://example.org/uri'; |
||||
19 | $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
||||
20 | |||||
21 | $router = new Router(static function (Routes $routes): void { |
||||
22 | $routes->get('uri', static fn () => new Response('body')); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
23 | }); |
||||
24 | $router->run(); |
||||
25 | |||||
26 | $this->expectOutputString('body'); |
||||
27 | |||||
28 | self::assertSame([], $this->headers()); |
||||
29 | } |
||||
30 | |||||
31 | public function test_json_response(): void |
||||
32 | { |
||||
33 | $_SERVER['REQUEST_URI'] = 'https://example.org/uri'; |
||||
34 | $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
||||
35 | |||||
36 | $router = new Router(static function (Routes $routes): void { |
||||
37 | $routes->get('uri', static fn () => new JsonResponse([ |
||||
0 ignored issues
–
show
function(...) { /* ... */ } of type callable is incompatible with the type object|string expected by parameter $controller of Gacela\Router\Configure\Routes::get() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
38 | 'key' => 'value', |
||||
39 | ])); |
||||
40 | }); |
||||
41 | $router->run(); |
||||
42 | |||||
43 | $this->expectOutputString('{"key":"value"}'); |
||||
44 | |||||
45 | self::assertSame([ |
||||
46 | [ |
||||
47 | 'header' => 'Content-Type: application/json', |
||||
48 | 'replace' => true, |
||||
49 | 'response_code' => 0, |
||||
50 | ], |
||||
51 | ], $this->headers()); |
||||
52 | } |
||||
53 | |||||
54 | public function test_response_headers(): void |
||||
55 | { |
||||
56 | $_SERVER['REQUEST_URI'] = 'https://example.org/uri'; |
||||
57 | $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
||||
58 | |||||
59 | $router = new Router(static function (Routes $routes): void { |
||||
60 | $routes->get('uri', static fn () => new Response('{"key":"value"}', [ |
||||
0 ignored issues
–
show
function(...) { /* ... */ } of type callable is incompatible with the type object|string expected by parameter $controller of Gacela\Router\Configure\Routes::get() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
61 | 'Access-Control-Allow-Origin: *', |
||||
62 | 'Content-Type: application/json', |
||||
63 | ])); |
||||
64 | }); |
||||
65 | $router->run(); |
||||
66 | |||||
67 | $this->expectOutputString('{"key":"value"}'); |
||||
68 | |||||
69 | self::assertSame([ |
||||
70 | [ |
||||
71 | 'header' => 'Access-Control-Allow-Origin: *', |
||||
72 | 'replace' => true, |
||||
73 | 'response_code' => 0, |
||||
74 | ], |
||||
75 | [ |
||||
76 | 'header' => 'Content-Type: application/json', |
||||
77 | 'replace' => true, |
||||
78 | 'response_code' => 0, |
||||
79 | ], |
||||
80 | ], $this->headers()); |
||||
81 | } |
||||
82 | |||||
83 | public function test_json_response_headers(): void |
||||
84 | { |
||||
85 | $_SERVER['REQUEST_URI'] = 'https://example.org/uri'; |
||||
86 | $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET; |
||||
87 | |||||
88 | $router = new Router(static function (Routes $routes): void { |
||||
89 | $routes->get('uri', static fn () => new JsonResponse(['key' => 'value'], [ |
||||
0 ignored issues
–
show
function(...) { /* ... */ } of type callable is incompatible with the type object|string expected by parameter $controller of Gacela\Router\Configure\Routes::get() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
90 | 'Access-Control-Allow-Origin: *', |
||||
91 | 'Content-Type: application/json', |
||||
92 | ])); |
||||
93 | }); |
||||
94 | $router->run(); |
||||
95 | |||||
96 | $this->expectOutputString('{"key":"value"}'); |
||||
97 | |||||
98 | self::assertSame([ |
||||
99 | [ |
||||
100 | 'header' => 'Access-Control-Allow-Origin: *', |
||||
101 | 'replace' => true, |
||||
102 | 'response_code' => 0, |
||||
103 | ], |
||||
104 | [ |
||||
105 | 'header' => 'Content-Type: application/json', |
||||
106 | 'replace' => true, |
||||
107 | 'response_code' => 0, |
||||
108 | ], |
||||
109 | ], $this->headers()); |
||||
110 | } |
||||
111 | } |
||||
112 |