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\RouteCollectionInterface; |
14
|
|
|
use Ds\Router\Interfaces\RouterInterface; |
15
|
|
|
use Ds\Router\Loaders\YamlLoader; |
16
|
|
|
use Ds\Router\RouteCollection; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class YamlLoaderTest |
20
|
|
|
* @package Tests\Ds\Router\Loaders |
21
|
|
|
*/ |
22
|
|
|
class YamlLoaderTest extends \PHPUnit_Framework_TestCase |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|RouterInterface |
27
|
|
|
*/ |
28
|
|
|
public $router; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var YamlLoader |
32
|
|
|
*/ |
33
|
|
|
public $loader; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $file; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
public function setUp() |
44
|
|
|
{ |
45
|
|
|
$this->router = $this->getMockBuilder(RouterInterface::class)->getMock(); |
46
|
|
|
$this->loader = new YamlLoader($this->router); |
47
|
|
|
$this->file = __DIR__ . '/Files/FileLoaderRoutes.yml'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function testLoadFileNoCacheParse() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->setExpectedException(RouterException::class); |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->router->expects($this->once()) |
|
|
|
|
59
|
|
|
->method('isCached') |
60
|
|
|
->willReturn(false); |
61
|
|
|
$this->loader->loadFile(__DIR__ . '/Files/malformed.yml'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
public function testLoadFileNoCacheNoFile() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->setExpectedException(RouterException::class); |
70
|
|
|
|
71
|
|
|
$this->router->expects($this->once()) |
|
|
|
|
72
|
|
|
->method('isCached') |
73
|
|
|
->willReturn(false); |
74
|
|
|
|
75
|
|
|
$this->loader->loadFile(__DIR__ . '/RouteFileNone.yml'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function testLoadFileNoCache() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$this->router->expects($this->once()) |
|
|
|
|
84
|
|
|
->method('isCached') |
85
|
|
|
->willReturn(false); |
86
|
|
|
|
87
|
|
|
$collection = $this->loader->loadFile($this->file); |
88
|
|
|
$route = $collection->current(); |
89
|
|
|
|
90
|
|
|
$expected = 'myHandler::myMethod'; |
91
|
|
|
$actual = $route->getHandler(); |
92
|
|
|
|
93
|
|
|
$this->assertInstanceOf(RouteCollectionInterface::class, $collection); |
94
|
|
|
$this->assertEquals($expected, $actual); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function testLoadFileCache() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$this->router->expects($this->once()) |
|
|
|
|
103
|
|
|
->method('isCached') |
104
|
|
|
->willReturn(true); |
105
|
|
|
|
106
|
|
|
$this->router->expects($this->once()) |
107
|
|
|
->method('getCollection') |
108
|
|
|
->willReturn(new RouteCollection()); |
109
|
|
|
|
110
|
|
|
$collection = $this->loader->loadFile($this->file); |
111
|
|
|
|
112
|
|
|
$this->assertInstanceOf(RouteCollection::class, $collection); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function testLoadFilesNoCache() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$this->router->expects($this->any()) |
|
|
|
|
121
|
|
|
->method('isCached') |
122
|
|
|
->willReturn(false); |
123
|
|
|
|
124
|
|
|
$this->router->expects($this->any()) |
125
|
|
|
->method('mergeCollection') |
126
|
|
|
->willReturn($this->router); |
127
|
|
|
|
128
|
|
|
$this->loader->loadFiles([ |
129
|
|
|
$this->file, |
130
|
|
|
__DIR__ . '/Files/FileLoaderRoutesAlt.yml' |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
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.