Passed
Push — main ( 2aad05...56c0cb )
by Chema
53s queued 13s
created

RouterResponseTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 48
c 3
b 0
f 0
dl 0
loc 92
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A test_string_response() 0 12 1
A test_json_response() 0 20 1
A test_json_response_headers() 0 26 1
A test_response_headers() 0 26 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Feature\Router;
6
7
use Gacela\Router\Entities\JsonResponse;
8
use Gacela\Router\Entities\Request;
9
use Gacela\Router\Entities\Response;
10
use Gacela\Router\Router;
11
use Gacela\Router\Routes;
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::configure(static function (Routes $routes): void {
22
            $routes->get('uri', static fn () => new Response('body'));
1 ignored issue
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type object|string expected by parameter $controller of Gacela\Router\Routes::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
            $routes->get('uri', /** @scrutinizer ignore-type */ static fn () => new Response('body'));
Loading history...
23
        });
24
25
        $this->expectOutputString('body');
26
27
        self::assertSame([], $this->headers());
28
    }
29
30
    public function test_json_response(): void
31
    {
32
        $_SERVER['REQUEST_URI'] = 'https://example.org/uri';
33
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
34
35
        Router::configure(static function (Routes $routes): void {
36
            $routes->get('uri', static fn () => new JsonResponse([
1 ignored issue
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type object|string expected by parameter $controller of Gacela\Router\Routes::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

36
            $routes->get('uri', /** @scrutinizer ignore-type */ static fn () => new JsonResponse([
Loading history...
37
                'key' => 'value',
38
            ]));
39
        });
40
41
        $this->expectOutputString('{"key":"value"}');
42
43
        self::assertSame([
44
            [
45
                'header' => 'Content-Type: application/json',
46
                'replace' => true,
47
                'response_code' => 0,
48
            ],
49
        ], $this->headers());
50
    }
51
52
    public function test_response_headers(): void
53
    {
54
        $_SERVER['REQUEST_URI'] = 'https://example.org/uri';
55
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
56
57
        Router::configure(static function (Routes $routes): void {
58
            $routes->get('uri', static fn () => new Response('{"key":"value"}', [
1 ignored issue
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type object|string expected by parameter $controller of Gacela\Router\Routes::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
            $routes->get('uri', /** @scrutinizer ignore-type */ static fn () => new Response('{"key":"value"}', [
Loading history...
59
                'Access-Control-Allow-Origin: *',
60
                'Content-Type: application/json',
61
            ]));
62
        });
63
64
        $this->expectOutputString('{"key":"value"}');
65
66
        self::assertSame([
67
            [
68
                'header' => 'Access-Control-Allow-Origin: *',
69
                'replace' => true,
70
                'response_code' => 0,
71
            ],
72
            [
73
                'header' => 'Content-Type: application/json',
74
                'replace' => true,
75
                'response_code' => 0,
76
            ],
77
        ], $this->headers());
78
    }
79
80
    public function test_json_response_headers(): void
81
    {
82
        $_SERVER['REQUEST_URI'] = 'https://example.org/uri';
83
        $_SERVER['REQUEST_METHOD'] = Request::METHOD_GET;
84
85
        Router::configure(static function (Routes $routes): void {
86
            $routes->get('uri', static fn () => new JsonResponse(['key' => 'value'], [
1 ignored issue
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type object|string expected by parameter $controller of Gacela\Router\Routes::get(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
            $routes->get('uri', /** @scrutinizer ignore-type */ static fn () => new JsonResponse(['key' => 'value'], [
Loading history...
87
                'Access-Control-Allow-Origin: *',
88
                'Content-Type: application/json',
89
            ]));
90
        });
91
92
        $this->expectOutputString('{"key":"value"}');
93
94
        self::assertSame([
95
            [
96
                'header' => 'Access-Control-Allow-Origin: *',
97
                'replace' => true,
98
                'response_code' => 0,
99
            ],
100
            [
101
                'header' => 'Content-Type: application/json',
102
                'replace' => true,
103
                'response_code' => 0,
104
            ],
105
        ], $this->headers());
106
    }
107
}
108