SplashDefaultRouterTest::testExpirationTag()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
3
namespace Mouf\Mvc\Splash\Routers;
4
5
use Cache\Adapter\PHPArray\ArrayCachePool;
6
use Doctrine\Common\Annotations\AnnotationReader;
7
use Doctrine\Common\Annotations\AnnotationRegistry;
8
use Mouf\Mvc\Splash\Controllers\HttpErrorsController;
9
use Mouf\Mvc\Splash\Exception\PageNotFoundException;
10
use Mouf\Mvc\Splash\Exception\SplashMissingParameterException;
11
use Mouf\Mvc\Splash\Fixtures\TestBadParamController;
12
use Mouf\Mvc\Splash\Fixtures\TestController2;
13
use Mouf\Mvc\Splash\Fixtures\TestExtendedController2;
14
use Mouf\Mvc\Splash\Fixtures\TestFilteredController;
15
use Mouf\Mvc\Splash\Services\ControllerAnalyzer;
16
use Mouf\Mvc\Splash\Services\ControllerRegistry;
17
use Mouf\Mvc\Splash\Services\ParameterFetcherRegistry;
18
use Mouf\Mvc\Splash\Services\SplashUtils;
19
use Mouf\Mvc\Splash\Utils\SplashException;
20
use Mouf\Picotainer\Picotainer;
21
use Psr\Cache\CacheItemPoolInterface;
22
use Zend\Diactoros\Response\HtmlResponse;
23
use Zend\Diactoros\Response\JsonResponse;
24
use Zend\Diactoros\Response\RedirectResponse;
25
use Zend\Diactoros\ServerRequest;
26
use Mouf\Mvc\Splash\Fixtures\TestController3;
27
28
class SplashDefaultRouterTest extends \PHPUnit_Framework_TestCase
29
{
30
    protected function setUp()
31
    {
32
        $loader = require __DIR__.'../../../../../../vendor/autoload.php';
33
        AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...istry::registerLoader() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
34
    }
35
36
    public function testRoute()
37
    {
38
        $container = new Picotainer([
39
            'controller' => function () {
40
                return new TestController2();
41
            },
42
        ]);
43
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
44
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
45
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']);
46
        $defaultRouter = new SplashDefaultRouter($container, [
47
            $controllerRegistry,
48
        ], $parameterFetcherRegistry);
49
50
        $request = new ServerRequest([], [], '/foo/var/bar', 'GET', 'php://input',
51
            [],
52
            [],
53
            ['id' => 42]
54
            );
55
        $response = new HtmlResponse('');
56
        $response = $defaultRouter($request, $response);
57
        $this->assertInstanceOf(JsonResponse::class, $response);
58
        /* @var $response JsonResponse */
59
        $decodedResponse = json_decode((string) $response->getBody(), true);
60
        $this->assertEquals(42, $decodedResponse['id']);
61
        $this->assertEquals('var', $decodedResponse['var']);
62
        $this->assertEquals(42, $decodedResponse['id2']);
63
        $this->assertEquals(42, $decodedResponse['opt']);
64
65
        // Now, let's test the redirect
66
        $request = new ServerRequest([], [], '/foo/var/bar/', 'GET', 'php://input',
67
            [],
68
            [],
69
            ['id' => 42]
70
        );
71
        $response = new HtmlResponse('');
72
        $response = $defaultRouter($request, $response);
73
        $this->assertInstanceOf(RedirectResponse::class, $response);
74
        $this->assertEquals('/foo/var/bar', $response->getHeader('Location')[0]);
75
76
        // Now, let's test the second kind of redirect
77
        $request = new ServerRequest([], [], '/controller', 'GET');
78
        $response = new HtmlResponse('');
79
        $response = $defaultRouter($request, $response);
80
        $this->assertInstanceOf(RedirectResponse::class, $response);
81
        $this->assertEquals('/controller/', $response->getHeader('Location')[0]);
82
    }
83
84
    public function testUnknownRoute()
85
    {
86
        $container = new Picotainer([
87
        ]);
88
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
89
        $defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry);
90
91
        $request = new ServerRequest([], [], '/foo', 'GET');
92
        $response = new HtmlResponse('');
93
        $response = $defaultRouter($request, $response, function () {
94
            return new HtmlResponse('Not found', 404);
95
        });
96
        $this->assertInstanceOf(HtmlResponse::class, $response);
97
        /* @var $response HtmlResponse */
98
        $this->assertEquals(404, $response->getStatusCode());
99
        $this->assertEquals('Not found', (string) $response->getBody());
100
101
        // Now, let's retry without a $out parameter and let's check we get an exception
102
        $this->expectException(PageNotFoundException::class);
103
        $response = $defaultRouter($request, $response);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
104
    }
