TwigExtensionTest.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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. public function testLoadEmptyConfiguration()
  23. {
  24. $container = $this->createContainer();
  25. $container->registerExtension(new TwigExtension());
  26. $container->loadFromExtension('twig', array());
  27. $this->compileContainer($container);
  28. $this->assertEquals('Twig_Environment', $container->getParameter('twig.class'), '->load() loads the twig.xml file');
  29. $this->assertContains('form_div_layout.html.twig', $container->getParameter('twig.form.resources'), '->load() includes default template for form resources');
  30. // Twig options
  31. $options = $container->getParameter('twig.options');
  32. $this->assertEquals(__DIR__.'/twig', $options['cache'], '->load() sets default value for cache option');
  33. $this->assertEquals('UTF-8', $options['charset'], '->load() sets default value for charset option');
  34. $this->assertEquals(false, $options['debug'], '->load() sets default value for debug option');
  35. }
  36. /**
  37. * @dataProvider getFormats
  38. */
  39. public function testLoadFullConfiguration($format)
  40. {
  41. $container = $this->createContainer();
  42. $container->registerExtension(new TwigExtension());
  43. $this->loadFromFile($container, 'full', $format);
  44. $this->compileContainer($container);
  45. $this->assertEquals('Twig_Environment', $container->getParameter('twig.class'), '->load() loads the twig.xml file');
  46. // Form resources
  47. $resources = $container->getParameter('twig.form.resources');
  48. $this->assertContains('form_div_layout.html.twig', $resources, '->load() includes default template for form resources');
  49. $this->assertContains('MyBundle::form.html.twig', $resources, '->load() merges new templates into form resources');
  50. // Globals
  51. $calls = $container->getDefinition('twig')->getMethodCalls();
  52. $this->assertEquals('foo', $calls[0][1][0], '->load() registers services as Twig globals');
  53. $this->assertEquals(new Reference('bar'), $calls[0][1][1], '->load() registers services as Twig globals');
  54. $this->assertEquals('pi', $calls[1][1][0], '->load() registers variables as Twig globals');
  55. $this->assertEquals(3.14, $calls[1][1][1], '->load() registers variables as Twig globals');
  56. // Yaml and Php specific configs
  57. if (in_array($format, array('yml', 'php'))) {
  58. $this->assertEquals('bad', $calls[2][1][0], '->load() registers variables as Twig globals');
  59. $this->assertEquals(array('key' => 'foo'), $calls[2][1][1], '->load() registers variables as Twig globals');
  60. }
  61. // Twig options
  62. $options = $container->getParameter('twig.options');
  63. $this->assertTrue($options['auto_reload'], '->load() sets the auto_reload option');
  64. $this->assertTrue($options['autoescape'], '->load() sets the autoescape option');
  65. $this->assertEquals('stdClass', $options['base_template_class'], '->load() sets the base_template_class option');
  66. $this->assertEquals('/tmp', $options['cache'], '->load() sets the cache option');
  67. $this->assertEquals('ISO-8859-1', $options['charset'], '->load() sets the charset option');
  68. $this->assertTrue($options['debug'], '->load() sets the debug option');
  69. $this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
  70. }
  71. public function testGlobalsWithDifferentTypesAndValues()
  72. {
  73. $globals = array(
  74. 'array' => array(),
  75. 'false' => false,
  76. 'float' => 2.0,
  77. 'integer' => 3,
  78. 'null' => null,
  79. 'object' => new \stdClass(),
  80. 'string' => 'foo',
  81. 'true' => true,
  82. );
  83. $container = $this->createContainer();
  84. $container->registerExtension(new TwigExtension());
  85. $container->loadFromExtension('twig', array('globals' => $globals));
  86. $this->compileContainer($container);
  87. $calls = $container->getDefinition('twig')->getMethodCalls();
  88. foreach ($calls as $call) {
  89. list($name, $value) = each($globals);
  90. $this->assertEquals($name, $call[1][0]);
  91. $this->assertSame($value, $call[1][1]);
  92. }
  93. }
  94. public function getFormats()
  95. {
  96. return array(
  97. array('php'),
  98. array('yml'),
  99. array('xml'),
  100. );
  101. }
  102. private function createContainer()
  103. {
  104. $container = new ContainerBuilder(new ParameterBag(array(
  105. 'kernel.cache_dir' => __DIR__,
  106. 'kernel.charset' => 'UTF-8',
  107. 'kernel.debug' => false,
  108. )));
  109. return $container;
  110. }
  111. private function compileContainer(ContainerBuilder $container)
  112. {
  113. $container->getCompilerPassConfig()->setOptimizationPasses(array());
  114. $container->getCompilerPassConfig()->setRemovingPasses(array());
  115. $container->compile();
  116. }
  117. private function loadFromFile(ContainerBuilder $container, $file, $format)
  118. {
  119. $locator = new FileLocator(__DIR__.'/Fixtures/'.$format);
  120. switch ($format) {
  121. case 'php':
  122. $loader = new PhpFileLoader($container, $locator);
  123. break;
  124. case 'xml':
  125. $loader = new XmlFileLoader($container, $locator);
  126. break;
  127. case 'yml':
  128. $loader = new YamlFileLoader($container, $locator);
  129. break;
  130. default:
  131. throw new \InvalidArgumentException('Unsupported format: '.$format);
  132. }
  133. $loader->load($file.'.'.$format);
  134. }
  135. }