Completed
Push — fetcher_factories ( 10a56f...fd93ea )
by David
13:44
created

SplashDefaultRouterTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 232
Duplicated Lines 34.91 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 6
Bugs 0 Features 0
Metric Value
dl 81
loc 232
c 6
b 0
f 0
wmc 11
lcom 1
cbo 15
rs 9.1666

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
B testRoute() 0 46 1
A testUnknownRoute() 0 21 1
A testUnknownRouteWith404Handler() 0 18 1
A testRootUrlError() 0 12 1
A testMissingCompulsoryParameter() 19 19 1
A testMissingCompulsoryParameterWithHandler() 22 22 1
A testExceptionWithHandler() 22 22 1
A testPurgeUrlCache() 0 10 1
A testFilters() 18 18 1
B testExpirationTag() 0 26 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\TestController2;
12
use Mouf\Mvc\Splash\Fixtures\TestFilteredController;
13
use Mouf\Mvc\Splash\Services\ControllerRegistry;
14
use Mouf\Mvc\Splash\Services\ParameterFetcherRegistry;
15
use Mouf\Mvc\Splash\Services\SplashUtils;
16
use Mouf\Mvc\Splash\Utils\SplashException;
17
use Mouf\Picotainer\Picotainer;
18
use Psr\Cache\CacheItemPoolInterface;
19
use Zend\Diactoros\Response\HtmlResponse;
20
use Zend\Diactoros\Response\JsonResponse;
21
use Zend\Diactoros\Response\RedirectResponse;
22
use Zend\Diactoros\ServerRequest;
23
24
class SplashDefaultRouterTest extends \PHPUnit_Framework_TestCase
25
{
26
    protected function setUp()
27
    {
28
        $loader = require __DIR__.'../../../../../../vendor/autoload.php';
29
        AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
30
    }
31
32
    public function testRoute()
33
    {
34
        $container = new Picotainer([
35
            'controller' => function () {
36
                return new TestController2();
37
            },
38
        ]);
39
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
40
        $controllerRegistry = new ControllerRegistry($container, $parameterFetcherRegistry, new AnnotationReader(), ['controller']);
41
        $defaultRouter = new SplashDefaultRouter($container, [
42
            $controllerRegistry,
43
        ], $parameterFetcherRegistry);
44
45
        $request = new ServerRequest([], [], '/foo/var/bar', 'GET', 'php://input',
46
            [],
47
            [],
48
            ['id' => 42]
49
            );
50
        $response = new HtmlResponse('');
51
        $response = $defaultRouter($request, $response);
52
        $this->assertInstanceOf(JsonResponse::class, $response);
53
        /* @var $response JsonResponse */
54
        $decodedResponse = json_decode((string) $response->getBody(), true);
55
        $this->assertEquals(42, $decodedResponse['id']);
56
        $this->assertEquals('var', $decodedResponse['var']);
57
        $this->assertEquals(42, $decodedResponse['id2']);
58
        $this->assertEquals(42, $decodedResponse['opt']);
59
60
        // Now, let's test the redirect
61
        $request = new ServerRequest([], [], '/foo/var/bar/', 'GET', 'php://input',
62
            [],
63
            [],
64
            ['id' => 42]
65
        );
66
        $response = new HtmlResponse('');
67
        $response = $defaultRouter($request, $response);
68
        $this->assertInstanceOf(RedirectResponse::class, $response);
69
        $this->assertEquals('/foo/var/bar', $response->getHeader('Location')[0]);
70
71
        // Now, let's test the second kind of redirect
72
        $request = new ServerRequest([], [], '/controller', 'GET');
73
        $response = new HtmlResponse('');
74
        $response = $defaultRouter($request, $response);
75
        $this->assertInstanceOf(RedirectResponse::class, $response);
76
        $this->assertEquals('/controller/', $response->getHeader('Location')[0]);
77
    }
78
79
    public function testUnknownRoute()
80
    {
81
        $container = new Picotainer([
82
        ]);
83
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
84
        $defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry);
85
86
        $request = new ServerRequest([], [], '/foo', 'GET');
87
        $response = new HtmlResponse('');
88
        $response = $defaultRouter($request, $response, function () {
89
            return new HtmlResponse('Not found', 404);
90
        });
91
        $this->assertInstanceOf(HtmlResponse::class, $response);
92
        /* @var $response HtmlResponse */
93
        $this->assertEquals(404, $response->getStatusCode());
94
        $this->assertEquals('Not found', (string) $response->getBody());
95
96
        // Now, let's retry without a $out parameter and let's check we get an exception
97
        $this->expectException(PageNotFoundException::class);
98
        $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...
99
    }
100
101
    public function testUnknownRouteWith404Handler()
102
    {
103
        $container = new Picotainer([
104
        ]);
105
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
106
        $defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry);
107
        $errorsController = HttpErrorsController::createDefault();
108
        $defaultRouter->setHttp404Handler($errorsController);
109
110
        $request = new ServerRequest([], [], '/foo', 'GET');
111
        $response = new HtmlResponse('');
112
        $response = $defaultRouter($request, $response);
113
        /* @var $response HtmlResponse */
114
115
        // Now, let's retry without a $out parameter and let's check we get an exception
