TranslationDefaultDomainNodeVisitorTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\TranslationDefaultDomainNodeVisitor;
  13. use Symfony\Bridge\Twig\NodeVisitor\TranslationNodeVisitor;
  14. use Twig\Environment;
  15. use Twig\Node\Expression\ArrayExpression;
  16. use Twig\Node\Node;
  17. class TranslationDefaultDomainNodeVisitorTest extends TestCase
  18. {
  19. private static $message = 'message';
  20. private static $domain = 'domain';
  21. /** @dataProvider getDefaultDomainAssignmentTestData */
  22. public function testDefaultDomainAssignment(Node $node)
  23. {
  24. $env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
  25. $visitor = new TranslationDefaultDomainNodeVisitor();
  26. // visit trans_default_domain tag
  27. $defaultDomain = TwigNodeProvider::getTransDefaultDomainTag(self::$domain);
  28. $visitor->enterNode($defaultDomain, $env);
  29. $visitor->leaveNode($defaultDomain, $env);
  30. // visit tested node
  31. $enteredNode = $visitor->enterNode($node, $env);
  32. $leavedNode = $visitor->leaveNode($node, $env);
  33. $this->assertSame($node, $enteredNode);
  34. $this->assertSame($node, $leavedNode);
  35. // extracting tested node messages
  36. $visitor = new TranslationNodeVisitor();
  37. $visitor->enable();
  38. $visitor->enterNode($node, $env);
  39. $visitor->leaveNode($node, $env);
  40. $this->assertEquals(array(array(self::$message, self::$domain)), $visitor->getMessages());
  41. }
  42. /** @dataProvider getDefaultDomainAssignmentTestData */
  43. public function testNewModuleWithoutDefaultDomainTag(Node $node)
  44. {
  45. $env = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock(), array('cache' => false, 'autoescape' => false, 'optimizations' => 0));
  46. $visitor = new TranslationDefaultDomainNodeVisitor();
  47. // visit trans_default_domain tag
  48. $newModule = TwigNodeProvider::getModule('test');
  49. $visitor->enterNode($newModule, $env);
  50. $visitor->leaveNode($newModule, $env);
  51. // visit tested node
  52. $enteredNode = $visitor->enterNode($node, $env);
  53. $leavedNode = $visitor->leaveNode($node, $env);
  54. $this->assertSame($node, $enteredNode);
  55. $this->assertSame($node, $leavedNode);
  56. // extracting tested node messages
  57. $visitor = new TranslationNodeVisitor();
  58. $visitor->enable();
  59. $visitor->enterNode($node, $env);
  60. $visitor->leaveNode($node, $env);
  61. $this->assertEquals(array(array(self::$message, null)), $visitor->getMessages());
  62. }
  63. public function getDefaultDomainAssignmentTestData()
  64. {
  65. return array(
  66. array(TwigNodeProvider::getTransFilter(self::$message)),
  67. array(TwigNodeProvider::getTransChoiceFilter(self::$message)),
  68. array(TwigNodeProvider::getTransTag(self::$message)),
  69. // with named arguments
  70. array(TwigNodeProvider::getTransFilter(self::$message, null, array(
  71. 'arguments' => new ArrayExpression(array(), 0),
  72. ))),
  73. array(TwigNodeProvider::getTransChoiceFilter(self::$message), null, array(
  74. 'arguments' => new ArrayExpression(array(), 0),
  75. )),
  76. );
  77. }
  78. }