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