Completed
Pull Request — master (#19)
by Jitendra
03:25
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
9
class WebTestCase extends TestCase
10
{
11
    protected $app;
12
13
    public function setUp()
14
    {
15
        // A new instance of fully configured app :)
16
        $this->app = include __DIR__ . '/../example/index.php';
17
18
        $this->resetDi();
19
    }
20
21
    protected function resetDi()
22
    {
23
        \Phalcon\Di::reset();
24
        \Phalcon\Di::setDefault($this->app->getDI());
25
    }
26
27
    protected function di(string $service = null)
28
    {
29
        if ($service) {
30
            return $this->app->getDI()->resolve($service);
31
        }
32
33
        return $this->app->getDI();
34
    }
35
36
    /**
37
     * This is stripped down barebone version for our example/ endpoints.
38
     */
39
    protected function doRequest(string $uri, array $parameters = [], array $headers = []): self
40
    {
41
        if ($uri[0] !== '/') {
42
            list($method, $uri) = explode(' ', $uri, 2);
43
        }
44
45
        $parameters['_url']        = $uri;
46
        $_SERVER['REQUEST_METHOD'] = $method ?? 'GET';
47
        $_SERVER['QUERY_STRING']   = http_build_query($parameters);
48
        $_SERVER['REQUEST_URI']    = '/?' . $_SERVER['QUERY_STRING'];
49
        $_GET                      = $parameters;
50
51
        $headerKeys = [];
52
        foreach ($headers as $key => $value) {
53
            if (!in_array($key, ['Origin', 'Authorization'])) {
54
                $key = 'HTTP_' . str_replace('-', '_', $key);
55
            }
56
            $_SERVER[$headerKeys[] = strtoupper($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
        foreach ($headerKeys as $key) {
74
            unset($_SERVER[$key]);
75
        }
76
        foreach ($parameters as $key) {
77
            unset($_REQUEST[$key], $_GET[$key], $_POST[$key]);
78
        }
79
80
        $this->response = $response;
81
82
        return $this;
83
    }
84
85
    protected function config(string $path)
86
    {
87
        return $this->di('config')->path($path);
88
    }
89
90
    protected function configure(string $node, array $config): self
91
    {
92
        $config = array_replace_recursive($this->di('config')->toArray(), [$node => $config]);
93
94
        $this->di()->replace(['config' => new Config($config)]);
95
96
        return $this;
97
    }
98
99
    protected function assertResponseOk(): self
100
    {
101
        $this->assertContains($this->responseCode(), [null, 200]);
102
103
        return $this;
104
    }
105
106
    protected function assertResponseNotOk(): self
107
    {
108
        $this->assertNotContains($this->responseCode(), [null, 200]);
109
110
        return $this;
111
    }
112
113
    protected function assertHeaderContains(string $header, string $value): self
114
    {
115
        $headers = $this->response->getHeaders()->toArray();
116
117
        $this->assertArrayHasKey($header, $headers);
118
        $this->assertContains($value, $headers[$header]);
119
120
        return $this;
121
    }
122
123
    protected function assertHeaderKeys(array $keys, bool $has = true): self
124
    {
125
        $headers = $this->response->getHeaders()->toArray();
126
127
        foreach ($keys as $key) {
128
            $has
129
                ? $this->assertArrayHasKey($key, $headers)
130
                : $this->assertArrayNotHasKey($key, $headers);
131
        }
132
133
        return $this;
134
    }
135
136
    protected function assertNotHeaderKeys(array $keys): self
137
    {
138
        return $this->assertHeaderKeys($keys, false);
139
    }
140
141
    protected function assertResponseContains(string $part): self
142
    {
143
        $this->assertContains($part, $this->response->getContent());
144
145
        return $this;
146
    }
147
148
    protected function assertStatusCode(int $code): self
149
    {
150
        $this->assertEquals($code, $this->responseCode());
151
152
        return $this;
153
    }
154
155
    protected function assertResponseJson(): self
156
    {
157
        $this->assertHeaderContains('Content-Type', 'application/json');
158
159
        return $this;
160
    }
161
162
    protected function responseCode(): int
163
    {
164
        $code = $this->response->getStatusCode();
165
166
        return $code === null ? 200 : (int) substr($code, 0, 3);
167
    }
168
}
169