|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PSFS\tests\base; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\MockObject\Exception; |
|
6
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use PSFS\base\config\Config; |
|
9
|
|
|
use PSFS\base\exception\GeneratorException; |
|
10
|
|
|
use PSFS\base\Router; |
|
11
|
|
|
use PSFS\base\Security; |
|
12
|
|
|
use PSFS\base\types\helpers\Inspector; |
|
13
|
|
|
use PSFS\base\types\helpers\ResponseHelper; |
|
14
|
|
|
use PSFS\base\types\helpers\SecurityHelper; |
|
15
|
|
|
use PSFS\controller\base\Admin; |
|
16
|
|
|
use PSFS\Dispatcher; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class DispatcherTest |
|
20
|
|
|
* @package PSFS\tests\base |
|
21
|
|
|
*/ |
|
22
|
|
|
class DispatcherTest extends TestCase |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Método que devuelve una instancia del Dspatcher |
|
27
|
|
|
* @param MockObject|Config|null $config |
|
28
|
|
|
* @param MockObject|Router|null $router |
|
29
|
|
|
* @param MockObject|Security|null $security |
|
30
|
|
|
* @return Dispatcher |
|
31
|
|
|
*/ |
|
32
|
|
|
private function getInstance(MockObject|Config $config = null, MockObject|Router $router = null, MockObject|Security $security = null): Dispatcher |
|
33
|
|
|
{ |
|
34
|
|
|
$dispatcher = Dispatcher::getInstance(); |
|
35
|
|
|
Security::setTest(false); |
|
36
|
|
|
if (null !== $config) { |
|
37
|
|
|
$dispatcher->config = $config; |
|
38
|
|
|
} |
|
39
|
|
|
if (null !== $router) { |
|
40
|
|
|
$dispatcher->router = $router; |
|
41
|
|
|
} |
|
42
|
|
|
if (null !== $security) { |
|
43
|
|
|
$dispatcher->security = $security; |
|
44
|
|
|
} |
|
45
|
|
|
return $dispatcher; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Método que crea un objeto Mock seteado a debug |
|
50
|
|
|
* @param boolean $configured |
|
51
|
|
|
* @param boolean $debug |
|
52
|
|
|
* @return MockObject |
|
53
|
|
|
* @throws Exception |
|
54
|
|
|
*/ |
|
55
|
|
|
private function mockConfiguredDebugConfig(bool $configured = true, bool $debug = true): MockObject |
|
56
|
|
|
{ |
|
57
|
|
|
$config = $this->createMock(Config::class); |
|
58
|
|
|
$config->expects($this->any())->method('isConfigured')->will($this->returnValue($configured)); |
|
59
|
|
|
$config->expects($this->any())->method('getDebugMode')->will($this->returnValue($debug)); |
|
60
|
|
|
return $config; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Método que mockea la clase Router |
|
65
|
|
|
* @return MockObject |
|
66
|
|
|
* @throws Exception |
|
67
|
|
|
*/ |
|
68
|
|
|
private function mockDebugRouter(): MockObject |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->createMock(Router::class); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Método que mockea la clase Router |
|
75
|
|
|
* @return MockObject |
|
76
|
|
|
* @throws Exception |
|
77
|
|
|
*/ |
|
78
|
|
|
private function mockDebugSecurity(): MockObject |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->createMock(Security::class); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testConstructor() |
|
84
|
|
|
{ |
|
85
|
|
|
$dispatcher = $this->getInstance(); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertNotNull($dispatcher); |
|
88
|
|
|
$this->assertInstanceOf(Dispatcher::class, $dispatcher); |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function testMem() |
|
93
|
|
|
{ |
|
94
|
|
|
$dispatcher = $this->getInstance(); |
|
95
|
|
|
$this->assertNotNull($dispatcher->getMem('Bytes')); |
|
96
|
|
|
$this->assertNotNull($dispatcher->getMem('KBytes')); |
|
97
|
|
|
$this->assertNotNull($dispatcher->getMem('MBytes')); |
|
98
|
|
|
$this->assertNotNull($dispatcher->getMem()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testTS() |
|
102
|
|
|
{ |
|
103
|
|
|
$dispatcher = $this->getInstance(); |
|
104
|
|
|
$ts = $dispatcher->getTs(); |
|
105
|
|
|
$this->assertNotNull($ts); |
|
106
|
|
|
usleep(200); |
|
107
|
|
|
$this->assertGreaterThan($ts, $dispatcher->getTs()); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Check if notice was converted to exception |
|
112
|
|
|
* @throws Exception |
|
113
|
|
|
*/ |
|
114
|
|
|
public function testNotice() |
|
115
|
|
|
{ |
|
116
|
|
|
$this->getInstance($this->mockConfiguredDebugConfig()); |
|
117
|
|
|
try { |
|
118
|
|
|
$test = array(); |
|
119
|
|
|
//This throws notice because position 0 in array not exists |
|
120
|
|
|
$test = $test[0] === true; |
|
121
|
|
|
unset($test); |
|
122
|
|
|
$this->fail('Exception has not been thrown'); |
|
123
|
|
|
} catch (\Exception $e) { |
|
124
|
|
|
$this->assertTrue(true); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Check if warning was converted to exception |
|
130
|
|
|
* @throws Exception |
|
131
|
|
|
*/ |
|
132
|
|
|
public function testWarning() |
|
133
|
|
|
{ |
|
134
|
|
|
$this->getInstance($this->mockConfiguredDebugConfig()); |
|
135
|
|
|
try { |
|
136
|
|
|
//This throws a warning because file 'test.txt' not exists |
|
137
|
|
|
file_get_contents(__DIR__ . 'test.txt'); |
|
138
|
|
|
$this->fail('Exception has not been thrown'); |
|
139
|
|
|
} catch (\Exception $e) { |
|
140
|
|
|
$this->assertTrue(true); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @throws GeneratorException |
|
146
|
|
|
* @throws Exception |
|
147
|
|
|
*/ |
|
148
|
|
|
public function testNormalExecution() |
|
149
|
|
|
{ |
|
150
|
|
|
$router = $this->mockDebugRouter(); |
|
151
|
|
|
$router->expects($this->any())->method('execute')->willReturn('OK'); |
|
152
|
|
|
|
|
153
|
|
|
$dispatcher = $this->getInstance($this->mockConfiguredDebugConfig(), $router); |
|
154
|
|
|
$response = $dispatcher->run(); |
|
155
|
|
|
$this->assertNotNull($response); |
|
156
|
|
|
$this->assertEquals('OK', $response); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @throws GeneratorException |
|
161
|
|
|
* @throws Exception |
|
162
|
|
|
*/ |
|
163
|
|
|
public function testNotConfigured() |
|
164
|
|
|
{ |
|
165
|
|
|
$this->expectExceptionMessage("CONFIG"); |
|
166
|
|
|
$this->expectException(\PSFS\base\exception\ConfigException::class); |
|
167
|
|
|
$config = $this->mockConfiguredDebugConfig(false); |
|
168
|
|
|
$router = $this->mockDebugRouter(); |
|
169
|
|
|
$router->expects($this->any())->method('httpNotFound')->willThrowException(new \PSFS\base\exception\ConfigException('CONFIG')); |
|
170
|
|
|
$dispatcher = $this->getInstance($config, $router); |
|
171
|
|
|
Security::setTest(true); |
|
172
|
|
|
$dispatcher->run(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @throws GeneratorException |
|
177
|
|
|
* @throws Exception |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testNotAuthorized() |
|
180
|
|
|
{ |
|
181
|
|
|
$this->expectExceptionMessage("NOT AUTHORIZED"); |
|
182
|
|
|
$this->expectException(\PSFS\base\exception\SecurityException::class); |
|
183
|
|
|
$config = $this->mockConfiguredDebugConfig(); |
|
184
|
|
|
$router = $this->mockDebugRouter(); |
|
185
|
|
|
$router->expects($this->any())->method('execute')->willThrowException(new \PSFS\base\exception\SecurityException('NOT AUTHORIZED')); |
|
186
|
|
|
Security::dropInstance(); |
|
187
|
|
|
$security = $this->mockDebugSecurity(); |
|
188
|
|
|
$security->expects($this->any())->method('notAuthorized')->willThrowException(new \PSFS\base\exception\SecurityException('NOT AUTHORIZED')); |
|
189
|
|
|
$dispatcher = $this->getInstance($config, $router, $security); |
|
190
|
|
|
Security::setTest(true); |
|
191
|
|
|
$dispatcher->run(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @throws GeneratorException |
|
196
|
|
|
* @throws Exception |
|
197
|
|
|
*/ |
|
198
|
|
|
public function testNotFound() |
|
199
|
|
|
{ |
|
200
|
|
|
$this->expectExceptionMessage("NOT FOUND"); |
|
201
|
|
|
$this->expectException(\PSFS\base\exception\RouterException::class); |
|
202
|
|
|
$config = $this->mockConfiguredDebugConfig(); |
|
203
|
|
|
$router = $this->mockDebugRouter(); |
|
204
|
|
|
$router->expects($this->any())->method('execute')->willThrowException(new \PSFS\base\exception\RouterException('NOT FOUND')); |
|
205
|
|
|
$router->expects($this->any())->method('httpNotFound')->willThrowException(new \PSFS\base\exception\RouterException('NOT FOUND')); |
|
206
|
|
|
$dispatcher = $this->getInstance($config, $router); |
|
207
|
|
|
Security::setTest(true); |
|
208
|
|
|
$dispatcher->run(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @throws GeneratorException |
|
213
|
|
|
* @throws Exception |
|
214
|
|
|
*/ |
|
215
|
|
|
public function testCatchException() |
|
216
|
|
|
{ |
|
217
|
|
|
$this->expectExceptionMessage("CATCH EXCEPTION"); |
|
218
|
|
|
$this->expectException(\Exception::class); |
|
219
|
|
|
$router = $this->mockDebugRouter(); |
|
220
|
|
|
$router->expects($this->any())->method('execute')->willThrowException(new \Exception('CATCH EXCEPTION')); |
|
221
|
|
|
$router->expects($this->any())->method('httpNotFound')->willThrowException(new \Exception('CATCH EXCEPTION')); |
|
222
|
|
|
$dispatcher = $this->getInstance($this->mockConfiguredDebugConfig(), $router); |
|
223
|
|
|
Security::setTest(true); |
|
224
|
|
|
$dispatcher->run(); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function testStats() |
|
228
|
|
|
{ |
|
229
|
|
|
Inspector::stats('test1', Inspector::SCOPE_DEBUG); |
|
230
|
|
|
$stats = Inspector::getStats(); |
|
231
|
|
|
$this->assertNotEmpty($stats, 'Empty stats'); |
|
232
|
|
|
Inspector::stats('test2', Inspector::SCOPE_DEBUG); |
|
233
|
|
|
$secondStats = Inspector::getStats(Inspector::SCOPE_DEBUG); |
|
234
|
|
|
$this->assertNotEmpty($secondStats, 'Empty stats'); |
|
235
|
|
|
$this->assertNotEquals($stats, $secondStats, 'Stats are similar'); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @throws GeneratorException |
|
240
|
|
|
* @throws Exception |
|
241
|
|
|
*/ |
|
242
|
|
|
public function testExecuteRoute() |
|
243
|
|
|
{ |
|
244
|
|
|
$router = Router::getInstance(); |
|
245
|
|
|
$security = Security::getInstance(); |
|
246
|
|
|
$dispatcher = $this->getInstance($this->mockConfiguredDebugConfig(), $router, $security); |
|
247
|
|
|
Admin::setTest(true); |
|
248
|
|
|
ResponseHelper::setTest(true); |
|
249
|
|
|
Security::setTest(true); |
|
250
|
|
|
SecurityHelper::setTest(true); |
|
251
|
|
|
Config::setTest(true); |
|
252
|
|
|
$result = $dispatcher->run('/admin/config/params'); |
|
253
|
|
|
$this->assertNotEmpty($result, 'Empty response'); |
|
254
|
|
|
$jsonDecodedResponse = json_decode($result, true); |
|
255
|
|
|
$this->assertNotNull($jsonDecodedResponse, 'Bad JSON response'); |
|
256
|
|
|
$this->assertTrue(is_array($jsonDecodedResponse), 'Bad decoded response'); |
|
257
|
|
|
Admin::setTest(false); |
|
258
|
|
|
Security::setTest(false); |
|
259
|
|
|
SecurityHelper::setTest(false); |
|
260
|
|
|
ResponseHelper::setTest(false); |
|
261
|
|
|
Config::setTest(false); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|