|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Assetic package, an OpenSky project. |
|
|
5 |
* |
|
|
6 |
* (c) 2010-2011 OpenSky Project Inc |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Assetic\Test\Asset; |
|
|
13 |
|
|
|
14 |
use Assetic\Asset\StringAsset; |
|
|
15 |
use Assetic\Asset\AssetCollection; |
|
|
16 |
use Assetic\Filter\CallablesFilter; |
|
|
17 |
|
|
|
18 |
class AssetCollectionTest extends \PHPUnit_Framework_TestCase |
|
|
19 |
{ |
|
|
20 |
public function testInterface() |
|
|
21 |
{ |
|
|
22 |
$coll = new AssetCollection(); |
|
|
23 |
$this->assertInstanceOf('Assetic\\Asset\\AssetInterface', $coll, 'AssetCollection implements AssetInterface'); |
|
|
24 |
} |
|
|
25 |
|
|
|
26 |
public function testLoadFilter() |
|
|
27 |
{ |
|
|
28 |
$filter = $this->getMock('Assetic\\Filter\\FilterInterface'); |
|
|
29 |
$filter->expects($this->once())->method('filterLoad'); |
|
|
30 |
|
|
|
31 |
$coll = new AssetCollection(array(new StringAsset('')), array($filter)); |
|
|
32 |
$coll->load(); |
|
|
33 |
} |
|
|
34 |
|
|
|
35 |
public function testDumpFilter() |
|
|
36 |
{ |
|
|
37 |
$filter = $this->getMock('Assetic\\Filter\\FilterInterface'); |
|
|
38 |
$filter->expects($this->once())->method('filterDump'); |
|
|
39 |
|
|
|
40 |
$coll = new AssetCollection(array(new StringAsset('')), array($filter)); |
|
|
41 |
$coll->dump(); |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
public function testNestedCollectionLoad() |
|
|
45 |
{ |
|
|
46 |
$content = 'foobar'; |
|
|
47 |
|
|
|
48 |
$count = 0; |
|
|
49 |
$matches = array(); |
|
|
50 |
$filter = new CallablesFilter(function($asset) use ($content, & $matches, & $count) |
|
|
51 |
{ |
|
|
52 |
++$count; |
|
|
53 |
if ($content == $asset->getContent()) { |
|
|
54 |
$matches[] = $asset; |
|
|
55 |
} |
|
|
56 |
}); |
|
|
57 |
|
|
|
58 |
$innerColl = new AssetCollection(array(new StringAsset($content))); |
|
|
59 |
$outerColl = new AssetCollection(array($innerColl), array($filter)); |
|
|
60 |
$outerColl->load(); |
|
|
61 |
|
|
|
62 |
$this->assertEquals(1, count($matches), '->load() applies filters to leaves'); |
|
|
63 |
$this->assertEquals(1, $count, '->load() applies filters to leaves only'); |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
public function testMixedIteration() |
|
|
67 |
{ |
|
|
68 |
$asset = new StringAsset('asset'); |
|
|
69 |
$nestedAsset = new StringAsset('nested'); |
|
|
70 |
$innerColl = new AssetCollection(array($nestedAsset)); |
|
|
71 |
|
|
|
72 |
$contents = array(); |
|
|
73 |
$filter = new CallablesFilter(function($asset) use(& $contents) |
|
|
74 |
{ |
|
|
75 |
$contents[] = $asset->getContent(); |
|
|
76 |
}); |
|
|
77 |
|
|
|
78 |
$coll = new AssetCollection(array($asset, $innerColl), array($filter)); |
|
|
79 |
$coll->load(); |
|
|
80 |
|
|
|
81 |
$this->assertEquals(array('asset', 'nested'), $contents, '->load() iterates over multiple levels'); |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
public function testLoadDedupBySourceUrl() |
|
|
85 |
{ |
|
|
86 |
$asset1 = new StringAsset('asset', array(), '/some/dir', 'foo.bar'); |
|
|
87 |
$asset2 = new StringAsset('asset', array(), '/some/dir', 'foo.bar'); |
|
|
88 |
|
|
|
89 |
$coll = new AssetCollection(array($asset1, $asset2)); |
|
|
90 |
$coll->load(); |
|
|
91 |
|
|
|
92 |
$this->assertEquals('asset', $coll->getContent(), '->load() detects duplicate assets based on source URL'); |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
public function testLoadDedupByStrictEquality() |
|
|
96 |
{ |
|
|
97 |
$asset = new StringAsset('foo'); |
|
|
98 |
|
|
|
99 |
$coll = new AssetCollection(array($asset, $asset)); |
|
|
100 |
$coll->load(); |
|
|
101 |
|
|
|
102 |
$this->assertEquals('foo', $coll->getContent(), '->load() detects duplicate assets based on strict equality'); |
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
public function testDumpDedupBySourceUrl() |
|
|
106 |
{ |
|
|
107 |
$asset1 = new StringAsset('asset', array(), '/some/dir', 'foo.bar'); |
|
|
108 |
$asset2 = new StringAsset('asset', array(), '/some/dir', 'foo.bar'); |
|
|
109 |
|
|
|
110 |
$coll = new AssetCollection(array($asset1, $asset2)); |
|
|
111 |
$coll->load(); |
|
|
112 |
|
|
|
113 |
$this->assertEquals('asset', $coll->dump(), '->dump() detects duplicate assets based on source URL'); |
|
|
114 |
} |
|
|
115 |
|
|
|
116 |
public function testDumpDedupByStrictEquality() |
|
|
117 |
{ |
|
|
118 |
$asset = new StringAsset('foo'); |
|
|
119 |
|
|
|
120 |
$coll = new AssetCollection(array($asset, $asset)); |
|
|
121 |
$coll->load(); |
|
|
122 |
|
|
|
123 |
$this->assertEquals('foo', $coll->dump(), '->dump() detects duplicate assets based on strict equality'); |
|
|
124 |
} |
|
|
125 |
|
|
|
126 |
public function testIterationFilters() |
|
|
127 |
{ |
|
|
128 |
$count = 0; |
|
|
129 |
$filter = new CallablesFilter(function() use(&$count) { ++$count; }); |
|
|
130 |
|
|
|
131 |
$coll = new AssetCollection(); |
|
|
132 |
$coll->add(new StringAsset('')); |
|
|
133 |
$coll->ensureFilter($filter); |
|
|
134 |
|
|
|
135 |
foreach ($coll as $asset) { |
|
|
136 |
$asset->dump(); |
|
|
137 |
} |
|
|
138 |
|
|
|
139 |
$this->assertEquals(1, $count, 'collection filters are called when child assets are iterated over'); |
|
|
140 |
} |
|
|
141 |
|
|
|
142 |
public function testSetContent() |
|
|
143 |
{ |
|
|
144 |
$coll = new AssetCollection(); |
|
|
145 |
$coll->setContent('asdf'); |
|
|
146 |
|
|
|
147 |
$this->assertEquals('asdf', $coll->getContent(), '->setContent() sets the content'); |
|
|
148 |
} |
|
|
149 |
|
|
|
150 |
/** |
|
|
151 |
* @dataProvider getTimestampsAndExpected |
|
|
152 |
*/ |
|
|
153 |
public function testGetLastModified($timestamps, $expected) |
|
|
154 |
{ |
|
|
155 |
$asset = $this->getMock('Assetic\\Asset\\AssetInterface'); |
|
|
156 |
|
|
|
157 |
for ($i = 0; $i < count($timestamps); $i++) { |
|
|
158 |
$asset->expects($this->at($i)) |
|
|
159 |
->method('getLastModified') |
|
|
160 |
->will($this->returnValue($timestamps[$i])); |
|
|
161 |
} |
|
|
162 |
|
|
|
163 |
$coll = new AssetCollection(array_fill(0, count($timestamps), $asset)); |
|
|
164 |
|
|
|
165 |
$this->assertEquals($expected, $coll->getLastModified(), '->getLastModifed() returns the highest last modified'); |
|
|
166 |
} |
|
|
167 |
|
|
|
168 |
public function getTimestampsAndExpected() |
|
|
169 |
{ |
|
|
170 |
return array( |
|
|
171 |
array(array(1, 2, 3), 3), |
|
|
172 |
array(array(5, 4, 3), 5), |
|
|
173 |
array(array(3, 8, 5), 8), |
|
|
174 |
array(array(3, 8, null), 8), |
|
|
175 |
); |
|
|
176 |
} |
|
|
177 |
|
|
|
178 |
public function testRecursiveIteration() |
|
|
179 |
{ |
|
|
180 |
$asset1 = $this->getMock('Assetic\\Asset\\AssetInterface'); |
|
|
181 |
$asset2 = $this->getMock('Assetic\\Asset\\AssetInterface'); |
|
|
182 |
$asset3 = $this->getMock('Assetic\\Asset\\AssetInterface'); |
|
|
183 |
$asset4 = $this->getMock('Assetic\\Asset\\AssetInterface'); |
|
|
184 |
|
|
|
185 |
$coll3 = new AssetCollection(array($asset1, $asset2)); |
|
|
186 |
$coll2 = new AssetCollection(array($asset3, $coll3)); |
|
|
187 |
$coll1 = new AssetCollection(array($asset4, $coll2)); |
|
|
188 |
|
|
|
189 |
$i = 0; |
|
|
190 |
foreach ($coll1 as $a) { |
|
|
191 |
$i++; |
|
|
192 |
} |
|
|
193 |
|
|
|
194 |
$this->assertEquals(4, $i, 'iteration with a recursive iterator is recursive'); |
|
|
195 |
} |
|
|
196 |
|
|
|
197 |
public function testRecursiveDeduplication() |
|
|
198 |
{ |
|
|
199 |
$asset = $this->getMock('Assetic\\Asset\\AssetInterface'); |
|
|
200 |
|
|
|
201 |
$coll3 = new AssetCollection(array($asset, $asset)); |
|
|
202 |
$coll2 = new AssetCollection(array($asset, $coll3)); |
|
|
203 |
$coll1 = new AssetCollection(array($asset, $coll2)); |
|
|
204 |
|
|
|
205 |
$i = 0; |
|
|
206 |
foreach ($coll1 as $a) { |
|
|
207 |
$i++; |
|
|
208 |
} |
|
|
209 |
|
|
|
210 |
$this->assertEquals(1, $i, 'deduplication is performed recursively'); |
|
|
211 |
} |
|
|
212 |
|
|
|
213 |
public function testIteration() |
|
|
214 |
{ |
|
|
215 |
$asset1 = new StringAsset('asset1', array(), '/some/dir', 'foo.css'); |
|
|
216 |
$asset2 = new StringAsset('asset2', array(), '/some/dir', 'foo.css'); |
|
|
217 |
$asset3 = new StringAsset('asset3', array(), '/some/dir', 'bar.css'); |
|
|
218 |
|
|
|
219 |
$coll = new AssetCollection(array($asset1, $asset2, $asset3)); |
|
|
220 |
|
|
|
221 |
$count = 0; |
|
|
222 |
foreach ($coll as $a) { |
|
|
223 |
++$count; |
|
|
224 |
} |
|
|
225 |
|
|
|
226 |
$this->assertEquals(2, $count, 'iterator filters duplicates based on url'); |
|
|
227 |
} |
|
|
228 |
|
|
|
229 |
public function testBasenameCollision() |
|
|
230 |
{ |
|
|
231 |
$asset1 = new StringAsset('asset1', array(), '/some/dir', 'foo/foo.css'); |
|
|
232 |
$asset2 = new StringAsset('asset2', array(), '/some/dir', 'bar/foo.css'); |
|
|
233 |
|
|
|
234 |
$coll = new AssetCollection(array($asset1, $asset2)); |
|
|
235 |
|
|
|
236 |
$urls = array(); |
|
|
237 |
foreach ($coll as $leaf) { |
|
|
238 |
$urls[] = $leaf->getTargetPath(); |
|
|
239 |
} |
|
|
240 |
|
|
|
241 |
$this->assertEquals(2, count(array_unique($urls)), 'iterator prevents basename collisions'); |
|
|
242 |
} |
|
|
243 |
|
|
|
244 |
public function testEmptyMtime() |
|
|
245 |
{ |
|
|
246 |
$coll = new AssetCollection(); |
|
|
247 |
$this->assertNull($coll->getLastModified(), '->getLastModified() returns null on empty collection'); |
|
|
248 |
} |
|
|
249 |
|
|
|
250 |
public function testLeafManipulation() |
|
|
251 |
{ |
|
|
252 |
$coll = new AssetCollection(array(new StringAsset('asdf'))); |
|
|
253 |
|
|
|
254 |
foreach ($coll as $leaf) { |
|
|
255 |
$leaf->setTargetPath('asdf'); |
|
|
256 |
} |
|
|
257 |
|
|
|
258 |
foreach ($coll as $leaf) { |
|
|
259 |
$this->assertEquals('asdf', $leaf->getTargetPath(), 'leaf changes persist between iterations'); |
|
|
260 |
} |
|
|
261 |
} |
|
|
262 |
} |