116
        $response = $defaultRouter($request, $response);
117
        $this->assertEquals(404, $response->getStatusCode());
118
    }
119
120
    public function testRootUrlError()
121
    {
122
        $container = new Picotainer([
123
        ]);
124
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
125
        $defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry, null, null, SplashUtils::MODE_STRICT, true, '/baseUrl/');
126
127
        $request = new ServerRequest([], [], '/foo', 'GET');
128
        $response = new HtmlResponse('');
129
        $this->expectException(SplashException::class);
130
        $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...
131
    }
132
133 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...
134
    {
135
        $container = new Picotainer([
136
            'controller' => function () {
137
                return new TestController2();
138
            },
139
        ]);
140
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
141
        $controllerRegistry = new ControllerRegistry($container, $parameterFetcherRegistry, new AnnotationReader(), ['controller']);
142
        $defaultRouter = new SplashDefaultRouter($container, [
143
            $controllerRegistry,
144
        ], $parameterFetcherRegistry);
145
146
        // We need an ID parameter
147
        $request = new ServerRequest([], [], '/foo/var/bar', 'GET');
148
        $response = new HtmlResponse('');
149
        $this->expectException(SplashMissingParameterException::class);
150
        $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...
151
    }
152
153 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...
154
    {
155
        $container = new Picotainer([
156
            'controller' => function () {
157
                return new TestController2();
158
            },
159
        ]);
160
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
161
        $controllerRegistry = new ControllerRegistry($container, $parameterFetcherRegistry, new AnnotationReader(), ['controller']);
162
        $defaultRouter = new SplashDefaultRouter($container, [
163
            $controllerRegistry,
164
        ], $parameterFetcherRegistry);
165
166
        $errorsController = HttpErrorsController::createDefault();
167
        $defaultRouter->setHttp400Handler($errorsController);
168
169
        // We need an ID parameter
170
        $request = new ServerRequest([], [], '/foo/var/bar', 'GET');
171
        $response = new HtmlResponse('');
172
        $response = $defaultRouter($request, $response);
173
        $this->assertEquals(400, $response->getStatusCode());
174
    }
175
176 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...
177
    {
178
        $container = new Picotainer([
179
            'controller' => function () {
180
                return new TestController2();
181
            },
182
        ]);
183
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
184
        $controllerRegistry = new ControllerRegistry($container, $parameterFetcherRegistry, new AnnotationReader(), ['controller']);
185
        $defaultRouter = new SplashDefaultRouter($container, [
186
            $controllerRegistry,
187
        ], $parameterFetcherRegistry);
188
189
        $errorsController = HttpErrorsController::createDefault();
190
        $defaultRouter->setHttp500Handler($errorsController);
191
192
        // We need an ID parameter
193
        $request = new ServerRequest([], [], '/controller/triggerException', 'GET');
194
        $response = new HtmlResponse('');
195
        $response = $defaultRouter($request, $response);
196
        $this->assertEquals(500, $response->getStatusCode());
197
    }
198
199
    public function testPurgeUrlCache()
200
    {
201
        $cache = $this->prophesize(CacheItemPoolInterface::class);
202
        $cache->deleteItem('splashUrlNodes')->shouldBeCalled();
203
204
        $container = new Picotainer([]);
205
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
206
        $defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry, $cache->reveal());
207
        $defaultRouter->purgeUrlsCache();
208
    }
209
210 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...
211
    {
212
        $container = new Picotainer([
213
            'controller' => function () {
214
                return new TestFilteredController();
215
            },
216
        ]);
217
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
218
        $controllerRegistry = new ControllerRegistry($container, $parameterFetcherRegistry, new AnnotationReader(), ['controller']);
219
        $defaultRouter = new SplashDefaultRouter($container, [
220
            $controllerRegistry,
221
        ], $parameterFetcherRegistry);
222
223
        $request = new ServerRequest([], [], '/foo', 'GET');
224
        $response = new HtmlResponse('');
225
        $response = $defaultRouter($request, $response);
226
        $this->assertEquals("42bar", (string) $response->getBody());
227
    }
228
229
    public function testExpirationTag()
230
    {
231
        $container = new Picotainer([
232
            'controller' => function () {
233
                return new TestController2();
234
            },
235
        ]);
236
        $parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry();
237
        $controllerRegistry = new ControllerRegistry($container, $parameterFetcherRegistry, new AnnotationReader(), ['controller']);
238
        $defaultRouter = new SplashDefaultRouter($container, [
239
            $controllerRegistry,
240
        ], $parameterFetcherRegistry, new ArrayCachePool());
241
242
        $request = new ServerRequest([], [], '/foo/var/bar', 'GET', 'php://input',
243
            [],
244
            [],
245
            ['id' => 42]
246
        );
247
        $response = new HtmlResponse('');
248
        $response = $defaultRouter($request, $response);
249
        $this->assertInstanceOf(JsonResponse::class, $response);
250
251
        // Now, let's make another request (this time, we should go through the cache with unchanged etag)
252
        $response2 = $defaultRouter($request, $response);
253
        $this->assertInstanceOf(JsonResponse::class, $response2);
254
    }
255
}
256