-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TwigBridge] fixed trans twig extractor #7206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
94364da
[TwigBridge] added trans_default_domain + trans test
jfsimon 11b7dbc
TwigBridge] added translation extractor's node visitors tests
jfsimon 2f3d0e0
[TwigBridge] fixed node visitors priority & added extractor tests
jfsimon 5a2c1c0
removed temp variable when compiling trans_default_domain to allow ot…
fabpot f5dd9c7
[TwigBridge] applied previous fix on node visitors
jfsimon 0923e1d
[TwigBridge] added message extraction with invalid domain node test
jfsimon c032595
[TwigBridge] added support for named arguments to translation visitor
jfsimon e4be8e7
[TwigBridge] renamed private methods
jfsimon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationDefaultDomainNodeVisitorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Symfony\Bridge\Twig\Tests\NodeVisitor; | ||
|
||
use Symfony\Bridge\Twig\NodeVisitor\TranslationDefaultDomainNodeVisitor; | ||
use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor; | ||
use Symfony\Bridge\Twig\Tests\TestCase; | ||
|
||
class TranslationDefaultDomainNodeVisitorTest extends TestCase | ||
{ | ||
private static $message = 'message'; | ||
private static $domain = 'domain'; | ||
|
||
/** @dataProvider getDefaultDomainAssignmentTestData */ | ||
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'); | ||
$visitor->enterNode($defaultDomain, $env); | ||
$visitor->leaveNode($defaultDomain, $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, self::$domain)), $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)), | ||
); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Symfony\Bridge\Twig\Tests\NodeVisitor; | ||
|
||
use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor; | ||
use Symfony\Bridge\Twig\Tests\TestCase; | ||
|
||
class TranslationNodeVisitorTest extends TestCase | ||
{ | ||
/** @dataProvider getMessagesExtractionTestData */ | ||
public function testMessagesExtraction(\Twig_Node $node, array $expectedMessages) | ||
{ | ||
$env = new \Twig_Environment(new \Twig_Loader_String(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0)); | ||
$visitor = new TranslationNodeVisitor(); | ||
$visitor->enable(); | ||
$visitor->enterNode($node, $env); | ||
$visitor->leaveNode($node, $env); | ||
$this->assertEquals($expectedMessages, $visitor->getMessages()); | ||
} | ||
|
||
public function testMessageExtractionWithInvalidDomainNode() | ||
{ | ||
$message = 'new key'; | ||
|
||
$node = new \Twig_Node_Expression_Filter( | ||
new \Twig_Node_Expression_Constant($message, 0), | ||
new \Twig_Node_Expression_Constant('trans', 0), | ||
new \Twig_Node(array( | ||
new \Twig_Node_Expression_Array(array(), 0), | ||
new \Twig_Node_Expression_Name('variable', 0), | ||
)), | ||
0 | ||
); | ||
|
||
$this->testMessagesExtraction($node, array(array($message, TranslationNodeVisitor::UNDEFINED_DOMAIN))); | ||
} | ||
|
||
public function getMessagesExtractionTestData() | ||
{ | ||
$message = 'new key'; | ||
$domain = 'domain'; | ||
|
||
return array( | ||
array(TwigNodeProvider::getTransFilter($message), array(array($message, null))), | ||
array(TwigNodeProvider::getTransChoiceFilter($message), array(array($message, null))), | ||
array(TwigNodeProvider::getTransTag($message), array(array($message, null))), | ||
array(TwigNodeProvider::getTransFilter($message, $domain), array(array($message, $domain))), | ||
array(TwigNodeProvider::getTransChoiceFilter($message, $domain), array(array($message, $domain))), | ||
array(TwigNodeProvider::getTransTag($message, $domain), array(array($message, $domain))), | ||
); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Symfony\Bridge\Twig\Tests\NodeVisitor; | ||
|
||
use Symfony\Bridge\Twig\Node\TransDefaultDomainNode; | ||
use Symfony\Bridge\Twig\Node\TransNode; | ||
|
||
class TwigNodeProvider | ||
{ | ||
public static function getTransFilter($message, $domain = null) | ||
{ | ||
$arguments = $domain ? array( | ||
new \Twig_Node_Expression_Array(array(), 0), | ||
new \Twig_Node_Expression_Constant($domain, 0), | ||
) : array(); | ||
|
||
return new \Twig_Node_Expression_Filter( | ||
new \Twig_Node_Expression_Constant($message, 0), | ||
new \Twig_Node_Expression_Constant('trans', 0), | ||
new \Twig_Node($arguments), | ||
0 | ||
); | ||
} | ||
|
||
public static function getTransChoiceFilter($message, $domain = null) | ||
{ | ||
$arguments = $domain ? array( | ||
new \Twig_Node_Expression_Constant(0, 0), | ||
new \Twig_Node_Expression_Array(array(), 0), | ||
new \Twig_Node_Expression_Constant($domain, 0), | ||
) : array(); | ||
|
||
return new \Twig_Node_Expression_Filter( | ||
new \Twig_Node_Expression_Constant($message, 0), | ||
new \Twig_Node_Expression_Constant('transchoice', 0), | ||
new \Twig_Node($arguments), | ||
0 | ||
); | ||
} | ||
|
||
public static function getTransTag($message, $domain = null) | ||
{ | ||
return new TransNode( | ||
new \Twig_Node_Body(array(), array('data' => $message)), | ||
$domain ? new \Twig_Node_Expression_Constant($domain, 0) : null | ||
); | ||
} | ||
|
||
public static function getTransDefaultDomainTag($domain) | ||
{ | ||
return new TransDefaultDomainNode( | ||
new \Twig_Node_Expression_Constant($domain, 0) | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong. A visitor has to return a node, not a boolean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or
false
to remove the node ;)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then the phpdoc of the interface is wrong