1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Router; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
6
|
|
|
use Ds\Router\Interfaces\AdaptorInterface; |
7
|
|
|
use Ds\Router\Interfaces\RouteCollectionInterface; |
8
|
|
|
use Ds\Router\Interfaces\RouterInterface; |
9
|
|
|
use Ds\Router\RouteCollection; |
10
|
|
|
use Ds\Router\Router; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class RouterTest |
14
|
|
|
* @package Tests\Router |
15
|
|
|
*/ |
16
|
|
|
class RouterTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|AdaptorInterface |
21
|
|
|
*/ |
22
|
|
|
public $adaptor; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|RouteCollectionInterface |
26
|
|
|
*/ |
27
|
|
|
public $collection; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var RouterInterface |
31
|
|
|
*/ |
32
|
|
|
public $router; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
*/ |
38
|
|
|
public function setUp() |
39
|
|
|
{ |
40
|
|
|
$this->adaptor = $this->getMockBuilder(AdaptorInterface::class)->getMock(); |
41
|
|
|
$this->collection = $this->getMockBuilder(RouteCollectionInterface::class)->getMock(); |
42
|
|
|
|
43
|
|
|
$this->router = new Router( |
44
|
|
|
$this->adaptor, |
45
|
|
|
$this->collection |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* |
51
|
|
|
*/ |
52
|
|
|
public function testGetRouterResponseFromPath() |
53
|
|
|
{ |
54
|
|
|
$method = 'GET'; |
55
|
|
|
$path = '/mypath'; |
56
|
|
|
$actual = 'my-handler'; |
57
|
|
|
|
58
|
|
|
$this->adaptor->expects($this->once()) |
|
|
|
|
59
|
|
|
->method('match') |
60
|
|
|
->with($this->collection, $method, $path) |
61
|
|
|
->willReturn($actual); |
62
|
|
|
|
63
|
|
|
$expected = $this->router->getRouterResponseFromPath($method, $path); |
64
|
|
|
$this->assertEquals($expected, $actual); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* |
69
|
|
|
*/ |
70
|
|
|
public function testGetRouterResponse() |
71
|
|
|
{ |
72
|
|
|
$request = $this->getMockBuilder(ServerRequestInterface::class)->getMock(); |
73
|
|
|
|
74
|
|
|
$method = 'GET'; |
75
|
|
|
$path = '/path'; |
76
|
|
|
$actual = 'my-handler'; |
77
|
|
|
|
78
|
|
|
$request->expects($this->at(0)) |
79
|
|
|
->method('getMethod') |
80
|
|
|
->willReturn($method); |
81
|
|
|
|
82
|
|
|
$request->expects($this->at(1)) |
83
|
|
|
->method('getRequestTarget') |
84
|
|
|
->willReturn($path); |
85
|
|
|
|
86
|
|
|
$this->adaptor->expects($this->once()) |
|
|
|
|
87
|
|
|
->method('match') |
88
|
|
|
->with($this->collection, $method, $path) |
89
|
|
|
->willReturn($actual); |
90
|
|
|
|
91
|
|
|
$expected = $this->router->getRouterResponse($request); |
92
|
|
|
$this->assertEquals($expected, $actual); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
*/ |
99
|
|
|
public function testWithAdaptor() |
100
|
|
|
{ |
101
|
|
|
$newAdaptor = $this->getMockBuilder(AdaptorInterface::class)->getMock(); |
102
|
|
|
$newRouter = $this->router->withAdaptor($newAdaptor); |
103
|
|
|
$this->assertNotSame($this->adaptor, $newRouter->getAdaptor()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* |
108
|
|
|
*/ |
109
|
|
|
public function testGetAdaptor() |
110
|
|
|
{ |
111
|
|
|
$expected = $this->adaptor; |
112
|
|
|
$actual = $this->router->getAdaptor(); |
113
|
|
|
$this->assertEquals($expected, $actual); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* |
118
|
|
|
*/ |
119
|
|
|
public function testWithCollection() |
120
|
|
|
{ |
121
|
|
|
$routes = $this->getMockBuilder(RouteCollectionInterface::class)->getMock(); |
122
|
|
|
$this->router = $this->router->withCollection($routes); |
123
|
|
|
|
124
|
|
|
$this->assertSame( |
125
|
|
|
$routes, |
126
|
|
|
Helpers\Reflection::getProperty(Router::class, 'collection', $this->router) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
*/ |
133
|
|
|
public function testGetCollection() |
134
|
|
|
{ |
135
|
|
|
$expected = $this->collection; |
136
|
|
|
$actual = $this->router->getCollection(); |
137
|
|
|
$this->assertEquals($expected, $actual); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* |
142
|
|
|
*/ |
143
|
|
|
public function testGetCollectionCached() |
144
|
|
|
{ |
145
|
|
|
$this->adaptor->expects($this->once()) |
|
|
|
|
146
|
|
|
->method('isCached') |
147
|
|
|
->willReturn(true); |
148
|
|
|
|
149
|
|
|
$this->adaptor->expects($this->once()) |
150
|
|
|
->method('getCachedRoutes') |
151
|
|
|
->willReturn($this->collection); |
152
|
|
|
|
153
|
|
|
$collection = $this->router->getCollection(); |
154
|
|
|
$this->assertSame($this->collection, $collection); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* |
160
|
|
|
*/ |
161
|
|
|
public function testMergeCollection() |
162
|
|
|
{ |
163
|
|
|
$collection = new RouteCollection(); |
164
|
|
|
|
165
|
|
|
$this->collection->expects($this->once()) |
|
|
|
|
166
|
|
|
->method('mergeCollection') |
167
|
|
|
->with($this->equalTo($collection)) |
168
|
|
|
->willReturn($collection); |
169
|
|
|
|
170
|
|
|
$newCollection = $this->router->mergeCollection($collection); |
171
|
|
|
$this->assertNotSame($this->collection, $newCollection); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* |
176
|
|
|
*/ |
177
|
|
|
public function testIsCached() |
178
|
|
|
{ |
179
|
|
|
$this->adaptor->expects($this->once()) |
|
|
|
|
180
|
|
|
->method('isCached') |
181
|
|
|
->willReturn(true); |
182
|
|
|
$this->router->isCached(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: