AsseticExtensionTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
  14. use Symfony\Component\DependencyInjection\Scope;
  15. use Symfony\Component\HttpFoundation\Request;
  16. class AsseticExtensionTest extends \PHPUnit_Framework_TestCase
  17. {
  18. private $kernel;
  19. private $container;
  20. protected function setUp()
  21. {
  22. if (!class_exists('Assetic\\AssetManager')) {
  23. $this->markTestSkipped('Assetic is not available.');
  24. }
  25. $this->kernel = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Kernel')
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $this->container = new ContainerBuilder();
  29. $this->container->addScope(new Scope('request'));
  30. $this->container->register('request', 'Symfony\\Component\\HttpFoundation\\Request')->setScope('request');
  31. $this->container->register('response', 'Symfony\\Component\\HttpFoundation\\Response')->setScope('prototype');
  32. $this->container->register('twig', 'Twig_Environment');
  33. $this->container->setParameter('kernel.debug', false);
  34. $this->container->setParameter('kernel.root_dir', __DIR__);
  35. $this->container->setParameter('kernel.cache_dir', __DIR__);
  36. $this->container->setParameter('kernel.bundles', array());
  37. }
  38. /**
  39. * @dataProvider getDebugModes
  40. */
  41. public function testDefaultConfig($debug)
  42. {
  43. $this->container->setParameter('kernel.debug', $debug);
  44. $extension = new AsseticExtension();
  45. $extension->load(array(array()), $this->container);
  46. $this->assertFalse($this->container->has('assetic.filter.yui_css'), '->load() does not load the yui_css filter when a yui value is not provided');
  47. $this->assertFalse($this->container->has('assetic.filter.yui_js'), '->load() does not load the yui_js filter when a yui value is not provided');
  48. // sanity check
  49. $container = $this->getDumpedContainer();
  50. foreach ($container->getServiceIds() as $id) {
  51. $container->get($id);
  52. }
  53. }
  54. public function getDebugModes()
  55. {
  56. return array(
  57. array(true),
  58. array(false),
  59. );
  60. }
  61. public function testYuiConfig()
  62. {
  63. $extension = new AsseticExtension();
  64. $extension->load(array(array('yui' => '/path/to/yuicompressor.jar')), $this->container);
  65. $this->assertTrue($this->container->has('assetic.filter.yui_css'), '->load() loads the yui_css filter when a yui value is provided');
  66. $this->assertTrue($this->container->has('assetic.filter.yui_js'), '->load() loads the yui_js filter when a yui value is provided');
  67. // sanity check
  68. $container = $this->getDumpedContainer();
  69. foreach ($container->getServiceIds() as $id) {
  70. $container->get($id);
  71. }
  72. }
  73. /**
  74. * @dataProvider getDocumentRootKeys
  75. */
  76. public function testDocumentRoot($key)
  77. {
  78. $extension = new AsseticExtension();
  79. $extension->load(array(array($key => '/path/to/web')), $this->container);
  80. $this->assertEquals('/path/to/web', $this->container->getParameter('assetic.document_root'), '"'.$key.'" sets document root');
  81. }
  82. public function getDocumentRootKeys()
  83. {
  84. return array(
  85. array('document_root'),
  86. array('document-root'),
  87. );
  88. }
  89. /**
  90. * @dataProvider getUseControllerKeys
  91. */
  92. public function testUseController($bool, $includes, $omits)
  93. {
  94. $extension = new AsseticExtension();
  95. $extension->load(array(array('use_controller' => $bool)), $this->container);
  96. foreach ($includes as $id) {
  97. $this->assertTrue($this->container->has($id), '"'.$id.'" is registered when use_controller is '.$bool);
  98. }
  99. foreach ($omits as $id) {
  100. $this->assertFalse($this->container->has($id), '"'.$id.'" is not registered when use_controller is '.$bool);
  101. }
  102. // sanity check
  103. $container = $this->getDumpedContainer();
  104. foreach ($container->getServiceIds() as $id) {
  105. $container->get($id);
  106. }
  107. }
  108. public function getUseControllerKeys()
  109. {
  110. return array(
  111. array(true, array('assetic.routing_loader', 'assetic.controller'), array('assetic.asset_writer_cache_warmer', 'assetic.asset_writer')),
  112. array(false, array('assetic.asset_writer_cache_warmer', 'assetic.asset_writer'), array('assetic.routing_loader', 'assetic.controller')),
  113. );
  114. }
  115. private function getDumpedContainer()
  116. {
  117. static $i = 0;
  118. $class = 'AsseticExtensionTestContainer'.$i++;
  119. $this->container->compile();
  120. $dumper = new PhpDumper($this->container);
  121. eval('?>'.$dumper->dump(array('class' => $class)));
  122. $container = new $class();
  123. $container->enterScope('request');
  124. $container->set('kernel', $this->kernel);
  125. return $container;
  126. }
  127. }