Completed
Push — master ( 465be6...9ae858 )
by Kirill
31s queued 20s
created

IntegrationTest::testRoute3()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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 testRoute2(): void
34
    {
35
        $r = $this->post('/');
36
        $this->assertStringContainsString('method', $r->getBody()->__toString());
37
    }
38
39
    public function testRoute3(): void
40
    {
41
        $r = $this->get('/page/test');
42
43
        $this->assertSame('page-test', $r->getBody()->__toString());
44
    }
45
46
    public function testRoute4(): void
47
    {
48
        $r = $this->get('/page/about');
49
50
        $this->assertSame('about', $r->getBody()->__toString());
51
    }
52
53
    public function get(
54
        $uri,
55
        array $query = [],
56
        array $headers = [],
57
        array $cookies = []
58
    ): ResponseInterface {
59
        return $this->app->getHttp()->handle($this->request($uri, 'GET', $query, $headers, $cookies));
60
    }
61
62
    public function getWithAttributes(
63
        $uri,
64
        array $attributes,
65
        array $headers = []
66
    ): ResponseInterface {
67
        $r = $this->request($uri, 'GET', [], $headers, []);
68
        foreach ($attributes as $k => $v) {
69
            $r = $r->withAttribute($k, $v);
70
        }
71
72
        return $this->app->getHttp()->handle($r);
73
    }
74
75
    public function post(
76
        $uri,
77
        array $data = [],
78
        array $headers = [],
79
        array $cookies = []
80
    ): ResponseInterface {
81
        return $this->app->getHttp()->handle(
82
            $this->request($uri, 'POST', [], $headers, $cookies)->withParsedBody($data)
83
        );
84
    }
85
86
    public function request(
87
        $uri,
88
        string $method,
89
        array $query = [],
90
        array $headers = [],
91
        array $cookies = []
92
    ): ServerRequest {
93
        $headers = array_merge([
94
            'accept-language' => 'en'
95
        ], $headers);
96
97
        return new ServerRequest(
98
            [],
99
            [],
100
            $uri,
101
            $method,
102
            'php://input',
103
            $headers,
104
            $cookies,
105
            $query
106
        );
107
    }
108
109
    public function fetchCookies(array $header)
110
    {
111
        $result = [];
112
        foreach ($header as $line) {
113
            $cookie = explode('=', $line);
114
            $result[$cookie[0]] = rawurldecode(substr($cookie[1], 0, strpos($cookie[1], ';')));
115
        }
116
117
        return $result;
118
    }
119
}
120