|
1
|
|
|
<?php |
|
2
|
|
|
namespace AcelayaTest\Expressive\Router; |
|
3
|
|
|
|
|
4
|
|
|
use Acelaya\Expressive\Router\SlimRouter; |
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
7
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
8
|
|
|
use Slim\Router; |
|
9
|
|
|
use Zend\Diactoros\ServerRequest; |
|
10
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
11
|
|
|
use Zend\Expressive\Router\Route; |
|
12
|
|
|
|
|
13
|
|
|
class SlimRouterTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var SlimRouter |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $router; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Router |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $slimRouter; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ObjectProphecy |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $middleware; |
|
27
|
|
|
|
|
28
|
|
|
public function setUp() |
|
29
|
|
|
{ |
|
30
|
|
|
new SlimRouter(); |
|
31
|
|
|
$this->slimRouter = new Router(); |
|
32
|
|
|
$this->router = new SlimRouter($this->slimRouter); |
|
33
|
|
|
$this->middleware = $this->prophesize(MiddlewareInterface::class); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testAddRoute() |
|
37
|
|
|
{ |
|
38
|
|
|
$middleware = $this->middleware->reveal(); |
|
39
|
|
|
$this->router->addRoute(new Route('/foo(/:bar)', $middleware, ['GET', 'POST'], 'home')); |
|
40
|
|
|
$this->injectRoutes(); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertCount(1, $this->slimRouter->getNamedRoutes()); |
|
43
|
|
|
|
|
44
|
|
|
/** @var \Slim\Route $route */ |
|
45
|
|
|
$route = $this->slimRouter->getMatchedRoutes('GET', '/foo/baz')[0]; |
|
46
|
|
|
$this->assertEquals('/foo(/:bar)', $route->getPattern()); |
|
47
|
|
|
$this->assertEquals('home', $route->getName()); |
|
48
|
|
|
$this->assertEquals($middleware, $route->getParams()['middleware']); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testAddRouteWithOptions() |
|
52
|
|
|
{ |
|
53
|
|
|
$middleware = $this->middleware->reveal(); |
|
54
|
|
|
$route = new Route('/foo/:bar', $middleware, ['GET', 'POST'], 'home'); |
|
55
|
|
|
$route->setOptions([ |
|
56
|
|
|
'conditions' => [ |
|
57
|
|
|
'bar' => 'es|en' |
|
58
|
|
|
], |
|
59
|
|
|
'defaults' => [ |
|
60
|
|
|
'bar' => 'en' |
|
61
|
|
|
] |
|
62
|
|
|
]); |
|
63
|
|
|
$this->router->addRoute($route); |
|
64
|
|
|
$this->injectRoutes(); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertCount(1, $this->slimRouter->getMatchedRoutes('GET', '/foo/es')); |
|
|
|
|
|
|
67
|
|
|
$this->assertCount(0, $this->slimRouter->getMatchedRoutes('GET', '/foo/baz', true)); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testAddRouteWithAnyMethod() |
|
71
|
|
|
{ |
|
72
|
|
|
$middleware = $this->middleware->reveal(); |
|
73
|
|
|
$route = new Route('/foo/bar', $middleware, Route::HTTP_METHOD_ANY, 'home'); |
|
74
|
|
|
$this->router->addRoute($route); |
|
75
|
|
|
$this->injectRoutes(); |
|
76
|
|
|
|
|
77
|
|
|
$this->assertCount(1, $this->slimRouter->getMatchedRoutes('GET', '/foo/bar')); |
|
|
|
|
|
|
78
|
|
|
$this->assertCount(1, $this->slimRouter->getMatchedRoutes('POST', '/foo/bar')); |
|
|
|
|
|
|
79
|
|
|
$this->assertCount(1, $this->slimRouter->getMatchedRoutes('PUT', '/foo/bar')); |
|
|
|
|
|
|
80
|
|
|
$this->assertCount(1, $this->slimRouter->getMatchedRoutes('DELETE', '/foo/bar')); |
|
|
|
|
|
|
81
|
|
|
$this->assertCount(1, $this->slimRouter->getMatchedRoutes('PATCH', '/foo/bar')); |
|
|
|
|
|
|
82
|
|
|
$this->assertCount(1, $this->slimRouter->getMatchedRoutes('OPTIONS', '/foo/bar')); |
|
|
|
|
|
|
83
|
|
|
$this->assertCount(1, $this->slimRouter->getMatchedRoutes('HEAD', '/foo/bar')); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testDummyCallable() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->assertNull($this->router->dummyCallable()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testGenerateUrl() |
|
92
|
|
|
{ |
|
93
|
|
|
$middleware = $this->middleware->reveal(); |
|
94
|
|
|
$route = new Route('/foo(/:bar)', $middleware, ['GET', 'POST'], 'home'); |
|
95
|
|
|
$this->router->addRoute($route); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertEquals('/foo', $this->router->generateUri('home')); |
|
98
|
|
|
$this->assertEquals('/foo/baz', $this->router->generateUri('home', ['bar' => 'baz'])); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @expectedException \Zend\Expressive\Router\Exception\RuntimeException |
|
103
|
|
|
*/ |
|
104
|
|
|
public function testGenerateUrlWithInvalidName() |
|
105
|
|
|
{ |
|
106
|
|
|
$middleware = $this->middleware->reveal(); |
|
107
|
|
|
$route = new Route('/foo(/:bar)', $middleware, ['GET', 'POST'], 'home'); |
|
108
|
|
|
$this->router->addRoute($route); |
|
109
|
|
|
$this->router->generateUri('invalidName'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testMatchInvalidRequest() |
|
113
|
|
|
{ |
|
114
|
|
|
$result = $this->router->match(ServerRequestFactory::fromGlobals()); |
|
115
|
|
|
$this->assertTrue($result->isFailure()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testMatchValidRequest() |
|
119
|
|
|
{ |
|
120
|
|
|
$middleware = $this->middleware->reveal(); |
|
121
|
|
|
$this->router->addRoute(new Route('/foo(/:bar)', $middleware, ['GET', 'POST'], 'home')); |
|
122
|
|
|
$this->injectRoutes(); |
|
123
|
|
|
|
|
124
|
|
|
$this->assertCount(1, $this->slimRouter->getNamedRoutes()); |
|
125
|
|
|
$result = $this->router->match(new ServerRequest([], [], '/foo/bar', 'POST')); |
|
126
|
|
|
$this->assertTrue($result->isSuccess()); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
private function injectRoutes() |
|
130
|
|
|
{ |
|
131
|
|
|
$ref = new \ReflectionObject($this->router); |
|
132
|
|
|
$method = $ref->getMethod('injectRoutes'); |
|
133
|
|
|
$method->setAccessible(true); |
|
134
|
|
|
$method->invoke($this->router); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: