-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathPrioritizedPathsResolverTest.php
More file actions
256 lines (214 loc) · 7.58 KB
/
PrioritizedPathsResolverTest.php
File metadata and controls
256 lines (214 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
namespace AssetManagerTest\Service;
use AssetManager\Exception\InvalidArgumentException;
use AssetManager\Resolver\MimeResolverAwareInterface;
use AssetManager\Resolver\PrioritizedPathsResolver;
use AssetManager\Resolver\ResolverInterface;
use AssetManager\Service\MimeResolver;
use PHPUnit_Framework_TestCase;
class PrioritizedPathsResolverTest extends PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$resolver = new PrioritizedPathsResolver();
$this->assertEmpty($resolver->getPaths()->toArray());
$resolver->addPaths(array(__DIR__));
$this->assertEquals(array(__DIR__ . DIRECTORY_SEPARATOR), $resolver->getPaths()->toArray());
$this->assertTrue($resolver instanceof MimeResolverAwareInterface);
$this->assertTrue($resolver instanceof ResolverInterface);
}
public function testClearPaths()
{
$resolver = new PrioritizedPathsResolver();
$resolver->addPath('someDir');
$paths = $resolver->getPaths();
$this->assertEquals('someDir' . DIRECTORY_SEPARATOR, $paths->top());
$resolver->clearPaths();
$this->assertEquals(array(), $resolver->getPaths()->toArray());
}
public function testSetPaths()
{
$resolver = new PrioritizedPathsResolver();
$resolver->setPaths(array(
array(
'path' => 'dir3',
'priority' => 750,
),
array(
'path' => 'dir2',
'priority' => 1000,
),
array(
'path' => 'dir1',
'priority' => 500,
),
));
$this->assertTrue($resolver->getPaths()->hasPriority(1000));
$this->assertTrue($resolver->getPaths()->hasPriority(500));
$fetched = array();
foreach ($resolver->getPaths() as $path) {
$fetched[] = $path;
}
// order inverted because of how a stack is traversed
$this->assertSame(
array('dir2' . DIRECTORY_SEPARATOR, 'dir3' . DIRECTORY_SEPARATOR, 'dir1' . DIRECTORY_SEPARATOR),
$fetched
);
$this->setExpectedException(InvalidArgumentException::class);
$resolver->setPaths('invalid');
}
public function testAddPaths()
{
$resolver = new PrioritizedPathsResolver();
$resolver->setPaths(array(
array(
'path' => 'dir3',
'priority' => 750,
),
array(
'path' => 'dir2',
'priority' => 1000,
),
array(
'path' => 'dir1',
'priority' => 500,
),
));
$resolver->addPaths(array(
'dir4',
array(
'path' => 'dir5',
'priority' => -5,
)
));
$fetched = array();
foreach ($resolver->getPaths() as $path) {
$fetched[] = $path;
}
// order inverted because of how a stack is traversed
$this->assertSame(
array(
'dir2' . DIRECTORY_SEPARATOR,
'dir3' . DIRECTORY_SEPARATOR,
'dir1' . DIRECTORY_SEPARATOR,
'dir4' . DIRECTORY_SEPARATOR,
'dir5' . DIRECTORY_SEPARATOR,
),
$fetched
);
}
public function testAddPath()
{
$resolver = new PrioritizedPathsResolver();
$resolver->setPaths(array(
array(
'path' => 'dir3',
'priority' => 750,
),
array(
'path' => 'dir2',
'priority' => 1000,
),
array(
'path' => 'dir1',
'priority' => 500,
),
));
$resolver->addPath('dir4');
$resolver->addPath(array('path'=>'dir5', 'priority'=>-5));
$fetched = array();
foreach ($resolver->getPaths() as $path) {
$fetched[] = $path;
}
// order inverted because of how a stack is traversed
$this->assertSame(
array(
'dir2' . DIRECTORY_SEPARATOR,
'dir3' . DIRECTORY_SEPARATOR,
'dir1' . DIRECTORY_SEPARATOR,
'dir4' . DIRECTORY_SEPARATOR,
'dir5' . DIRECTORY_SEPARATOR,
),
$fetched
);
}
public function testSetPathsAllowsStringPaths()
{
$resolver = new PrioritizedPathsResolver();
$resolver->setPaths(array('dir1', 'dir2', 'dir3'));
$paths = $resolver->getPaths()->toArray();
$this->assertCount(3, $paths);
$this->assertContains('dir1' . DIRECTORY_SEPARATOR, $paths);
$this->assertContains('dir2' . DIRECTORY_SEPARATOR, $paths);
$this->assertContains('dir3' . DIRECTORY_SEPARATOR, $paths);
}
public function testWillValidateGivenPathArray()
{
$resolver = new PrioritizedPathsResolver();
$this->setExpectedException(InvalidArgumentException::class);
$resolver->addPath(array('invalid'));
}
public function testResolve()
{
$resolver = new PrioritizedPathsResolver;
$resolver->setMimeResolver(new MimeResolver);
$resolver->addPath(__DIR__);
$this->assertEquals(file_get_contents(__FILE__), $resolver->resolve(basename(__FILE__))->dump());
$this->assertNull($resolver->resolve('i-do-not-exist.php'));
}
public function testWillNotResolveDirectories()
{
$resolver = new PrioritizedPathsResolver();
$resolver->addPath(__DIR__ . '/..');
$this->assertNull($resolver->resolve(basename(__DIR__)));
}
public function testLfiProtection()
{
$resolver = new PrioritizedPathsResolver();
$resolver->setMimeResolver(new MimeResolver);
// should be on by default
$this->assertTrue($resolver->isLfiProtectionOn());
$resolver->addPath(__DIR__);
$this->assertNull($resolver->resolve(
'..' . DIRECTORY_SEPARATOR . basename(__DIR__) . DIRECTORY_SEPARATOR . basename(__FILE__)
));
$resolver->setLfiProtection(false);
$this->assertSame(
file_get_contents(__FILE__),
$resolver->resolve(
'..' . DIRECTORY_SEPARATOR . basename(__DIR__) . DIRECTORY_SEPARATOR . basename(__FILE__)
)->dump()
);
}
public function testWillRefuseInvalidPath()
{
$resolver = new PrioritizedPathsResolver();
$this->setExpectedException(InvalidArgumentException::class);
$resolver->addPath(null);
}
/**
* Test Collect returns valid list of assets
*
* @covers \AssetManager\Resolver\PrioritizedPathsResolver::collect
*/
public function testCollect()
{
$resolver = new PrioritizedPathsResolver();
$resolver->addPath(__DIR__);
$this->assertContains(basename(__FILE__), $resolver->collect());
$this->assertNotContains('i-do-not-exist.php', $resolver->collect());
}
/**
* Test Collect returns valid list of assets
*
* @covers \AssetManager\Resolver\PrioritizedPathsResolver::collect
*/
public function testCollectDirectory()
{
$resolver = new PrioritizedPathsResolver();
$resolver->addPath(realpath(__DIR__ . '/../'));
$dir = substr(__DIR__, strrpos(__DIR__, DIRECTORY_SEPARATOR, 0) + 1);
$this->assertContains($dir . DIRECTORY_SEPARATOR . basename(__FILE__), $resolver->collect());
$this->assertNotContains($dir . DIRECTORY_SEPARATOR . 'i-do-not-exist.php', $resolver->collect());
}
}