public function Twig_Tests_EnvironmentTest::testExtensionsAreNotInitializedWhenRenderingACompiledTemplate

File

drupal/core/vendor/twig/twig/test/Twig/Tests/EnvironmentTest.php, line 140

Class

Twig_Tests_EnvironmentTest

Code

public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate() {
  $options = array(
    'cache' => sys_get_temp_dir() . '/twig',
    'auto_reload' => false,
    'debug' => false,
  );

  // force compilation
  $twig = new Twig_Environment(new Twig_Loader_String(), $options);
  $cache = $twig
    ->getCacheFilename('{{ foo }}');
  if (!is_dir(dirname($cache))) {
    mkdir(dirname($cache), 0777, true);
  }
  file_put_contents($cache, $twig
    ->compileSource('{{ foo }}', '{{ foo }}'));

  // check that extensions won't be initialized when rendering a template that is already in the cache
  $twig = $this
    ->getMockBuilder('Twig_Environment')
    ->setConstructorArgs(array(
    new Twig_Loader_String(),
    $options,
  ))
    ->setMethods(array(
    'initExtensions',
  ))
    ->getMock();
  $twig
    ->expects($this
    ->never())
    ->method('initExtensions');

  // render template
  $output = $twig
    ->render('{{ foo }}', array(
    'foo' => 'bar',
  ));
  $this
    ->assertEquals('bar', $output);
  unlink($cache);
}