TwigExtensionTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection;
  11. use Symfony\Bundle\TwigBundle\DependencyInjection\TwigExtension;
  12. use Symfony\Bundle\TwigBundle\Tests\TestCase;
  13. use Symfony\Component\Config\FileLocator;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  17. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  18. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  19. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  20. class TwigExtensionTest extends TestCase
  21. {
  22. /**
  23. * @dataProvider getFormats
  24. */
  25. public function testLoadEmptyConfiguration($format)
  26. {
  27. $container = $this->createContainer();
  28. $container->registerExtension(new TwigExtension());
  29. $container->loadFromExtension('twig', array());
  30. $this->compileContainer($container);
  31. $this->assertEquals('Twig_Environment', $container->getParameter('twig.class'), '->load() loads the twig.xml file');
  32. $this->assertFalse($container->getDefinition('templating.cache_warmer.templates_cache')->hasTag('kernel.cache_warmer'), '->load() does not enable cache warming by default');
  33. $this->assertContains('TwigBundle::form.html.twig', $container->getParameter('twig.form.resources'), '->load() includes default template for form resources');
  34. // Twig options
  35. $options = $container->getParameter('twig.options');
  36. $this->assertEquals(__DIR__.'/twig', $options['cache'], '->load() sets default value for cache option');
  37. $this->assertEquals('UTF-8', $options['charset'], '->load() sets default value for charset option');
  38. $this->assertEquals(false, $options['debug'], '->load() sets default value for debug option');
  39. }
  40. /**
  41. * @dataProvider getFormats
  42. */
  43. public function testLoadFullConfiguration($format)
  44. {
  45. $container = $this->createContainer();
  46. $container->registerExtension(new TwigExtension());
  47. $this->loadFromFile($container, 'full', $format);
  48. $this->compileContainer($container);
  49. $this->assertEquals('Twig_Environment', $container->getParameter('twig.class'), '->load() loads the twig.xml file');
  50. $this->assertTrue($container->getDefinition('templating.cache_warmer.templates_cache')->hasTag('kernel.cache_warmer'), '->load() enables cache warming');
  51. // Extensions
  52. foreach (array('twig.extension.debug', 'twig.extension.text') as $id) {
  53. $config = $container->getDefinition($id);
  54. $this->assertEquals(array('twig.extension'), array_keys($config->getTags()), '->load() adds tags to extension definitions');
  55. }
  56. // Form resources
  57. $resources = $container->getParameter('twig.form.resources');
  58. $this->assertContains('TwigBundle::form.html.twig', $resources, '->load() includes default template for form resources');
  59. $this->assertContains('MyBundle::form.html.twig', $resources, '->load() merges new templates into form resources');
  60. // Globals
  61. $calls = $container->getDefinition('twig')->getMethodCalls();
  62. $this->assertEquals('foo', $calls[0][1][0], '->load() registers services as Twig globals');
  63. $this->assertEquals(new Reference('bar'), $calls[0][1][1], '->load() registers services as Twig globals');
  64. $this->assertEquals('pi', $calls[1][1][0], '->load() registers variables as Twig globals');
  65. $this->assertEquals(3.14, $calls[1][1][1], '->load() registers variables as Twig globals');
  66. // Twig options
  67. $options = $container->getParameter('twig.options');
  68. $this->assertTrue($options['auto_reload'], '->load() sets the auto_reload option');
  69. $this->assertTrue($options['autoescape'], '->load() sets the autoescape option');
  70. $this->assertEquals('stdClass', $options['base_template_class'], '->load() sets the base_template_class option');
  71. $this->assertEquals('/tmp', $options['cache'], '->load() sets the cache option');
  72. $this->assertEquals('ISO-8859-1', $options['charset'], '->load() sets the charset option');
  73. $this->assertTrue($options['debug'], '->load() sets the debug option');
  74. $this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
  75. }
  76. public function getFormats()
  77. {
  78. return array(
  79. array('php'),
  80. array('yml'),
  81. array('xml'),
  82. );
  83. }
  84. private function createContainer()
  85. {
  86. $container = new ContainerBuilder(new ParameterBag(array(
  87. 'kernel.cache_dir' => __DIR__,
  88. 'kernel.charset' => 'UTF-8',
  89. 'kernel.debug' => false,
  90. )));
  91. return $container;
  92. }
  93. private function compileContainer(ContainerBuilder $container)
  94. {
  95. $container->getCompilerPassConfig()->setOptimizationPasses(array());
  96. $container->getCompilerPassConfig()->setRemovingPasses(array());
  97. $container->compile();
  98. }
  99. private function loadFromFile(ContainerBuilder $container, $file, $format)
  100. {
  101. $locator = new FileLocator(__DIR__.'/Fixtures/'.$format);
  102. switch ($format) {
  103. case 'php':
  104. $loader = new PhpFileLoader($container, $locator);
  105. break;
  106. case 'xml':
  107. $loader = new XmlFileLoader($container, $locator);
  108. break;
  109. case 'yml':
  110. $loader = new YamlFileLoader($container, $locator);
  111. break;
  112. default:
  113. throw new \InvalidArgumentException('Unsupported format: '.$format);
  114. }
  115. $loader->load($file.'.'.$format);
  116. }
  117. }