Passed
Pull Request — master (#439)
by Kirill
06:35
created

IntegrationTest::testAttributeRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Router;
13
14
use Laminas\Diactoros\ServerRequest;
15
use Psr\Http\Message\ResponseInterface;
16
17
class IntegrationTest extends TestCase
18
{
19
    private $app;
20
21
    public function setUp(): void
22
    {
23
        parent::setUp();
24
        $this->app = $this->makeApp(['DEBUG' => true]);
25
    }
26
27
    public function testRoute(): void
28
    {
29
        $r = $this->get('/');
30
        $this->assertStringContainsString('index', $r->getBody()->__toString());
31
    }
32
33
    public function testAttributeRoute(): void
34
    {
35
        $r = $this->get('/attribute');
36
        $this->assertStringContainsString('attribute', $r->getBody()->__toString());
37
    }
38
39
    public function testRoute2(): void
40
    {
41
        $r = $this->post('/');
42
        $this->assertStringContainsString('method', $r->getBody()->__toString());
43
    }
44
45
    public function testRoute3(): void
46
    {
47
        $r = $this->get('/page/test');
48
49
        $this->assertSame('page-test', $r->getBody()->__toString());
50
    }
51
52
    public function testRoute4(): void
53
    {
54
        $r = $this->get('/page/about');
55
56
        $this->assertSame('about', $r->getBody()->__toString());
57
    }
58
59
    public function testRoutesWithoutNames(): void
60
    {
61
        $r = $this->get('/nameless');
62
        $this->assertSame('index', $r->getBody()->__toString());
63
64
        $r = $this->post('/nameless');
65
        $this->assertSame('method', $r->getBody()->__toString());
66
67
        $r = $this->get('/nameless/route');
68
        $this->assertSame('route', $r->getBody()->__toString());
69
    }
70
71
    public function get(
72
        $uri,
73
        array $query = [],
74
        array $headers = [],
75
        array $cookies = []
76
    ): ResponseInterface {
77
        return $this->app->getHttp()->handle($this->request($uri, 'GET', $query, $headers, $cookies));
78
    }
79
80
    public function getWithAttributes(
81
        $uri,
82
        array $attributes,
83
        array $headers = []
84
    ): ResponseInterface {
85
        $r = $this->request($uri, 'GET', [], $headers, []);
86
        foreach ($attributes as $k => $v) {
87
            $r = $r->withAttribute($k, $v);
88
        }
89
90
        return $this->app->getHttp()->handle($r);
91
    }
92
93
    public function post(
94
        $uri,
95
        array $data = [],
96
        array $headers = [],
97
        array $cookies = []
98
    ): ResponseInterface {
99
        return $this->app->getHttp()->handle(
100
            $this->request($uri, 'POST', [], $headers, $cookies)->withParsedBody($data)
101
        );
102
    }
103
104
    public function request(
105
        $uri,
106
        string $method,
107
        array $query = [],
108
        array $headers = [],
109
        array $cookies = []
110
    ): ServerRequest {
111
        $headers = array_merge([
112
            'accept-language' => 'en'
113
        ], $headers);
114
115
        return new ServerRequest(
116
            [],
117
            [],
118
            $uri,
119
            $method,
120
            'php://input',
121
            $headers,
122
            $cookies,
123
            $query
124
        );
125
    }
126
127
    public function fetchCookies(array $header)
128
    {
129
        $result = [];
130
        foreach ($header as $line) {
131
            $cookie = explode('=', $line);
132
            $result[$cookie[0]] = rawurldecode(substr($cookie[1], 0, strpos($cookie[1], ';')));
133
        }
134
135
        return $result;
136
    }
137
}
138