From c5e999aad49d7ea9c5cf317ed522006ff14c1894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Tue, 12 Mar 2013 17:19:53 +0100 Subject: [PATCH 1/3] [TwigBridge] fixed non probant tests & added new one --- ...ranslationDefaultDomainNodeVisitorTest.php | 35 ++++++++++++++++--- .../Tests/NodeVisitor/TwigNodeProvider.php | 13 +++++++ 2 files changed, 43 insertions(+), 5 deletions(-) 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( From fe4cc249b500323b9bde6acff04182a86414c88f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Tue, 12 Mar 2013 17:20:40 +0100 Subject: [PATCH 2/3] [TwigBridge] fixed fixed scope & trans_default_domain node visitor --- src/Symfony/Bridge/Twig/NodeVisitor/Scope.php | 1 + .../Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) 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..04bcce5700462 100644 --- a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php +++ b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php @@ -43,10 +43,6 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) $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')); From 5ad4bd1e4065ed5bc51e81b3a8f290f4a8a0498f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Tue, 12 Mar 2013 18:03:08 +0100 Subject: [PATCH 3/3] [TwigBridge] now enter/leave scope on Twig_Node_Module --- .../Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php index 04bcce5700462..8e7e7f48e3dc8 100644 --- a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php +++ b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php @@ -39,7 +39,7 @@ 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(); } @@ -89,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(); }