Completed
Push — 8.0 ( bf11e8...f611ca )
by David
02:23
created

SplashDefaultRouterTest::testRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

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