vendor/symfony/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection;
       
    13 
       
    14 use Symfony\Bundle\TwigBundle\DependencyInjection\TwigExtension;
       
    15 use Symfony\Bundle\TwigBundle\Tests\TestCase;
       
    16 use Symfony\Component\Config\FileLocator;
       
    17 use Symfony\Component\DependencyInjection\ContainerBuilder;
       
    18 use Symfony\Component\DependencyInjection\Reference;
       
    19 use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
       
    20 use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
       
    21 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
       
    22 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
       
    23 
       
    24 class TwigExtensionTest extends TestCase
       
    25 {
       
    26     public function testLoadEmptyConfiguration()
       
    27     {
       
    28         $container = $this->createContainer();
       
    29         $container->registerExtension(new TwigExtension());
       
    30         $container->loadFromExtension('twig', array());
       
    31         $this->compileContainer($container);
       
    32 
       
    33         $this->assertEquals('Twig_Environment', $container->getParameter('twig.class'), '->load() loads the twig.xml file');
       
    34         $this->assertContains('form_div_layout.html.twig', $container->getParameter('twig.form.resources'), '->load() includes default template for form resources');
       
    35 
       
    36         // Twig options
       
    37         $options = $container->getParameter('twig.options');
       
    38         $this->assertEquals(__DIR__.'/twig', $options['cache'], '->load() sets default value for cache option');
       
    39         $this->assertEquals('UTF-8', $options['charset'], '->load() sets default value for charset option');
       
    40         $this->assertEquals(false, $options['debug'], '->load() sets default value for debug option');
       
    41     }
       
    42 
       
    43     /**
       
    44      * @dataProvider getFormats
       
    45      */
       
    46     public function testLoadFullConfiguration($format)
       
    47     {
       
    48         $container = $this->createContainer();
       
    49         $container->registerExtension(new TwigExtension());
       
    50         $this->loadFromFile($container, 'full', $format);
       
    51         $this->compileContainer($container);
       
    52 
       
    53         $this->assertEquals('Twig_Environment', $container->getParameter('twig.class'), '->load() loads the twig.xml file');
       
    54 
       
    55         // Form resources
       
    56         $resources = $container->getParameter('twig.form.resources');
       
    57         $this->assertContains('form_div_layout.html.twig', $resources, '->load() includes default template for form resources');
       
    58         $this->assertContains('MyBundle::form.html.twig', $resources, '->load() merges new templates into form resources');
       
    59 
       
    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 
       
    67         // Twig options
       
    68         $options = $container->getParameter('twig.options');
       
    69         $this->assertTrue($options['auto_reload'], '->load() sets the auto_reload option');
       
    70         $this->assertTrue($options['autoescape'], '->load() sets the autoescape option');
       
    71         $this->assertEquals('stdClass', $options['base_template_class'], '->load() sets the base_template_class option');
       
    72         $this->assertEquals('/tmp', $options['cache'], '->load() sets the cache option');
       
    73         $this->assertEquals('ISO-8859-1', $options['charset'], '->load() sets the charset option');
       
    74         $this->assertTrue($options['debug'], '->load() sets the debug option');
       
    75         $this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
       
    76     }
       
    77 
       
    78     public function testGlobalsWithDifferentTypesAndValues()
       
    79     {
       
    80         $globals = array(
       
    81             'array'   => array(),
       
    82             'false'   => false,
       
    83             'float'   => 2.0,
       
    84             'integer' => 3,
       
    85             'null'    => null,
       
    86             'object'  => new \stdClass(),
       
    87             'string'  => 'foo',
       
    88             'true'    => true,
       
    89         );
       
    90 
       
    91         $container = $this->createContainer();
       
    92         $container->registerExtension(new TwigExtension());
       
    93         $container->loadFromExtension('twig', array('globals' => $globals));
       
    94         $this->compileContainer($container);
       
    95 
       
    96         $calls = $container->getDefinition('twig')->getMethodCalls();
       
    97 
       
    98         foreach ($calls as $call) {
       
    99             list($name, $value) = each($globals);
       
   100             $this->assertEquals($name, $call[1][0]);
       
   101             $this->assertSame($value, $call[1][1]);
       
   102         }
       
   103     }
       
   104 
       
   105     public function getFormats()
       
   106     {
       
   107         return array(
       
   108             array('php'),
       
   109             array('yml'),
       
   110             array('xml'),
       
   111         );
       
   112     }
       
   113 
       
   114     private function createContainer()
       
   115     {
       
   116         $container = new ContainerBuilder(new ParameterBag(array(
       
   117             'kernel.cache_dir' => __DIR__,
       
   118             'kernel.charset'   => 'UTF-8',
       
   119             'kernel.debug'     => false,
       
   120         )));
       
   121 
       
   122         return $container;
       
   123     }
       
   124 
       
   125     private function compileContainer(ContainerBuilder $container)
       
   126     {
       
   127         $container->getCompilerPassConfig()->setOptimizationPasses(array());
       
   128         $container->getCompilerPassConfig()->setRemovingPasses(array());
       
   129         $container->compile();
       
   130     }
       
   131 
       
   132     private function loadFromFile(ContainerBuilder $container, $file, $format)
       
   133     {
       
   134         $locator = new FileLocator(__DIR__.'/Fixtures/'.$format);
       
   135 
       
   136         switch ($format) {
       
   137             case 'php':
       
   138                 $loader = new PhpFileLoader($container, $locator);
       
   139                 break;
       
   140             case 'xml':
       
   141                 $loader = new XmlFileLoader($container, $locator);
       
   142                 break;
       
   143             case 'yml':
       
   144                 $loader = new YamlFileLoader($container, $locator);
       
   145                 break;
       
   146             default:
       
   147                 throw new \InvalidArgumentException('Unsupported format: '.$format);
       
   148         }
       
   149 
       
   150         $loader->load($file.'.'.$format);
       
   151     }
       
   152 }