You are here

public function Twig_Tests_EnvironmentTest::testGlobals in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/twig/twig/test/Twig/Tests/EnvironmentTest.php \Twig_Tests_EnvironmentTest::testGlobals()

File

vendor/twig/twig/test/Twig/Tests/EnvironmentTest.php, line 47

Class

Twig_Tests_EnvironmentTest

Code

public function testGlobals() {

  // globals can be added after calling getGlobals
  $twig = new Twig_Environment($this
    ->getMock('Twig_LoaderInterface'));
  $twig
    ->addGlobal('foo', 'foo');
  $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($this
    ->getMock('Twig_LoaderInterface'));
  $twig
    ->addGlobal('foo', 'foo');
  $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($this
    ->getMock('Twig_LoaderInterface'));
  $twig
    ->addGlobal('foo', 'foo');
  $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($loader = new Twig_Loader_Array(array(
    'index' => '{{foo}}',
  )));
  $twig
    ->addGlobal('foo', 'foo');
  $twig
    ->getGlobals();
  $twig
    ->getFunctions();
  $twig
    ->initRuntime();
  $twig
    ->addGlobal('foo', 'bar');
  $globals = $twig
    ->getGlobals();
  $this
    ->assertEquals('bar', $globals['foo']);
  $twig = new Twig_Environment($loader);
  $twig
    ->getGlobals();
  $twig
    ->addGlobal('foo', 'bar');
  $template = $twig
    ->loadTemplate('index');
  $this
    ->assertEquals('bar', $template
    ->render(array()));

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