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 = []): self |
41
|
|
|
{ |
42
|
|
|
// Reset request/response! |
43
|
|
|
$this->di()->replace(['request' => new Request, 'response' => new Response]); |
44
|
|
|
|
45
|
|
|
$parameters['_url'] = $uri; |
46
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
47
|
|
|
$_SERVER['QUERY_STRING'] = http_build_query($parameters); |
48
|
|
|
$_SERVER['REQUEST_URI'] = '/?' . $_SERVER['QUERY_STRING']; |
49
|
|
|
$_GET = $parameters; |
50
|
|
|
|
51
|
|
|
$this->response = null; |
52
|
|
|
|
53
|
|
|
ob_start(); |
54
|
|
|
$this->app->handle($uri); |
55
|
|
|
$content = ob_get_clean(); |
56
|
|
|
|
57
|
|
|
$response = $this->di('response'); |
58
|
|
|
|
59
|
|
|
if (empty($response->getContent())) { |
60
|
|
|
$response->setContent($content); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->response = $response; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function configure(array $config): self |
69
|
|
|
{ |
70
|
|
|
$config = array_merge($this->di('config')->toArray(), $config); |
71
|
|
|
|
72
|
|
|
$this->di()->replace(['config' => new Config($config)]); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function assertResponseOk(): self |
78
|
|
|
{ |
79
|
|
|
$this->assertContains($this->responseCode(), [null, 200]); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function assertResponseNotOk(): self |
85
|
|
|
{ |
86
|
|
|
$this->assertNotContains($this->responseCode(), [null, 200]); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function assertHeaderContains(string $header, string $value): self |
92
|
|
|
{ |
93
|
|
|
$headers = $this->response->getHeaders()->toArray(); |
94
|
|
|
|
95
|
|
|
$this->assertArrayHasKey($header, $headers); |
96
|
|
|
$this->assertContains($value, $headers[$header]); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function assertHeaderKeys(array $keys, bool $has = false): self |
102
|
|
|
{ |
103
|
|
|
$headers = $this->response->getHeaders()->toArray(); |
104
|
|
|
|
105
|
|
|
foreach ($keys as $key) { |
106
|
|
|
$has |
107
|
|
|
? $this->assertArrayHasKey($key, $headers) |
108
|
|
|
: $this->assertArrayNotHasKey($key, $headers); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function assertNotHeaderKeys(array $keys): self |
115
|
|
|
{ |
116
|
|
|
return $this->assertHeaderKeys($keys, false); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function assertResponseContains(string $part): self |
120
|
|
|
{ |
121
|
|
|
$this->assertContains($part, $this->response->getContent()); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function assertStatusCode(int $code): self |
127
|
|
|
{ |
128
|
|
|
$this->assertEquals($code, $this->responseCode()); |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function assertResponseJson(): self |
134
|
|
|
{ |
135
|
|
|
$this->assertHeaderContains('Content-Type', 'application/json'); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function responseCode(): int |
141
|
|
|
{ |
142
|
|
|
$code = $this->response->getStatusCode(); |
143
|
|
|
|
144
|
|
|
return $code === null ? 200 : (int) substr($code, 0, 3); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|