TranslationNodeVisitorTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\Tests\NodeVisitor;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
  13. use Twig\Environment;
  14. use Twig\Node\Expression\ArrayExpression;
  15. use Twig\Node\Expression\ConstantExpression;
  16. use Twig\Node\Expression\FilterExpression;
  17. use Twig\Node\Expression\NameExpression;
  18. use Twig\Node\Node;
  19. class TranslationNodeVisitorTest extends TestCase
  20. {
  21. /** @dataProvider getMessagesExtractionTestData */
  22. public function testMessagesExtraction(Node $node, array $expectedMessages)
  23. {
  24. $env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
  25. $visitor = new TranslationNodeVisitor();
  26. $visitor->enable();
  27. $visitor->enterNode($node, $env);
  28. $visitor->leaveNode($node, $env);
  29. $this->assertEquals($expectedMessages, $visitor->getMessages());
  30. }
  31. public function testMessageExtractionWithInvalidDomainNode()
  32. {
  33. $message = 'new key';
  34. $node = new FilterExpression(
  35. new ConstantExpression($message, 0),
  36. new ConstantExpression('trans', 0),
  37. new Node(array(
  38. new ArrayExpression(array(), 0),
  39. new NameExpression('variable', 0),
  40. )),
  41. 0
  42. );
  43. $this->testMessagesExtraction($node, array(array($message, TranslationNodeVisitor::UNDEFINED_DOMAIN)));
  44. }
  45. public function getMessagesExtractionTestData()
  46. {
  47. $message = 'new key';
  48. $domain = 'domain';
  49. return array(
  50. array(TwigNodeProvider::getTransFilter($message), array(array($message, null))),
  51. array(TwigNodeProvider::getTransChoiceFilter($message), array(array($message, null))),
  52. array(TwigNodeProvider::getTransTag($message), array(array($message, null))),
  53. array(TwigNodeProvider::getTransFilter($message, $domain), array(array($message, $domain))),
  54. array(TwigNodeProvider::getTransChoiceFilter($message, $domain), array(array($message, $domain))),
  55. array(TwigNodeProvider::getTransTag($message, $domain), array(array($message, $domain))),
  56. );
  57. }
  58. }