Passed
Pull Request — master (#277)
by Kirill
03:11
created

HttpTest::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 8
rs 10
c 0
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\Framework;
13
14
use Psr\Http\Message\ResponseInterface;
15
use Spiral\Http\Http;
16
use Spiral\App\TestApp;
17
use Zend\Diactoros\ServerRequest;
0 ignored issues
show
Bug introduced by
The type Zend\Diactoros\ServerRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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