105
106
107 View Code Duplication
    public function testEmojiRoute()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        $container = new Picotainer([
110
            'controller' => function () {
111
            return new TestController3();
112
            },
113
            ]);
114
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
115
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
116
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']);
117
        $defaultRouter = new SplashDefaultRouter($container, [
118
            $controllerRegistry,
119
        ], $parameterFetcherRegistry);
120
        
121
        $request = new ServerRequest([], [], '/'.urlencode('🍕'), 'GET');
122
        $response = new HtmlResponse('');
123
        $response = $defaultRouter($request, $response);
124
        $this->assertInstanceOf(JsonResponse::class, $response);
125
    }
126
    
127
    public function testUnknownRouteWith404Handler()
128
    {
129
        $container = new Picotainer([
130
        ]);
131
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
132
        $defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry);
133
        $errorsController = HttpErrorsController::createDefault();
134
        $defaultRouter->setHttp404Handler($errorsController);
135
136
        $request = new ServerRequest([], [], '/foo', 'GET');
137
        $response = new HtmlResponse('');
138
        $response = $defaultRouter($request, $response);
139
        /* @var $response HtmlResponse */
140
141
        // Now, let's retry without a $out parameter and let's check we get an exception
142
        $response = $defaultRouter($request, $response);
143
        $this->assertEquals(404, $response->getStatusCode());
144
    }
145
146
    public function testRootUrlError()
147
    {
148
        $container = new Picotainer([
149
        ]);
150
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
151
        $defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry, null, null, SplashUtils::MODE_STRICT, true, '/baseUrl/');
152
153
        $request = new ServerRequest([], [], '/foo', 'GET');
154
        $response = new HtmlResponse('');
155
        $this->expectException(SplashException::class);
156
        $response = $defaultRouter($request, $response);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
157
    }
158
159 View Code Duplication
    public function testMissingCompulsoryParameter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        $container = new Picotainer([
162
            'controller' => function () {
163
                return new TestController2();
164
            },
165
        ]);
166
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
167
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
168
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']);
169
        $defaultRouter = new SplashDefaultRouter($container, [
170
            $controllerRegistry,
171
        ], $parameterFetcherRegistry);
172
173
        // We need an ID parameter
174
        $request = new ServerRequest([], [], '/foo/var/bar', 'GET');
175
        $response = new HtmlResponse('');
176
        $this->expectException(SplashMissingParameterException::class);
177
        $response = $defaultRouter($request, $response);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
178
    }
179
180 View Code Duplication
    public function testMissingCompulsoryParameterWithHandler()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
    {
182
        $container = new Picotainer([
183
            'controller' => function () {
184
                return new TestController2();
185
            },
186
        ]);
187
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
188
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
189
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']);
190
        $defaultRouter = new SplashDefaultRouter($container, [
191
            $controllerRegistry,
192
        ], $parameterFetcherRegistry);
193
194
        $errorsController = HttpErrorsController::createDefault();
195
        $defaultRouter->setHttp400Handler($errorsController);
196
197
        // We need an ID parameter
198
        $request = new ServerRequest([], [], '/foo/var/bar', 'GET');
199
        $response = new HtmlResponse('');
200
        $response = $defaultRouter($request, $response);
201
        $this->assertEquals(400, $response->getStatusCode());
202
    }
