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