diff --git a/src/Symfony/Bridge/Twig/NodeVisitor/Scope.php b/src/Symfony/Bridge/Twig/NodeVisitor/Scope.php index 4993aed90a808..564e3573b1b8a 100644 --- a/src/Symfony/Bridge/Twig/NodeVisitor/Scope.php +++ b/src/Symfony/Bridge/Twig/NodeVisitor/Scope.php @@ -34,6 +34,7 @@ public function __construct(Scope $parent = null) { $this->parent = $parent; $this->left = false; + $this->data = array(); } /** diff --git a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php index f6a764bc6cd00..8e7e7f48e3dc8 100644 --- a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php +++ b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php @@ -39,14 +39,10 @@ public function __construct() */ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) { - if ($node instanceof \Twig_Node_Block) { + if ($node instanceof \Twig_Node_Block || $node instanceof \Twig_Node_Module) { $this->scope = $this->scope->enter(); } - if ($node instanceof \Twig_Node_Module) { - $this->scope->set('domain', null); - } - if ($node instanceof TransDefaultDomainNode) { if ($node->getNode('expr') instanceof \Twig_Node_Expression_Constant) { $this->scope->set('domain', $node->getNode('expr')); @@ -93,7 +89,7 @@ public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) return false; } - if ($node instanceof \Twig_Node_Block) { + if ($node instanceof \Twig_Node_Block || $node instanceof \Twig_Node_Module) { $this->scope = $this->scope->leave(); } diff --git a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationDefaultDomainNodeVisitorTest.php b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationDefaultDomainNodeVisitorTest.php index 9fd367c6c94e4..7e9c866094941 100644 --- a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationDefaultDomainNodeVisitorTest.php +++ b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationDefaultDomainNodeVisitorTest.php @@ -15,11 +15,10 @@ class TranslationDefaultDomainNodeVisitorTest extends TestCase public function testDefaultDomainAssignment(\Twig_Node $node) { $env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0)); - $visitor = new TranslationDefaultDomainNodeVisitor(); // visit trans_default_domain tag - $defaultDomain = TwigNodeProvider::getTransDefaultDomainTag('domain'); + $defaultDomain = TwigNodeProvider::getTransDefaultDomainTag(self::$domain); $visitor->enterNode($defaultDomain, $env); $visitor->leaveNode($defaultDomain, $env); @@ -38,12 +37,38 @@ public function testDefaultDomainAssignment(\Twig_Node $node) $this->assertEquals(array(array(self::$message, self::$domain)), $visitor->getMessages()); } + /** @dataProvider getDefaultDomainAssignmentTestData */ + public function testNewModuleWithoutDefaultDomainTag(\Twig_Node $node) + { + $env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0)); + $visitor = new TranslationDefaultDomainNodeVisitor(); + + // visit trans_default_domain tag + $newModule = TwigNodeProvider::getModule('test'); + $visitor->enterNode($newModule, $env); + $visitor->leaveNode($newModule, $env); + + // visit tested node + $enteredNode = $visitor->enterNode($node, $env); + $leavedNode = $visitor->leaveNode($node, $env); + $this->assertSame($node, $enteredNode); + $this->assertSame($node, $leavedNode); + + // extracting tested node messages + $visitor = new TranslationNodeVisitor(); + $visitor->enable(); + $visitor->enterNode($node, $env); + $visitor->leaveNode($node, $env); + + $this->assertEquals(array(array(self::$message, null)), $visitor->getMessages()); + } + public function getDefaultDomainAssignmentTestData() { return array( - array(TwigNodeProvider::getTransFilter(self::$message, self::$domain)), - array(TwigNodeProvider::getTransChoiceFilter(self::$message, self::$domain)), - array(TwigNodeProvider::getTransTag(self::$message, self::$domain)), + array(TwigNodeProvider::getTransFilter(self::$message)), + array(TwigNodeProvider::getTransChoiceFilter(self::$message)), + array(TwigNodeProvider::getTransTag(self::$message)), ); } } diff --git a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php index 2b753ce7afc79..2e26aeef582e9 100644 --- a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php +++ b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php @@ -7,6 +7,19 @@ class TwigNodeProvider { + public static function getModule($content) + { + return new \Twig_Node_Module( + new \Twig_Node_Expression_Constant($content, 0), + null, + new \Twig_Node_Expression_Array(array(), 0), + new \Twig_Node_Expression_Array(array(), 0), + new \Twig_Node_Expression_Array(array(), 0), + null, + null + ); + } + public static function getTransFilter($message, $domain = null) { $arguments = $domain ? array(