|
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\Factory; |
|
|
13 |
|
|
|
14 |
use Assetic\Factory\AssetFactory; |
|
|
15 |
|
|
|
16 |
class AssetFactoryTest extends \PHPUnit_Framework_TestCase |
|
|
17 |
{ |
|
|
18 |
private $am; |
|
|
19 |
private $fm; |
|
|
20 |
private $factory; |
|
|
21 |
|
|
|
22 |
protected function setUp() |
|
|
23 |
{ |
|
|
24 |
$this->am = $this->getMock('Assetic\\AssetManager'); |
|
|
25 |
$this->fm = $this->getMock('Assetic\\FilterManager'); |
|
|
26 |
|
|
|
27 |
$this->factory = new AssetFactory(__DIR__); |
|
|
28 |
$this->factory->setAssetManager($this->am); |
|
|
29 |
$this->factory->setFilterManager($this->fm); |
|
|
30 |
} |
|
|
31 |
|
|
|
32 |
public function testNoAssetManagerReference() |
|
|
33 |
{ |
|
|
34 |
$this->setExpectedException('LogicException', 'There is no asset manager.'); |
|
|
35 |
|
|
|
36 |
$factory = new AssetFactory('.'); |
|
|
37 |
$factory->createAsset(array('@foo')); |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
public function testNoAssetManagerNotReference() |
|
|
41 |
{ |
|
|
42 |
$factory = new AssetFactory('.'); |
|
|
43 |
$this->assertInstanceOf('Assetic\\Asset\\AssetInterface', $factory->createAsset(array('foo'))); |
|
|
44 |
} |
|
|
45 |
|
|
|
46 |
public function testNoFilterManager() |
|
|
47 |
{ |
|
|
48 |
$this->setExpectedException('LogicException', 'There is no filter manager.'); |
|
|
49 |
|
|
|
50 |
$factory = new AssetFactory('.'); |
|
|
51 |
$factory->createAsset(array('foo'), array('foo')); |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
public function testCreateAssetReference() |
|
|
55 |
{ |
|
|
56 |
$referenced = $this->getMock('Assetic\\Asset\\AssetInterface'); |
|
|
57 |
|
|
|
58 |
$this->am->expects($this->any()) |
|
|
59 |
->method('get') |
|
|
60 |
->with('jquery') |
|
|
61 |
->will($this->returnValue($referenced)); |
|
|
62 |
|
|
|
63 |
$assets = $this->factory->createAsset(array('@jquery')); |
|
|
64 |
$arr = iterator_to_array($assets); |
|
|
65 |
$this->assertInstanceOf('Assetic\\Asset\\AssetReference', $arr[0], '->createAsset() creates a reference'); |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
/** |
|
|
69 |
* @dataProvider getHttpUrls |
|
|
70 |
*/ |
|
|
71 |
public function testCreateHttpAsset($sourceUrl) |
|
|
72 |
{ |
|
|
73 |
$assets = $this->factory->createAsset(array($sourceUrl)); |
|
|
74 |
$arr = iterator_to_array($assets); |
|
|
75 |
$this->assertInstanceOf('Assetic\\Asset\\HttpAsset', $arr[0], '->createAsset() creates an HTTP asset'); |
|
|
76 |
} |
|
|
77 |
|
|
|
78 |
public function getHttpUrls() |
|
|
79 |
{ |
|
|
80 |
return array( |
|
|
81 |
array('http://example.com/foo.css'), |
|
|
82 |
array('https://example.com/foo.css'), |
|
|
83 |
array('//example.com/foo.css'), |
|
|
84 |
); |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
public function testCreateFileAsset() |
|
|
88 |
{ |
|
|
89 |
$assets = $this->factory->createAsset(array(basename(__FILE__))); |
|
|
90 |
$arr = iterator_to_array($assets); |
|
|
91 |
$this->assertInstanceOf('Assetic\\Asset\\FileAsset', $arr[0], '->createAsset() creates a file asset'); |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
public function testCreateGlobAsset() |
|
|
95 |
{ |
|
|
96 |
$assets = $this->factory->createAsset(array('*')); |
|
|
97 |
$arr = iterator_to_array($assets); |
|
|
98 |
$this->assertInstanceOf('Assetic\\Asset\\FileAsset', $arr[0], '->createAsset() uses a glob to create a file assets'); |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
public function testCreateAssetCollection() |
|
|
102 |
{ |
|
|
103 |
$asset = $this->factory->createAsset(array('*', basename(__FILE__))); |
|
|
104 |
$this->assertInstanceOf('Assetic\\Asset\\AssetCollection', $asset, '->createAsset() creates an asset collection'); |
|
|
105 |
} |
|
|
106 |
|
|
|
107 |
public function testFilter() |
|
|
108 |
{ |
|
|
109 |
$this->fm->expects($this->once()) |
|
|
110 |
->method('get') |
|
|
111 |
->with('foo') |
|
|
112 |
->will($this->returnValue($this->getMock('Assetic\\Filter\\FilterInterface'))); |
|
|
113 |
|
|
|
114 |
$asset = $this->factory->createAsset(array(), array('foo')); |
|
|
115 |
$this->assertEquals(1, count($asset->getFilters()), '->createAsset() adds filters'); |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
public function testInvalidFilter() |
|
|
119 |
{ |
|
|
120 |
$this->setExpectedException('InvalidArgumentException'); |
|
|
121 |
|
|
|
122 |
$this->fm->expects($this->once()) |
|
|
123 |
->method('get') |
|
|
124 |
->with('foo') |
|
|
125 |
->will($this->throwException(new \InvalidArgumentException())); |
|
|
126 |
|
|
|
127 |
$asset = $this->factory->createAsset(array(), array('foo')); |
|
|
128 |
} |
|
|
129 |
|
|
|
130 |
public function testOptionalInvalidFilter() |
|
|
131 |
{ |
|
|
132 |
$this->factory->setDebug(true); |
|
|
133 |
|
|
|
134 |
$asset = $this->factory->createAsset(array(), array('?foo')); |
|
|
135 |
|
|
|
136 |
$this->assertEquals(0, count($asset->getFilters()), '->createAsset() does not add an optional invalid filter'); |
|
|
137 |
} |
|
|
138 |
|
|
|
139 |
public function testIncludingOptionalFilter() |
|
|
140 |
{ |
|
|
141 |
$this->fm->expects($this->once()) |
|
|
142 |
->method('get') |
|
|
143 |
->with('foo') |
|
|
144 |
->will($this->returnValue($this->getMock('Assetic\\Filter\\FilterInterface'))); |
|
|
145 |
|
|
|
146 |
$this->factory->createAsset(array('foo.css'), array('?foo')); |
|
|
147 |
} |
|
|
148 |
|
|
|
149 |
public function testWorkers() |
|
|
150 |
{ |
|
|
151 |
$worker = $this->getMock('Assetic\\Factory\\Worker\\WorkerInterface'); |
|
|
152 |
|
|
|
153 |
// called once on the collection and once on each leaf |
|
|
154 |
$worker->expects($this->exactly(3)) |
|
|
155 |
->method('process') |
|
|
156 |
->with($this->isInstanceOf('Assetic\\Asset\\AssetInterface')); |
|
|
157 |
|
|
|
158 |
$this->factory->addWorker($worker); |
|
|
159 |
$this->factory->createAsset(array('foo.js', 'bar.js')); |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
public function testNestedFormula() |
|
|
163 |
{ |
|
|
164 |
$this->fm->expects($this->once()) |
|
|
165 |
->method('get') |
|
|
166 |
->with('foo') |
|
|
167 |
->will($this->returnValue($this->getMock('Assetic\\Filter\\FilterInterface'))); |
|
|
168 |
|
|
|
169 |
$inputs = array( |
|
|
170 |
'css/main.css', |
|
|
171 |
array( |
|
|
172 |
// nested formula |
|
|
173 |
array('css/more.sass'), |
|
|
174 |
array('foo'), |
|
|
175 |
), |
|
|
176 |
); |
|
|
177 |
|
|
|
178 |
$asset = $this->factory->createAsset($inputs, array(), array('output' => 'css/*.css')); |
|
|
179 |
|
|
|
180 |
$i = 0; |
|
|
181 |
foreach ($asset as $leaf) { |
|
|
182 |
$i++; |
|
|
183 |
} |
|
|
184 |
|
|
|
185 |
$this->assertEquals(2, $i); |
|
|
186 |
} |
|
|
187 |
} |