Completed
Push — master ( 84d2a7...0a907c )
by Anton
24s queued 11s
created

HttpTest::getWithAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 11
rs 10
cc 2
nc 2
nop 3
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\Framework;
13
14
use Psr\Http\Message\ResponseInterface;
15
use Spiral\App\TestApp;
16
use Spiral\Http\Http;
17
use Zend\Diactoros\ServerRequest;
18
19
abstract class HttpTest extends BaseTest
20
{
21
    /** @var TestApp */
22
    protected $app;
23
24
    /** @var Http */
25
    protected $http;
26
27
    public function setUp(): void
28
    {
29
        $this->app = $this->makeApp();
30
        $this->http = $this->app->get(Http::class);
31
    }
32
33
    protected function get(
34
        $uri,
35
        array $query = [],
36
        array $headers = [],
37
        array $cookies = []
38
    ): ResponseInterface {
39
        return $this->http->handle($this->request($uri, 'GET', $query, $headers, $cookies));
40
    }
41
42
    protected function getWithAttributes(
43
        $uri,
44
        array $attributes,
45
        array $headers = []
46
    ): ResponseInterface {
47
        $r = $this->request($uri, 'GET', [], $headers, []);
48
        foreach ($attributes as $k => $v) {
49
            $r = $r->withAttribute($k, $v);
50
        }
51
52
        return $this->http->handle($r);
53
    }
54
55
56
    protected function post(
57
        $uri,
58
        array $data = [],
59
        array $headers = [],
60
        array $cookies = []
61
    ): ResponseInterface {
62
        return $this->http->handle(
63
            $this->request($uri, 'POST', [], $headers, $cookies)->withParsedBody($data)
64
        );
65
    }
66
67
    protected function request(
68
        $uri,
69
        string $method,
70
        array $query = [],
71
        array $headers = [],
72
        array $cookies = []
73
    ): ServerRequest {
74
        return new ServerRequest(
75
            [],
76
            [],
77
            $uri,
78
            $method,
79
            'php://input',
80
            $headers,
81
            $cookies,
82
            $query
83
        );
84
    }
85
86
    protected function fetchCookies(array $header)
87
    {
88
        $result = [];
89
        foreach ($header as $line) {
90
            $cookie = explode('=', $line);
91
            $result[$cookie[0]] = rawurldecode(substr($cookie[1], 0, strpos($cookie[1], ';')));
92
        }
93
94
        return $result;
95
    }
96
}
97