Passed
Push — master ( 283335...4a2fc2 )
by Fran
18:22 queued 08:26
created

DispatcherTest   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 110
dl 0
loc 252
rs 10
c 0
b 0
f 0
wmc 21

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testMem() 0 7 1
A testNotFound() 0 11 1
A testTS() 0 7 1
A testExecuteRoute() 0 20 1
A testConstructor() 0 6 1
A testStats() 0 9 1
A testNormalExecution() 0 9 1
A mockDebugRouter() 0 3 1
A mockConfiguredDebugConfig() 0 6 1
A getInstance() 0 14 4
A testWarning() 0 9 2
A mockDebugSecurity() 0 3 1
A testNotAuthorized() 0 13 1
A testNotConfigured() 0 10 1
A testCatchException() 0 10 1
A testNotice() 0 11 2
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\MockObject\Stub\ReturnStub;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\MockObject\Stub\ReturnStub was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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