|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Mouf\Mvc\Splash\Routers; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Mouf\Mvc\Splash\Fixtures\TestController2; |
|
8
|
|
|
use Mouf\Mvc\Splash\Services\ControllerRegistry; |
|
9
|
|
|
use Mouf\Mvc\Splash\Services\ParameterFetcherRegistry; |
|
10
|
|
|
use Mouf\Mvc\Splash\Services\SplashUtils; |
|
11
|
|
|
use Mouf\Mvc\Splash\Utils\SplashException; |
|
12
|
|
|
use Mouf\Picotainer\Picotainer; |
|
13
|
|
|
use Zend\Diactoros\Response\HtmlResponse; |
|
14
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
15
|
|
|
use Zend\Diactoros\Response\RedirectResponse; |
|
16
|
|
|
use Zend\Diactoros\ServerRequest; |
|
17
|
|
|
|
|
18
|
|
|
class SplashDefaultRouterTest extends \PHPUnit_Framework_TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
public function testRoute() |
|
21
|
|
|
{ |
|
22
|
|
|
$container = new Picotainer([ |
|
23
|
|
|
"controller" => function() { |
|
24
|
|
|
return new TestController2(); |
|
25
|
|
|
} |
|
26
|
|
|
]); |
|
27
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
28
|
|
|
$controllerRegistry = new ControllerRegistry($container, $parameterFetcherRegistry, ['controller']); |
|
29
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [ |
|
30
|
|
|
$controllerRegistry |
|
31
|
|
|
], $parameterFetcherRegistry); |
|
32
|
|
|
|
|
33
|
|
|
$request = new ServerRequest([], [], '/foo/var/bar', 'GET', 'php://input', |
|
34
|
|
|
[], |
|
35
|
|
|
[], |
|
36
|
|
|
[ 'id' => 42 ] |
|
37
|
|
|
); |
|
38
|
|
|
$response = new HtmlResponse(''); |
|
39
|
|
|
$response = $defaultRouter($request, $response); |
|
40
|
|
|
$this->assertInstanceOf(JsonResponse::class, $response); |
|
41
|
|
|
/* @var $response JsonResponse */ |
|
42
|
|
|
$decodedResponse = json_decode((string) $response->getBody(), true); |
|
43
|
|
|
$this->assertEquals(42, $decodedResponse['id'] ); |
|
44
|
|
|
$this->assertEquals('var', $decodedResponse['var'] ); |
|
45
|
|
|
$this->assertEquals(42, $decodedResponse['id2'] ); |
|
46
|
|
|
$this->assertEquals(42, $decodedResponse['opt'] ); |
|
47
|
|
|
|
|
48
|
|
|
// Now, let's test the redirect |
|
49
|
|
|
$request = new ServerRequest([], [], '/foo/var/bar/', 'GET', 'php://input', |
|
50
|
|
|
[], |
|
51
|
|
|
[], |
|
52
|
|
|
[ 'id' => 42 ] |
|
53
|
|
|
); |
|
54
|
|
|
$response = new HtmlResponse(''); |
|
55
|
|
|
$response = $defaultRouter($request, $response); |
|
56
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
|
57
|
|
|
$this->assertEquals('/foo/var/bar', $response->getHeader('Location')[0]); |
|
58
|
|
|
|
|
59
|
|
|
// Now, let's test the second kind of redirect |
|
60
|
|
|
$request = new ServerRequest([], [], '/controller', 'GET'); |
|
61
|
|
|
$response = new HtmlResponse(''); |
|
62
|
|
|
$response = $defaultRouter($request, $response); |
|
63
|
|
|
$this->assertInstanceOf(RedirectResponse::class, $response); |
|
64
|
|
|
$this->assertEquals('/controller/', $response->getHeader('Location')[0]); |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testUnknownRoute() |
|
69
|
|
|
{ |
|
70
|
|
|
$container = new Picotainer([ |
|
71
|
|
|
]); |
|
72
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
73
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry); |
|
74
|
|
|
|
|
75
|
|
|
$request = new ServerRequest([], [], '/foo', 'GET'); |
|
76
|
|
|
$response = new HtmlResponse(''); |
|
77
|
|
|
$response = $defaultRouter($request, $response, function() { |
|
78
|
|
|
return new HtmlResponse("Not found", 404); |
|
79
|
|
|
}); |
|
80
|
|
|
$this->assertInstanceOf(HtmlResponse::class, $response); |
|
81
|
|
|
/* @var $response HtmlResponse */ |
|
82
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
|
83
|
|
|
$this->assertEquals("Not found", (string) $response->getBody() ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testRootUrlError() |
|
87
|
|
|
{ |
|
88
|
|
|
$container = new Picotainer([ |
|
89
|
|
|
]); |
|
90
|
|
|
$parameterFetcherRegistry = ParameterFetcherRegistry::buildDefaultControllerRegistry(); |
|
91
|
|
|
$defaultRouter = new SplashDefaultRouter($container, [], $parameterFetcherRegistry, null, null, SplashUtils::MODE_STRICT, true, '/baseUrl/'); |
|
92
|
|
|
|
|
93
|
|
|
$request = new ServerRequest([], [], '/foo', 'GET'); |
|
94
|
|
|
$response = new HtmlResponse(''); |
|
95
|
|
|
$this->expectException(SplashException::class); |
|
96
|
|
|
$response = $defaultRouter($request, $response); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.