Passed
Pull Request — master (#19)
by Jitendra
01:56
created

WebTestCase   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 110
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhalconExt\Test;
4
5
use Phalcon\Config;
6
use Phalcon\Http\Request;
7
use Phalcon\Http\Response;
8
use PHPUnit\Framework\TestCase;
9
10
class WebTestCase extends TestCase
11
{
12
    protected $app;
13
14
    public function setUp()
15
    {
16
        // A new instance of fully configured app :)
17
        $this->app = include __DIR__ . '/../example/index.php';
18
19
        $this->resetDi();
20
    }
21
22
    protected function resetDi()
23
    {
24
        \Phalcon\Di::reset();
25
        \Phalcon\Di::setDefault($this->app->getDI());
26
    }
27
28
    protected function di(string $service = null)
29
    {
30
        if ($service) {
31
            return $this->app->getDI()->resolve($service);
32
        }
33
34
        return $this->app->getDI();
35
    }
36
37
    /**
38
     * This is stripped down barebone version for our example/ endpoints.
39
     */
40
    protected function doRequest(string $uri, array $parameters = [], array $headers = []): self
41
    {
42
        if ($uri[0] !== '/') {
43
            list($method, $uri) = explode(' ', $uri, 2);
44
        }
45
46
        $parameters['_url']        = $uri;
47
        $_SERVER['REQUEST_METHOD'] = $method ?? 'GET';
48
        $_SERVER['QUERY_STRING']   = http_build_query($parameters);
49
        $_SERVER['REQUEST_URI']    = '/?' . $_SERVER['QUERY_STRING'];
50
        $_GET                      = $parameters;
51
52
        foreach ($headers as $key => $value) {
53
            if (!in_array($key, ['Origin', 'Authorization'])) {
54
                $key = 'HTTP_' . strtoupper(str_replace('-', '_', $key));
55
            }
56
            $_SERVER[$key] = $value;
57
        }
58
59
        $this->response = null;
60
61
        // Reset request/response!
62
        $this->di()->replace(['request' => new Request, 'response' => new Response]);
63
64
        ob_start();
65
        $this->app->handle($uri);
66
        $content = ob_get_clean();
67
68
        $response = $this->di('response');
69
70
        if (empty($response->getContent())) {
71
            $response->setContent($content);
72
        }
73
74
        $this->response = $response;
75
76
        return $this;
77
    }
78
79
    protected function configure(array $config): self
80
    {
81
        $config = array_merge($this->di('config')->toArray(), $config);
82
83
        $this->di()->replace(['config' => new Config($config)]);
84
85
        return $this;
86
    }
87
88
    protected function assertResponseOk(): self
89
    {
90
        $this->assertContains($this->responseCode(), [null, 200]);
91
92
        return $this;
93
    }
94
95
    protected function assertResponseNotOk(): self
96
    {
97
        $this->assertNotContains($this->responseCode(), [null, 200]);
98
99
        return $this;
100
    }
101
102
    protected function assertHeaderContains(string $header, string $value): self
103
    {
104
        $headers = $this->response->getHeaders()->toArray();
105
106
        $this->assertArrayHasKey($header, $headers);
107
        $this->assertContains($value, $headers[$header]);
108
109
        return $this;
110
    }
111
112
    protected function assertHeaderKeys(array $keys, bool $has = false): self
113
    {
114
        $headers = $this->response->getHeaders()->toArray();
115
116
        foreach ($keys as $key) {
117
            $has
118
                ? $this->assertArrayHasKey($key, $headers)
119
                : $this->assertArrayNotHasKey($key, $headers);
120
        }
121
122
        return $this;
123
    }
124
125
    protected function assertNotHeaderKeys(array $keys): self
126
    {
127
        return $this->assertHeaderKeys($keys, false);
128
    }
129
130
    protected function assertResponseContains(string $part): self
131
    {
132
        $this->assertContains($part, $this->response->getContent());
133
134
        return $this;
135
    }
136
137
    protected function assertStatusCode(int $code): self
138
    {
139
        $this->assertEquals($code, $this->responseCode());
140
141
        return $this;
142
    }
143
144
    protected function assertResponseJson(): self
145
    {
146
        $this->assertHeaderContains('Content-Type', 'application/json');
147
148
        return $this;
149
    }
150
151
    protected function responseCode(): int
152
    {
153
        $code = $this->response->getStatusCode();
154
155
        return $code === null ? 200 : (int) substr($code, 0, 3);
156
    }
157
}
158