TwigExtensionTest.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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('app', $calls[0][1][0]);
  53. $this->assertEquals(new Reference('templating.globals'), $calls[0][1][1]);
  54. $this->assertEquals('foo', $calls[1][1][0], '->load() registers services as Twig globals');
  55. $this->assertEquals(new Reference('bar'), $calls[1][1][1], '->load() registers services as Twig globals');
  56. $this->assertEquals('pi', $calls[2][1][0], '->load() registers variables as Twig globals');
  57. $this->assertEquals(3.14, $calls[2][1][1], '->load() registers variables as Twig globals');
  58. // Yaml and Php specific configs
  59. if (in_array($format, array('yml', 'php'))) {
  60. $this->assertEquals('bad', $calls[3][1][0], '->load() registers variables as Twig globals');
  61. $this->assertEquals(array('key' => 'foo'), $calls[3][1][1], '->load() registers variables as Twig globals');
  62. }
  63. // Twig options
  64. $options = $container->getParameter('twig.options');
  65. $this->assertTrue($options['auto_reload'], '->load() sets the auto_reload option');
  66. $this->assertTrue($options['autoescape'], '->load() sets the autoescape option');
  67. $this->assertEquals('stdClass', $options['base_template_class'], '->load() sets the base_template_class option');
  68. $this->assertEquals('/tmp', $options['cache'], '->load() sets the cache option');
  69. $this->assertEquals('ISO-8859-1', $options['charset'], '->load() sets the charset option');
  70. $this->assertTrue($options['debug'], '->load() sets the debug option');
  71. $this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
  72. }
  73. public function testGlobalsWithDifferentTypesAndValues()
  74. {
  75. $globals = array(
  76. 'array' => array(),
  77. 'false' => false,
  78. 'float' => 2.0,
  79. 'integer' => 3,
  80. 'null' => null,
  81. 'object' => new \stdClass(),
  82. 'string' => 'foo',
  83. 'true' => true,
  84. );
  85. $container = $this->createContainer();
  86. $container->registerExtension(new TwigExtension());
  87. $container->loadFromExtension('twig', array('globals' => $globals));
  88. $this->compileContainer($container);
  89. $calls = $container->getDefinition('twig')->getMethodCalls();
  90. foreach (array_slice($calls, 1) as $call) {
  91. list($name, $value) = each($globals);
  92. $this->assertEquals($name, $call[1][0]);
  93. $this->assertSame($value, $call[1][1]);
  94. }
  95. }
  96. public function getFormats()
  97. {
  98. return array(
  99. array('php'),
  100. array('yml'),
  101. array('xml'),
  102. );
  103. }
  104. private function createContainer()
  105. {
  106. $container = new ContainerBuilder(new ParameterBag(array(
  107. 'kernel.cache_dir' => __DIR__,
  108. 'kernel.charset' => 'UTF-8',
  109. 'kernel.debug' => false,
  110. )));
  111. return $container;
  112. }
  113. private function compileContainer(ContainerBuilder $container)
  114. {
  115. $container->getCompilerPassConfig()->setOptimizationPasses(array());
  116. $container->getCompilerPassConfig()->setRemovingPasses(array());
  117. $container->compile();
  118. }
  119. private function loadFromFile(ContainerBuilder $container, $file, $format)
  120. {
  121. $locator = new FileLocator(__DIR__.'/Fixtures/'.$format);
  122. switch ($format) {
  123. case 'php':
  124. $loader = new PhpFileLoader($container, $locator);
  125. break;
  126. case 'xml':
  127. $loader = new XmlFileLoader($container, $locator);
  128. break;
  129. case 'yml':
  130. $loader = new YamlFileLoader($container, $locator);
  131. break;
  132. default:
  133. throw new \InvalidArgumentException('Unsupported format: '.$format);
  134. }
  135. $loader->load($file.'.'.$format);
  136. }
  137. }