AsseticExtensionTest.php 7.4KB

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