1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the DS Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Dan Smith <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Tests\Ds\Router; |
11
|
|
|
|
12
|
|
|
use Ds\Router\Interfaces\AdaptorInterface; |
13
|
|
|
use Ds\Router\Interfaces\RouteCollectionInterface; |
14
|
|
|
use Ds\Router\Interfaces\RouterInterface; |
15
|
|
|
use Ds\Router\RouteCollection; |
16
|
|
|
use Ds\Router\Router; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class RouterTest |
20
|
|
|
* @package Tests\Ds\Router |
21
|
|
|
*/ |
22
|
|
|
class RouterTest extends \PHPUnit_Framework_TestCase |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|AdaptorInterface |
27
|
|
|
*/ |
28
|
|
|
public $adaptor; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|RouteCollectionInterface |
32
|
|
|
*/ |
33
|
|
|
public $collection; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var RouterInterface |
37
|
|
|
*/ |
38
|
|
|
public $router; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
|
|
public function setUp() |
45
|
|
|
{ |
46
|
|
|
$this->adaptor = $this->getMockBuilder(AdaptorInterface::class)->getMock(); |
47
|
|
|
$this->collection = $this->getMockBuilder(RouteCollectionInterface::class)->getMock(); |
48
|
|
|
|
49
|
|
|
$this->router = new Router( |
50
|
|
|
$this->adaptor, |
51
|
|
|
$this->collection |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function testGetRouterResponseFromPath() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$method = 'GET'; |
61
|
|
|
$path = '/mypath'; |
62
|
|
|
$actual = 'my-handler'; |
63
|
|
|
|
64
|
|
|
$this->adaptor->expects($this->once()) |
|
|
|
|
65
|
|
|
->method('match') |
66
|
|
|
->with($this->collection, $method, $path) |
67
|
|
|
->willReturn($actual); |
68
|
|
|
|
69
|
|
|
$expected = $this->router->getRouterResponseFromPath($method, $path); |
70
|
|
|
$this->assertEquals($expected, $actual); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function testGetRouterResponse() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$method = 'GET'; |
79
|
|
|
$path = '/path'; |
80
|
|
|
$actual = 'my-handler'; |
81
|
|
|
|
82
|
|
|
$this->adaptor->expects($this->once()) |
|
|
|
|
83
|
|
|
->method('match') |
84
|
|
|
->with($this->collection, $method, $path) |
85
|
|
|
->willReturn($actual); |
86
|
|
|
|
87
|
|
|
$expected = $this->router->getRouterResponseFromPath($method,$path); |
88
|
|
|
$this->assertEquals($expected, $actual); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
*/ |
95
|
|
|
public function testWithAdaptor() |
96
|
|
|
{ |
97
|
|
|
$newAdaptor = $this->getMockBuilder(AdaptorInterface::class)->getMock(); |
98
|
|
|
$newRouter = $this->router->withAdaptor($newAdaptor); |
99
|
|
|
$this->assertNotSame($this->adaptor, $newRouter->getAdaptor()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* |
104
|
|
|
*/ |
105
|
|
|
public function testGetAdaptor() |
106
|
|
|
{ |
107
|
|
|
$expected = $this->adaptor; |
108
|
|
|
$actual = $this->router->getAdaptor(); |
109
|
|
|
$this->assertEquals($expected, $actual); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* |
114
|
|
|
*/ |
115
|
|
|
public function testWithCollection() |
116
|
|
|
{ |
117
|
|
|
$routes = $this->getMockBuilder(RouteCollectionInterface::class)->getMock(); |
118
|
|
|
$this->router = $this->router->withCollection($routes); |
119
|
|
|
|
120
|
|
|
$this->assertSame( |
121
|
|
|
$routes, |
122
|
|
|
Helpers\Reflection::getProperty(Router::class, 'collection', $this->router) |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* |
128
|
|
|
*/ |
129
|
|
|
public function testGetCollection() |
130
|
|
|
{ |
131
|
|
|
$expected = $this->collection; |
132
|
|
|
$actual = $this->router->getCollection(); |
133
|
|
|
$this->assertEquals($expected, $actual); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* |
138
|
|
|
*/ |
139
|
|
|
public function testGetCollectionCached() |
140
|
|
|
{ |
141
|
|
|
$this->adaptor->expects($this->once()) |
|
|
|
|
142
|
|
|
->method('isCached') |
143
|
|
|
->willReturn(true); |
144
|
|
|
|
145
|
|
|
$this->adaptor->expects($this->once()) |
146
|
|
|
->method('getCachedRoutes') |
147
|
|
|
->willReturn($this->collection); |
148
|
|
|
|
149
|
|
|
$collection = $this->router->getCollection(); |
150
|
|
|
$this->assertSame($this->collection, $collection); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* |
156
|
|
|
*/ |
157
|
|
|
public function testMergeCollection() |
158
|
|
|
{ |
159
|
|
|
$collection = new RouteCollection(); |
160
|
|
|
|
161
|
|
|
$this->collection->expects($this->once()) |
|
|
|
|
162
|
|
|
->method('mergeCollection') |
163
|
|
|
->with($this->equalTo($collection)) |
164
|
|
|
->willReturn($collection); |
165
|
|
|
|
166
|
|
|
$newCollection = $this->router->mergeCollection($collection); |
167
|
|
|
$this->assertNotSame($this->collection, $newCollection); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* |
172
|
|
|
*/ |
173
|
|
|
public function testIsCached() |
174
|
|
|
{ |
175
|
|
|
$this->adaptor->expects($this->once()) |
|
|
|
|
176
|
|
|
->method('isCached') |
177
|
|
|
->willReturn(true); |
178
|
|
|
$this->router->isCached(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
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.