203
204 View Code Duplication
    public function testExceptionWithHandler()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
    {
206
        $container = new Picotainer([
207
            'controller' => function () {
208
                return new TestController2();
209
            },
210
        ]);
211
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
212
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
213
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']);
214
        $defaultRouter = new SplashDefaultRouter($container, [
215
            $controllerRegistry,
216
        ], $parameterFetcherRegistry);
217
218
        $errorsController = HttpErrorsController::createDefault();
219
        $defaultRouter->setHttp500Handler($errorsController);
220
221
        // We need an ID parameter
222
        $request = new ServerRequest([], [], '/controller/triggerException', 'GET');
223
        $response = new HtmlResponse('');
224
        $response = $defaultRouter($request, $response);
225
        $this->assertEquals(500, $response->getStatusCode());
226
    }
227
228
    public function testPurgeUrlCache()
229
    {
230
        $cache = $this->prophesize(CacheItemPoolInterface::class);
231
        $cache->deleteItem('splashUrlNodes')->shouldBeCalled();
232
233
        $container = new Picotainer([]);
234
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
235
        $defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry, $cache->reveal());
236
        $defaultRouter->purgeUrlsCache();
237
    }
238
239 View Code Duplication
    public function testFilters()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
240
    {
241
        $container = new Picotainer([
242
            'controller' => function () {
243
                return new TestFilteredController();
244
            },
245
        ]);
246
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
247
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
248
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']);
249
        $defaultRouter = new SplashDefaultRouter($container, [
250
            $controllerRegistry,
251
        ], $parameterFetcherRegistry);
252
253
        $request = new ServerRequest([], [], '/foo', 'GET');
254
        $response = new HtmlResponse('');
255
        $response = $defaultRouter($request, $response);
256
        $this->assertEquals('42bar', (string) $response->getBody());
257
    }
258
259
    public function testExpirationTag()
260
    {
261
        $container = new Picotainer([
262
            'controller' => function () {
263
                return new TestController2();
264
            },
265
        ]);
266
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
267
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
268
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']);
269
        $defaultRouter = new SplashDefaultRouter($container, [
270
            $controllerRegistry,
271
        ], $parameterFetcherRegistry, new ArrayCachePool());
272
273
        $request = new ServerRequest([], [], '/foo/var/bar', 'GET', 'php://input',
274
            [],
275
            [],
276
            ['id' => 42]
277
        );
278
        $response = new HtmlResponse('');
279
        $response = $defaultRouter($request, $response);
280
        $this->assertInstanceOf(JsonResponse::class, $response);
281
282
        // Now, let's make another request (this time, we should go through the cache with unchanged etag)
283
        $response2 = $defaultRouter($request, $response);
284
        $this->assertInstanceOf(JsonResponse::class, $response2);
285
    }
286
287 View Code Duplication
    public function testExtendedController()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
288
    {
289
        $container = new Picotainer([
290
            'controller' => function () {
291
                return new TestExtendedController2();
292
            },
293
        ]);
294
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
295
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
296
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']);
297
        $defaultRouter = new SplashDefaultRouter($container, [
298
            $controllerRegistry,
299
        ], $parameterFetcherRegistry, new ArrayCachePool());
300
301
        $request = new ServerRequest([], [], '/url/42/foo/52', 'GET', 'php://input',
302
            [],
303
            [],
304
            []
305
        );
306
        $response = new HtmlResponse('');
307
        $response = $defaultRouter($request, $response);
308
        $this->assertInstanceOf(JsonResponse::class, $response);
309
    }
310
311 View Code Duplication
    public function testBadParam()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
312
    {
313
        $container = new Picotainer([
314
            'controller' => function () {
315
                return new TestBadParamController();
316
            },
317
        ]);
318
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
319
        $controllerAnalyzer = new ControllerAnalyzer($container, $parameterFetcherRegistry, new AnnotationReader());
320
        $controllerRegistry = new ControllerRegistry($controllerAnalyzer, ['controller']);
321
        $defaultRouter = new SplashDefaultRouter($container, [
322
            $controllerRegistry,
323
        ], $parameterFetcherRegistry, new ArrayCachePool());
324
325
        $request = new ServerRequest([], [], '/notexistparam/42', 'GET', 'php://input',
326
            [],
327
            [],
328
            []
329
        );
330
        $response = new HtmlResponse('');
331
        $this->expectException(\InvalidArgumentException::class);
332
        $response = $defaultRouter($request, $response);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
333
334
    }
335
}
336