public function Twig_Tests_EnvironmentTest::testGlobals

File

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

Class

Twig_Tests_EnvironmentTest

Code

public function testGlobals() {

  // globals can be added after calling getGlobals
  $twig = new Twig_Environment(new Twig_Loader_String());
  $twig
    ->addGlobal('foo', 'foo');
  $globals = $twig
    ->getGlobals();
  $twig
    ->addGlobal('foo', 'bar');
  $globals = $twig
    ->getGlobals();
  $this
    ->assertEquals('bar', $globals['foo']);

  // globals can be modified after runtime init
  $twig = new Twig_Environment(new Twig_Loader_String());
  $twig
    ->addGlobal('foo', 'foo');
  $globals = $twig
    ->getGlobals();
  $twig
    ->initRuntime();
  $twig
    ->addGlobal('foo', 'bar');
  $globals = $twig
    ->getGlobals();
  $this
    ->assertEquals('bar', $globals['foo']);

  // globals can be modified after extensions init
  $twig = new Twig_Environment(new Twig_Loader_String());
  $twig
    ->addGlobal('foo', 'foo');
  $globals = $twig
    ->getGlobals();
  $twig
    ->getFunctions();
  $twig
    ->addGlobal('foo', 'bar');
  $globals = $twig
    ->getGlobals();
  $this
    ->assertEquals('bar', $globals['foo']);

  // globals can be modified after extensions and runtime init
  $twig = new Twig_Environment(new Twig_Loader_String());
  $twig
    ->addGlobal('foo', 'foo');
  $globals = $twig
    ->getGlobals();
  $twig
    ->getFunctions();
  $twig
    ->initRuntime();
  $twig
    ->addGlobal('foo', 'bar');
  $globals = $twig
    ->getGlobals();
  $this
    ->assertEquals('bar', $globals['foo']);
  $twig = new Twig_Environment(new Twig_Loader_String());
  $twig
    ->getGlobals();
  $twig
    ->addGlobal('foo', 'bar');
  $template = $twig
    ->loadTemplate('{{foo}}');
  $this
    ->assertEquals('bar', $template
    ->render(array()));

  /* to be uncomment in Twig 2.0
          // globals cannot be added after runtime init
          $twig = new Twig_Environment(new Twig_Loader_String());
          $twig->addGlobal('foo', 'foo');
          $globals = $twig->getGlobals();
          $twig->initRuntime();
          try {
              $twig->addGlobal('bar', 'bar');
              $this->fail();
          } catch (LogicException $e) {
              $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
          }

          // globals cannot be added after extensions init
          $twig = new Twig_Environment(new Twig_Loader_String());
          $twig->addGlobal('foo', 'foo');
          $globals = $twig->getGlobals();
          $twig->getFunctions();
          try {
              $twig->addGlobal('bar', 'bar');
              $this->fail();
          } catch (LogicException $e) {
              $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
          }

          // globals cannot be added after extensions and runtime init
          $twig = new Twig_Environment(new Twig_Loader_String());
          $twig->addGlobal('foo', 'foo');
          $globals = $twig->getGlobals();
          $twig->getFunctions();
          $twig->initRuntime();
          try {
              $twig->addGlobal('bar', 'bar');
              $this->fail();
          } catch (LogicException $e) {
              $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
          }

          // test adding globals after initRuntime without call to getGlobals
          $twig = new Twig_Environment(new Twig_Loader_String());
          $twig->initRuntime();
          try {
              $twig->addGlobal('bar', 'bar');
              $this->fail();
          } catch (LogicException $e) {
              $this->assertFalse(array_key_exists('bar', $twig->getGlobals()));
          }
          */
}