|
1
|
|
|
<?php |
|
2
|
|
|
use Mezon\HtmlTemplate\HtmlTemplate; |
|
3
|
|
|
use Mezon\Rest; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* View class |
|
7
|
|
|
* |
|
8
|
|
|
* @author Dodonov A.A. |
|
9
|
|
|
*/ |
|
10
|
|
|
class TestView extends \Mezon\Application\View |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(string $content) |
|
14
|
|
|
{ |
|
15
|
|
|
parent::__construct(null, 'default'); |
|
16
|
|
|
|
|
17
|
|
|
$this->content = $content; |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function render(string $viewName = ''): string |
|
21
|
|
|
{ |
|
22
|
|
|
return $this->content; |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Application for testing purposes. |
|
28
|
|
|
*/ |
|
29
|
|
|
class TestCommonApplication extends \Mezon\Application\CommonApplication |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Constructor |
|
34
|
|
|
*/ |
|
35
|
|
|
function __construct() |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
parent::__construct(new HtmlTemplate(__DIR__, 'index')); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
function actionArrayResult(): array |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
return [ |
|
43
|
|
|
'title' => 'Array result', |
|
44
|
|
|
'main' => 'Route main' |
|
45
|
|
|
]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
function actionViewResult(): array |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
return [ |
|
51
|
|
|
'title' => 'View result', |
|
52
|
|
|
'main' => new TestView('Test view result') |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
function actionInvalid(): string |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
return 'Invalid'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
function actionRest(): array |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
throw (new Rest\Exception('exception', - 1, 502, 'body')); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
function redirectTo($url):void{ |
|
|
|
|
|
|
67
|
|
|
// do nothing |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
class CommonApplicationUnitTest extends \PHPUnit\Framework\TestCase |
|
72
|
|
|
{ |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Running with complex router result |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testComplexRouteResult() |
|
78
|
|
|
{ |
|
79
|
|
|
$application = new TestCommonApplication(); |
|
80
|
|
|
|
|
81
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
82
|
|
|
$_GET['r'] = '/array-result/'; |
|
83
|
|
|
|
|
84
|
|
|
ob_start(); |
|
85
|
|
|
$application->run(); |
|
86
|
|
|
$output = ob_get_contents(); |
|
87
|
|
|
ob_end_clean(); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertTrue(strpos($output, 'Array result') !== false, 'Template compilation failed (1)'); |
|
90
|
|
|
$this->assertTrue(strpos($output, 'Route main') !== false, 'Template compilation failed (2)'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Compiling page with functional view |
|
95
|
|
|
*/ |
|
96
|
|
|
public function testComplexViewRenderring() |
|
97
|
|
|
{ |
|
98
|
|
|
// setup |
|
99
|
|
|
$application = new TestCommonApplication(); |
|
100
|
|
|
|
|
101
|
|
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
102
|
|
|
$_GET['r'] = '/view-result/'; |
|
103
|
|
|
$_GET['redirect-to'] = 'redirectTo'; |
|
104
|
|
|
|
|
105
|
|
|
// test body |
|
106
|
|
|
ob_start(); |
|
107
|
|
|
$application->run(); |
|
108
|
|
|
$output = ob_get_contents(); |
|
109
|
|
|
ob_end_clean(); |
|
110
|
|
|
|
|
111
|
|
|
// assertions |
|
112
|
|
|
$this->assertStringContainsString('View result', $output); |
|
113
|
|
|
$this->assertStringContainsString('Test view result', $output); |
|
114
|
|
|
$this->assertStringContainsString('redirectTo', $output); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Testing handleException method |
|
119
|
|
|
*/ |
|
120
|
|
|
public function testHandleException() |
|
121
|
|
|
{ |
|
122
|
|
|
// setup |
|
123
|
|
|
$application = new TestCommonApplication(); |
|
124
|
|
|
$output = ''; |
|
|
|
|
|
|
125
|
|
|
$e = new Exception('', 0); |
|
126
|
|
|
|
|
127
|
|
|
// test body |
|
128
|
|
|
ob_start(); |
|
129
|
|
|
$application->handleException($e); |
|
130
|
|
|
$output = ob_get_contents(); |
|
131
|
|
|
ob_end_clean(); |
|
132
|
|
|
|
|
133
|
|
|
// assertions |
|
134
|
|
|
$this->assertStringContainsString('"message"', $output); |
|
135
|
|
|
$this->assertStringContainsString('"code"', $output); |
|
136
|
|
|
$this->assertStringContainsString('"call_stack"', $output); |
|
137
|
|
|
$this->assertStringContainsString('"host"', $output); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Testing handle_rest_exception method |
|
142
|
|
|
*/ |
|
143
|
|
|
public function testHandleRestException() |
|
144
|
|
|
{ |
|
145
|
|
|
// setup |
|
146
|
|
|
$application = new TestCommonApplication(); |
|
147
|
|
|
|
|
148
|
|
|
$e = new Rest\Exception('', 0, 200, ''); |
|
149
|
|
|
// test body |
|
150
|
|
|
ob_start(); |
|
151
|
|
|
$application->handleRestException($e); |
|
152
|
|
|
$output = ob_get_contents(); |
|
153
|
|
|
ob_end_clean(); |
|
154
|
|
|
|
|
155
|
|
|
// assertions |
|
156
|
|
|
$this->assertStringContainsString('"message"', $output); |
|
157
|
|
|
$this->assertStringContainsString('"code"', $output); |
|
158
|
|
|
$this->assertStringContainsString('"call_stack"', $output); |
|
159
|
|
|
$this->assertStringContainsString('"host"', $output); |
|
160
|
|
|
$this->assertStringContainsString('"host"', $output); |
|
161
|
|
|
$this->assertStringContainsString('"httpBody"', $output); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Testing handleException method |
|
166
|
|
|
*/ |
|
167
|
|
|
public function testHandleExceptionWithHost() |
|
168
|
|
|
{ |
|
169
|
|
|
// setup |
|
170
|
|
|
$application = new TestCommonApplication(); |
|
171
|
|
|
$output = ''; |
|
|
|
|
|
|
172
|
|
|
$_SERVER['HTTP_HOST'] = 'some host'; |
|
173
|
|
|
$_SERVER['REQUEST_URI'] = 'some uri'; |
|
174
|
|
|
try { |
|
175
|
|
|
throw (new Exception('', 0)); |
|
176
|
|
|
} catch (Exception $e) { |
|
177
|
|
|
// test body |
|
178
|
|
|
ob_start(); |
|
179
|
|
|
$application->handleException($e); |
|
180
|
|
|
$output = ob_get_contents(); |
|
181
|
|
|
ob_end_clean(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
// assertions |
|
185
|
|
|
$output = json_decode(str_replace('<pre>', '', $output), true); |
|
186
|
|
|
$this->assertEquals('some hostsome uri', $output['host']); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Testing exception throwing after invalid route handling |
|
191
|
|
|
*/ |
|
192
|
|
|
public function testInvalidRouteException(): void |
|
193
|
|
|
{ |
|
194
|
|
|
// setup and assertions |
|
195
|
|
|
$_GET['r'] = 'invalid'; |
|
196
|
|
|
$application = $this->getMockBuilder(TestCommonApplication::class) |
|
|
|
|
|
|
197
|
|
|
->setMethods([ |
|
198
|
|
|
'handleException' |
|
199
|
|
|
]) |
|
200
|
|
|
->getMock(); |
|
201
|
|
|
$application->expects($this->once()) |
|
202
|
|
|
->method('handleException'); |
|
203
|
|
|
|
|
204
|
|
|
// test body |
|
205
|
|
|
$application->run(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Testing exception throwing after invalid route handling |
|
210
|
|
|
*/ |
|
211
|
|
|
public function testRestException(): void |
|
212
|
|
|
{ |
|
213
|
|
|
// setup and assertions |
|
214
|
|
|
$_GET['r'] = 'rest'; |
|
215
|
|
|
$application = $this->getMockBuilder(TestCommonApplication::class) |
|
|
|
|
|
|
216
|
|
|
->setMethods([ |
|
217
|
|
|
'handleRestException' |
|
218
|
|
|
]) |
|
219
|
|
|
->getMock(); |
|
220
|
|
|
$application->expects($this->once()) |
|
221
|
|
|
->method('handleRestException'); |
|
222
|
|
|
|
|
223
|
|
|
// test body |
|
224
|
|
|
$application->run(); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|