|
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\Extension\Twig; |
|
|
13 |
|
|
|
14 |
use Assetic\Factory\AssetFactory; |
|
|
15 |
use Assetic\Extension\Twig\AsseticExtension; |
|
|
16 |
|
|
|
17 |
class AsseticExtensionTest extends \PHPUnit_Framework_TestCase |
|
|
18 |
{ |
|
|
19 |
private $am; |
|
|
20 |
private $fm; |
|
|
21 |
private $factory; |
|
|
22 |
private $twig; |
|
|
23 |
|
|
|
24 |
protected function setUp() |
|
|
25 |
{ |
|
|
26 |
if (!class_exists('Twig_Environment')) { |
|
|
27 |
$this->markTestSkipped('Twig is not installed.'); |
|
|
28 |
} |
|
|
29 |
|
|
|
30 |
$this->am = $this->getMock('Assetic\\AssetManager'); |
|
|
31 |
$this->fm = $this->getMock('Assetic\\FilterManager'); |
|
|
32 |
|
|
|
33 |
$this->factory = new AssetFactory(__DIR__.'/templates'); |
|
|
34 |
$this->factory->setAssetManager($this->am); |
|
|
35 |
$this->factory->setFilterManager($this->fm); |
|
|
36 |
|
|
|
37 |
$this->twig = new \Twig_Environment(); |
|
|
38 |
$this->twig->setLoader(new \Twig_Loader_Filesystem(__DIR__.'/templates')); |
|
|
39 |
$this->twig->addExtension(new AsseticExtension($this->factory)); |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
public function testReference() |
|
|
43 |
{ |
|
|
44 |
$asset = $this->getMock('Assetic\\Asset\\AssetInterface'); |
|
|
45 |
$this->am->expects($this->any()) |
|
|
46 |
->method('get') |
|
|
47 |
->with('foo') |
|
|
48 |
->will($this->returnValue($asset)); |
|
|
49 |
|
|
|
50 |
$xml = $this->renderXml('reference.twig'); |
|
|
51 |
$this->assertEquals(1, count($xml->asset)); |
|
|
52 |
$this->assertStringStartsWith('css/', (string) $xml->asset['url']); |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
public function testGlob() |
|
|
56 |
{ |
|
|
57 |
$xml = $this->renderXml('glob.twig'); |
|
|
58 |
$this->assertEquals(1, count($xml->asset)); |
|
|
59 |
$this->assertStringStartsWith('css/', (string) $xml->asset['url']); |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
public function testAbsolutePath() |
|
|
63 |
{ |
|
|
64 |
$xml = $this->renderXml('absolute_path.twig'); |
|
|
65 |
$this->assertEquals(1, count($xml->asset)); |
|
|
66 |
$this->assertStringStartsWith('css/', (string) $xml->asset['url']); |
|
|
67 |
} |
|
|
68 |
|
|
|
69 |
public function testFilters() |
|
|
70 |
{ |
|
|
71 |
$filter = $this->getMock('Assetic\\Filter\\FilterInterface'); |
|
|
72 |
|
|
|
73 |
$this->fm->expects($this->at(0)) |
|
|
74 |
->method('get') |
|
|
75 |
->with('foo') |
|
|
76 |
->will($this->returnValue($filter)); |
|
|
77 |
$this->fm->expects($this->at(1)) |
|
|
78 |
->method('get') |
|
|
79 |
->with('bar') |
|
|
80 |
->will($this->returnValue($filter)); |
|
|
81 |
|
|
|
82 |
$xml = $this->renderXml('filters.twig'); |
|
|
83 |
$this->assertEquals(1, count($xml->asset)); |
|
|
84 |
$this->assertStringStartsWith('css/', (string) $xml->asset['url']); |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
public function testOptionalFilter() |
|
|
88 |
{ |
|
|
89 |
$filter = $this->getMock('Assetic\\Filter\\FilterInterface'); |
|
|
90 |
|
|
|
91 |
$this->fm->expects($this->once()) |
|
|
92 |
->method('get') |
|
|
93 |
->with('foo') |
|
|
94 |
->will($this->returnValue($filter)); |
|
|
95 |
|
|
|
96 |
$xml = $this->renderXml('optional_filter.twig'); |
|
|
97 |
$this->assertEquals(1, count($xml->asset)); |
|
|
98 |
$this->assertStringStartsWith('css/', (string) $xml->asset['url']); |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
public function testOutputPattern() |
|
|
102 |
{ |
|
|
103 |
$xml = $this->renderXml('output_pattern.twig'); |
|
|
104 |
$this->assertEquals(1, count($xml->asset)); |
|
|
105 |
$this->assertStringStartsWith('css/packed/', (string) $xml->asset['url']); |
|
|
106 |
$this->assertStringEndsWith('.css', (string) $xml->asset['url']); |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
public function testOutput() |
|
|
110 |
{ |
|
|
111 |
$xml = $this->renderXml('output_url.twig'); |
|
|
112 |
$this->assertEquals(1, count($xml->asset)); |
|
|
113 |
$this->assertEquals('explicit_url.css', (string) $xml->asset['url']); |
|
|
114 |
} |
|
|
115 |
|
|
|
116 |
public function testMixture() |
|
|
117 |
{ |
|
|
118 |
$asset = $this->getMock('Assetic\\Asset\\AssetInterface'); |
|
|
119 |
$this->am->expects($this->any()) |
|
|
120 |
->method('get') |
|
|
121 |
->with('foo') |
|
|
122 |
->will($this->returnValue($asset)); |
|
|
123 |
|
|
|
124 |
$xml = $this->renderXml('mixture.twig'); |
|
|
125 |
$this->assertEquals(1, count($xml->asset)); |
|
|
126 |
$this->assertEquals('packed/mixture', (string) $xml->asset['url']); |
|
|
127 |
} |
|
|
128 |
|
|
|
129 |
public function testDebug() |
|
|
130 |
{ |
|
|
131 |
$filter = $this->getMock('Assetic\\Filter\\FilterInterface'); |
|
|
132 |
|
|
|
133 |
$this->fm->expects($this->once()) |
|
|
134 |
->method('get') |
|
|
135 |
->with('bar') |
|
|
136 |
->will($this->returnValue($filter)); |
|
|
137 |
|
|
|
138 |
$xml = $this->renderXml('debug.twig'); |
|
|
139 |
$this->assertEquals(2, count($xml->asset)); |
|
|
140 |
$this->assertStringStartsWith('css/packed_', (string) $xml->asset[0]['url']); |
|
|
141 |
$this->assertStringEndsWith('.css', (string) $xml->asset[0]['url']); |
|
|
142 |
} |
|
|
143 |
|
|
|
144 |
public function testCombine() |
|
|
145 |
{ |
|
|
146 |
$filter = $this->getMock('Assetic\\Filter\\FilterInterface'); |
|
|
147 |
|
|
|
148 |
$this->fm->expects($this->once()) |
|
|
149 |
->method('get') |
|
|
150 |
->with('bar') |
|
|
151 |
->will($this->returnValue($filter)); |
|
|
152 |
|
|
|
153 |
$xml = $this->renderXml('combine.twig'); |
|
|
154 |
$this->assertEquals(1, count($xml->asset)); |
|
|
155 |
$this->assertEquals('css/packed.css', (string) $xml->asset[0]['url']); |
|
|
156 |
} |
|
|
157 |
|
|
|
158 |
public function testImage() |
|
|
159 |
{ |
|
|
160 |
$xml = $this->renderXml('image.twig'); |
|
|
161 |
$this->assertEquals(1, count($xml->image)); |
|
|
162 |
$this->assertStringEndsWith('.png', (string) $xml->image[0]['url']); |
|
|
163 |
} |
|
|
164 |
|
|
|
165 |
public function testFilterFunction() |
|
|
166 |
{ |
|
|
167 |
$filter = $this->getMock('Assetic\\Filter\\FilterInterface'); |
|
|
168 |
|
|
|
169 |
$this->fm->expects($this->once()) |
|
|
170 |
->method('get') |
|
|
171 |
->with('some_filter') |
|
|
172 |
->will($this->returnValue($filter)); |
|
|
173 |
|
|
|
174 |
$this->twig->addExtension(new AsseticExtension($this->factory, array( |
|
|
175 |
'some_func' => array( |
|
|
176 |
'filter' => 'some_filter', |
|
|
177 |
'options' => array('output' => 'css/*.css'), |
|
|
178 |
), |
|
|
179 |
))); |
|
|
180 |
|
|
|
181 |
$xml = $this->renderXml('function.twig'); |
|
|
182 |
$this->assertEquals(1, count($xml->asset)); |
|
|
183 |
$this->assertStringEndsWith('.css', (string) $xml->asset[0]['url']); |
|
|
184 |
} |
|
|
185 |
|
|
|
186 |
private function renderXml($name, $context = array()) |
|
|
187 |
{ |
|
|
188 |
return new \SimpleXMLElement($this->twig->loadTemplate($name)->render($context)); |
|
|
189 |
} |
|
|
190 |
} |