|
| 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\Bridge\Twig\Tests\Node; |
| 13 | + |
| 14 | +use Symfony\Bridge\Twig\Node\TransNode; |
| 15 | + |
| 16 | +/** |
| 17 | + * |
| 18 | + * @author Asmir Mustafic <goetas@gmail.com> |
| 19 | + * |
| 20 | + */ |
| 21 | +class TransNodeTest extends \PHPUnit_Framework_TestCase |
| 22 | +{ |
| 23 | + |
| 24 | + public function testCompileStrict() |
| 25 | + { |
| 26 | + $body = new \Twig_Node_Text('trans %var%', 0); |
| 27 | + $vars = new \Twig_Node_Expression_Name('foo', 0); |
| 28 | + $node = new TransNode($body, null, null, $vars); |
| 29 | + |
| 30 | + $env = new \Twig_Environment(null, array( |
| 31 | + 'strict_variables' => true, |
| 32 | + )); |
| 33 | + $compiler = new \Twig_Compiler($env); |
| 34 | + |
| 35 | + $this->assertEquals( |
| 36 | + sprintf( |
| 37 | + 'echo $this->env->getExtension(\'translator\')->getTranslator()->trans("trans %%var%%", array_merge(array("%%var%%" => %s), %s), "messages");', |
| 38 | + $this->getVariableGetterWithoutStrictCheck('var'), |
| 39 | + $this->getVariableGetterWithStrictCheck('foo') |
| 40 | + ), |
| 41 | + trim($compiler->compile($node)->getSource()) |
| 42 | + ); |
| 43 | + } |
| 44 | + protected function getVariableGetterWithoutStrictCheck($name) |
| 45 | + { |
| 46 | + if (version_compare(phpversion(), '5.4.0RC1', '>=')) { |
| 47 | + return sprintf('(isset($context["%s"]) ? $context["%s"] : null)', $name, $name); |
| 48 | + } |
| 49 | + |
| 50 | + return sprintf('$this->getContext($context, "%s", true)', $name); |
| 51 | + } |
| 52 | + protected function getVariableGetterWithStrictCheck($name) |
| 53 | + { |
| 54 | + if (version_compare(phpversion(), '5.4.0RC1', '>=')) { |
| 55 | + return sprintf('(isset($context["%s"]) ? $context["%s"] : $this->getContext($context, "%s"))', $name, $name, $name); |
| 56 | + } |
| 57 | + |
| 58 | + return sprintf('$this->getContext($context, "%s")', $name); |
| 59 | + } |
| 60 | +} |
0 commit comments