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\Loaders; |
11
|
|
|
|
12
|
|
|
use Ds\Router\Exceptions\RouterException; |
13
|
|
|
use Ds\Router\Interfaces\RouterInterface; |
14
|
|
|
use Ds\Router\Loaders\FileLoader; |
15
|
|
|
use Ds\Router\RouteCollection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FileLoaderTest |
19
|
|
|
* @package Tests\Ds\Router\Loaders |
20
|
|
|
*/ |
21
|
|
|
class FileLoaderTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|RouterInterface |
25
|
|
|
*/ |
26
|
|
|
public $router; |
27
|
|
|
/** |
28
|
|
|
* @var FileLoader |
29
|
|
|
*/ |
30
|
|
|
public $loader; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
public $vars; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $file; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* PHP Route File Loader setUp |
44
|
|
|
*/ |
45
|
|
|
public function setUp() |
46
|
|
|
{ |
47
|
|
|
$options = [ |
48
|
|
|
'vars' => [ |
49
|
|
|
'variable' => 'my-var' |
50
|
|
|
] |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
$this->file = __DIR__ . '/Files/FileLoaderRoutes.php'; |
54
|
|
|
$this->router = $this->getMockBuilder(RouterInterface::class)->getMock(); |
55
|
|
|
$this->loader = new FileLoader($this->router, $options); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function testLoadFileNoCacheNonInvalidFile() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$this->setExpectedException(RouterException::class); |
64
|
|
|
$this->router->expects($this->once()) |
|
|
|
|
65
|
|
|
->method('isCached') |
66
|
|
|
->willReturn(false); |
67
|
|
|
$this->loader->loadFile(__DIR__ . '/Files/Malformed.php'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function testLoadFileNoCacheNoFile() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$this->setExpectedException(RouterException::class); |
76
|
|
|
$this->router->expects($this->once()) |
|
|
|
|
77
|
|
|
->method('isCached') |
78
|
|
|
->willReturn(false); |
79
|
|
|
$this->loader->loadFile(__DIR__ . '/RouteFileNone.php'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function testLoadFileNoCacheWithVars() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$this->router->expects($this->once()) |
|
|
|
|
88
|
|
|
->method('isCached') |
89
|
|
|
->willReturn(false); |
90
|
|
|
|
91
|
|
|
$routes = $this->loader->loadFile($this->file); |
92
|
|
|
$route = $routes->current(); |
93
|
|
|
$handler = $route->getHandler(); |
94
|
|
|
|
95
|
|
|
$expected = 'my-var'; |
96
|
|
|
$actual = $handler(); |
97
|
|
|
$this->assertEquals($expected, $actual); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
public function testLoadFileWithCache() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$this->router->expects($this->once()) |
|
|
|
|
106
|
|
|
->method('isCached') |
107
|
|
|
->willReturn(true); |
108
|
|
|
$this->router->expects($this->once()) |
109
|
|
|
->method('getCollection') |
110
|
|
|
->willReturn(new RouteCollection()); |
111
|
|
|
|
112
|
|
|
$collection = $this->loader->loadFile($this->file); |
113
|
|
|
$this->assertInstanceOf(RouteCollection::class, $collection); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function testLoadFilesNoCacheNoFile() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$this->setExpectedException(RouterException::class); |
122
|
|
|
|
123
|
|
|
$this->router->expects($this->any()) |
|
|
|
|
124
|
|
|
->method('isCached') |
125
|
|
|
->willReturn(false); |
126
|
|
|
|
127
|
|
|
$this->router->expects($this->any()) |
128
|
|
|
->method('mergeCollection') |
129
|
|
|
->willReturn($this->router); |
130
|
|
|
|
131
|
|
|
$this->loader->loadFiles([$this->file, __DIR__ . '/RouteFileNone.php']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
public function testLoadFilesNoCache() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$this->router->expects($this->any()) |
|
|
|
|
140
|
|
|
->method('isCached') |
141
|
|
|
->willReturn(false); |
142
|
|
|
|
143
|
|
|
$this->router->expects($this->any()) |
144
|
|
|
->method('mergeCollection') |
145
|
|
|
->willReturn($this->router); |
146
|
|
|
|
147
|
|
|
$this->loader->loadFiles([ |
148
|
|
|
$this->file, |
149
|
|
|
__DIR__ . '/Files/FileLoaderRoutesAlt.php' |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* |
155
|
|
|
*/ |
156
|
|
|
public function testLoadFilesCache() |
157
|
|
|
{ |
158
|
|
|
$this->router->expects($this->any()) |
|
|
|
|
159
|
|
|
->method('isCached') |
160
|
|
|
->willReturn(true); |
161
|
|
|
$router = $this->loader->loadFiles([ |
162
|
|
|
$this->file, |
163
|
|
|
__DIR__ . '/Files/FileLoaderRoutesAlt.php' |
164
|
|
|
]); |
165
|
|
|
$this->assertSame($this->router, $router); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
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.