|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony framework. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* This source file is subject to the MIT license that is bundled |
|
|
9 |
* with this source code in the file LICENSE. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Bundle\AsseticBundle\Tests\DependencyInjection; |
|
|
13 |
|
|
|
14 |
use Symfony\Bundle\AsseticBundle\DependencyInjection\AsseticExtension; |
|
|
15 |
use Symfony\Bundle\AsseticBundle\DependencyInjection\Compiler\CheckYuiFilterPass; |
|
|
16 |
use Symfony\Bundle\AsseticBundle\DependencyInjection\Compiler\CheckClosureFilterPass; |
|
|
17 |
use Symfony\Component\DependencyInjection\Container; |
|
|
18 |
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
|
19 |
use Symfony\Component\DependencyInjection\Definition; |
|
|
20 |
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
|
|
21 |
use Symfony\Component\DependencyInjection\Scope; |
|
|
22 |
use Symfony\Component\HttpFoundation\Request; |
|
|
23 |
|
|
|
24 |
class AsseticExtensionTest extends \PHPUnit_Framework_TestCase |
|
|
25 |
{ |
|
|
26 |
private $kernel; |
|
|
27 |
private $container; |
|
|
28 |
|
|
|
29 |
static public function assertSaneContainer(Container $container, $message = '') |
|
|
30 |
{ |
|
|
31 |
$errors = array(); |
|
|
32 |
foreach ($container->getServiceIds() as $id) { |
|
|
33 |
try { |
|
|
34 |
$container->get($id); |
|
|
35 |
} catch (\Exception $e) { |
|
|
36 |
$errors[$id] = $e->getMessage(); |
|
|
37 |
} |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
self::assertEquals(array(), $errors, $message); |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
protected function setUp() |
|
|
44 |
{ |
|
|
45 |
if (!class_exists('Assetic\\AssetManager')) { |
|
|
46 |
$this->markTestSkipped('Assetic is not available.'); |
|
|
47 |
} |
|
|
48 |
|
|
|
49 |
$this->kernel = $this->getMock('Symfony\\Component\\HttpKernel\\KernelInterface'); |
|
|
50 |
|
|
|
51 |
$this->container = new ContainerBuilder(); |
|
|
52 |
$this->container->addScope(new Scope('request')); |
|
|
53 |
$this->container->register('request', 'Symfony\\Component\\HttpFoundation\\Request')->setScope('request'); |
|
|
54 |
$this->container->register('templating.helper.assets', $this->getMockClass('Symfony\\Component\\Templating\\Helper\\AssetsHelper')); |
|
|
55 |
$this->container->register('templating.helper.router', $this->getMockClass('Symfony\\Bundle\\FrameworkBundle\\Templating\\Helper\\RouterHelper')) |
|
|
56 |
->addArgument(new Definition($this->getMockClass('Symfony\\Component\\Routing\\RouterInterface'))); |
|
|
57 |
$this->container->register('twig', 'Twig_Environment'); |
|
|
58 |
$this->container->setParameter('kernel.bundles', array()); |
|
|
59 |
$this->container->setParameter('kernel.cache_dir', __DIR__); |
|
|
60 |
$this->container->setParameter('kernel.debug', false); |
|
|
61 |
$this->container->setParameter('kernel.root_dir', __DIR__); |
|
|
62 |
$this->container->setParameter('kernel.charset', 'UTF-8'); |
|
|
63 |
$this->container->set('kernel', $this->kernel); |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
/** |
|
|
67 |
* @dataProvider getDebugModes |
|
|
68 |
*/ |
|
|
69 |
public function testDefaultConfig($debug) |
|
|
70 |
{ |
|
|
71 |
$this->container->setParameter('kernel.debug', $debug); |
|
|
72 |
|
|
|
73 |
$extension = new AsseticExtension(); |
|
|
74 |
$extension->load(array(array()), $this->container); |
|
|
75 |
|
|
|
76 |
$this->assertFalse($this->container->has('assetic.filter.yui_css'), '->load() does not load the yui_css filter when a yui value is not provided'); |
|
|
77 |
$this->assertFalse($this->container->has('assetic.filter.yui_js'), '->load() does not load the yui_js filter when a yui value is not provided'); |
|
|
78 |
|
|
|
79 |
$this->assertSaneContainer($this->getDumpedContainer()); |
|
|
80 |
} |
|
|
81 |
|
|
|
82 |
public function getDebugModes() |
|
|
83 |
{ |
|
|
84 |
return array( |
|
|
85 |
array(true), |
|
|
86 |
array(false), |
|
|
87 |
); |
|
|
88 |
} |
|
|
89 |
|
|
|
90 |
/** |
|
|
91 |
* @dataProvider getFilterNames |
|
|
92 |
*/ |
|
|
93 |
public function testFilterConfigs($name, $config = array()) |
|
|
94 |
{ |
|
|
95 |
$extension = new AsseticExtension(); |
|
|
96 |
$extension->load(array(array('filters' => array($name => $config))), $this->container); |
|
|
97 |
|
|
|
98 |
$this->assertSaneContainer($this->getDumpedContainer()); |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
public function getFilterNames() |
|
|
102 |
{ |
|
|
103 |
return array( |
|
|
104 |
array('closure', array('jar' => '/path/to/closure.jar')), |
|
|
105 |
array('coffee'), |
|
|
106 |
array('compass'), |
|
|
107 |
array('cssembed', array('jar' => '/path/to/cssembed.jar')), |
|
|
108 |
array('cssimport'), |
|
|
109 |
array('cssrewrite'), |
|
|
110 |
array('jpegoptim'), |
|
|
111 |
array('jpegtran'), |
|
|
112 |
array('less'), |
|
|
113 |
array('lessphp'), |
|
|
114 |
array('optipng'), |
|
|
115 |
array('packager'), |
|
|
116 |
array('pngout'), |
|
|
117 |
array('sass'), |
|
|
118 |
array('scss'), |
|
|
119 |
array('sprockets', array('include_dirs' => array('foo'))), |
|
|
120 |
array('stylus'), |
|
|
121 |
array('yui_css', array('jar' => '/path/to/yuicompressor.jar')), |
|
|
122 |
array('yui_js', array('jar' => '/path/to/yuicompressor.jar')), |
|
|
123 |
); |
|
|
124 |
} |
|
|
125 |
|
|
|
126 |
/** |
|
|
127 |
* @dataProvider getUseControllerKeys |
|
|
128 |
*/ |
|
|
129 |
public function testUseController($bool, $includes, $omits) |
|
|
130 |
{ |
|
|
131 |
$extension = new AsseticExtension(); |
|
|
132 |
$extension->load(array(array('use_controller' => $bool)), $this->container); |
|
|
133 |
|
|
|
134 |
foreach ($includes as $id) { |
|
|
135 |
$this->assertTrue($this->container->has($id), '"'.$id.'" is registered when use_controller is '.$bool); |
|
|
136 |
} |
|
|
137 |
|
|
|
138 |
foreach ($omits as $id) { |
|
|
139 |
$this->assertFalse($this->container->has($id), '"'.$id.'" is not registered when use_controller is '.$bool); |
|
|
140 |
} |
|
|
141 |
|
|
|
142 |
$this->assertSaneContainer($this->getDumpedContainer()); |
|
|
143 |
} |
|
|
144 |
|
|
|
145 |
public function getUseControllerKeys() |
|
|
146 |
{ |
|
|
147 |
return array( |
|
|
148 |
array(true, array('assetic.routing_loader', 'assetic.controller'), array('assetic.asset_writer_cache_warmer', 'assetic.asset_writer')), |
|
|
149 |
array(false, array('assetic.asset_writer_cache_warmer', 'assetic.asset_writer'), array('assetic.routing_loader', 'assetic.controller')), |
|
|
150 |
); |
|
|
151 |
} |
|
|
152 |
|
|
|
153 |
/** |
|
|
154 |
* @dataProvider getClosureJarAndExpected |
|
|
155 |
*/ |
|
|
156 |
public function testClosureCompilerPass($jar, $expected) |
|
|
157 |
{ |
|
|
158 |
$this->container->addCompilerPass(new CheckClosureFilterPass()); |
|
|
159 |
|
|
|
160 |
$extension = new AsseticExtension(); |
|
|
161 |
$extension->load(array(array( |
|
|
162 |
'filters' => array( |
|
|
163 |
'closure' => array('jar' => $jar), |
|
|
164 |
), |
|
|
165 |
)), $this->container); |
|
|
166 |
|
|
|
167 |
$container = $this->getDumpedContainer(); |
|
|
168 |
$this->assertSaneContainer($container); |
|
|
169 |
|
|
|
170 |
$this->assertTrue($this->container->getDefinition($expected)->hasTag('assetic.filter')); |
|
|
171 |
$this->assertNotEmpty($container->getParameter('assetic.filter.closure.java')); |
|
|
172 |
} |
|
|
173 |
|
|
|
174 |
public function getClosureJarAndExpected() |
|
|
175 |
{ |
|
|
176 |
return array( |
|
|
177 |
array(null, 'assetic.filter.closure.api'), |
|
|
178 |
array('/path/to/closure.jar', 'assetic.filter.closure.jar'), |
|
|
179 |
); |
|
|
180 |
} |
|
|
181 |
|
|
|
182 |
public function testInvalidYuiConfig() |
|
|
183 |
{ |
|
|
184 |
$this->setExpectedException('RuntimeException', 'assetic.filters.yui_js'); |
|
|
185 |
|
|
|
186 |
$this->container->addCompilerPass(new CheckYuiFilterPass()); |
|
|
187 |
|
|
|
188 |
$extension = new AsseticExtension(); |
|
|
189 |
$extension->load(array(array( |
|
|
190 |
'filters' => array( |
|
|
191 |
'yui_js' => array(), |
|
|
192 |
), |
|
|
193 |
)), $this->container); |
|
|
194 |
|
|
|
195 |
$this->getDumpedContainer(); |
|
|
196 |
} |
|
|
197 |
|
|
|
198 |
private function getDumpedContainer() |
|
|
199 |
{ |
|
|
200 |
static $i = 0; |
|
|
201 |
$class = 'AsseticExtensionTestContainer'.$i++; |
|
|
202 |
|
|
|
203 |
$this->container->compile(); |
|
|
204 |
|
|
|
205 |
$dumper = new PhpDumper($this->container); |
|
|
206 |
eval('?>'.$dumper->dump(array('class' => $class))); |
|
|
207 |
|
|
|
208 |
$container = new $class(); |
|
|
209 |
$container->enterScope('request'); |
|
|
210 |
$container->set('kernel', $this->kernel); |
|
|
211 |
|
|
|
212 |
return $container; |
|
|
213 |
} |
|
|
214 |
} |