-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathPathStackResolverTest.php
More file actions
171 lines (138 loc) · 5.21 KB
/
PathStackResolverTest.php
File metadata and controls
171 lines (138 loc) · 5.21 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
<?php
namespace AssetManagerTest\Service;
use ArrayObject;
use Assetic\Asset;
use AssetManager\Exception\InvalidArgumentException;
use AssetManager\Resolver\MimeResolverAwareInterface;
use AssetManager\Resolver\PathStackResolver;
use AssetManager\Resolver\ResolverInterface;
use AssetManager\Service\MimeResolver;
use PHPUnit_Framework_TestCase;
class PathStackResolverTest extends PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$resolver = new PathStackResolver();
$this->assertEmpty($resolver->getPaths()->toArray());
$resolver->addPaths(array(__DIR__));
$this->assertEquals(array(__DIR__ . DIRECTORY_SEPARATOR), $resolver->getPaths()->toArray());
$resolver->clearPaths();
$this->assertEquals(array(), $resolver->getPaths()->toArray());
$this->assertTrue($resolver instanceof MimeResolverAwareInterface);
$this->assertTrue($resolver instanceof ResolverInterface);
$mimeResolver = new MimeResolver;
$resolver->setMimeResolver($mimeResolver);
$this->assertEquals($mimeResolver, $resolver->getMimeResolver());
}
/**
* @expectedException \PHPUnit_Framework_Error
*/
public function testSetMimeResolverFailObject()
{
if (PHP_MAJOR_VERSION >= 7) {
$this->setExpectedException('\TypeError');
}
$resolver = new PathStackResolver();
$resolver->setMimeResolver(new \stdClass());
}
/**
* @expectedException \PHPUnit_Framework_Error
*/
public function testSetMimeResolverFailString()
{
if (PHP_MAJOR_VERSION >= 7) {
$this->setExpectedException('\TypeError');
}
$resolver = new PathStackResolver();
$resolver->setMimeResolver('invalid');
}
public function testSetPaths()
{
$resolver = new PathStackResolver();
$resolver->setPaths(array('dir2', 'dir1'));
// order inverted because of how a stack is traversed
$this->assertSame(
array('dir1' . DIRECTORY_SEPARATOR, 'dir2' . DIRECTORY_SEPARATOR),
$resolver->getPaths()->toArray()
);
$paths = new ArrayObject(array(
'dir4',
'dir3',
));
$resolver->setPaths($paths);
$this->assertSame(
array('dir3' . DIRECTORY_SEPARATOR, 'dir4' . DIRECTORY_SEPARATOR),
$resolver->getPaths()->toArray()
);
$this->setExpectedException(InvalidArgumentException::class);
$resolver->setPaths('invalid');
}
public function testResolve()
{
$resolver = new PathStackResolver();
$this->assertTrue($resolver instanceof PathStackResolver);
$mimeResolver = new MimeResolver;
$resolver->setMimeResolver($mimeResolver);
$resolver->addPath(__DIR__);
$fileAsset = new Asset\FileAsset(__FILE__);
$fileAsset->mimetype = $mimeResolver->getMimeType(__FILE__);
$this->assertEquals($fileAsset, $resolver->resolve(basename(__FILE__)));
$this->assertNull($resolver->resolve('i-do-not-exist.php'));
}
public function testWillNotResolveDirectories()
{
$resolver = new PathStackResolver();
$resolver->addPath(__DIR__ . '/..');
$this->assertNull($resolver->resolve(basename(__DIR__)));
}
public function testLfiProtection()
{
$mimeResolver = new MimeResolver;
$resolver = new PathStackResolver;
$resolver->setMimeResolver($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->assertEquals(
file_get_contents(__FILE__),
$resolver->resolve(
'..' . DIRECTORY_SEPARATOR . basename(__DIR__) . DIRECTORY_SEPARATOR . basename(__FILE__)
)->dump()
);
}
public function testWillRefuseInvalidPath()
{
$resolver = new PathStackResolver();
$this->setExpectedException(InvalidArgumentException::class);
$resolver->addPath(null);
}
/**
* Test Collect returns valid list of assets
*
* @covers \AssetManager\Resolver\PathStackResolver::collect
*/
public function testCollect()
{
$resolver = new PathStackResolver();
$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\PathStackResolver::collect
*/
public function testCollectDirectory()
{
$resolver = new PathStackResolver();
$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());
}
}