TwigExtractorTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\Translation;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  13. use Symfony\Bridge\Twig\Translation\TwigExtractor;
  14. use Symfony\Component\Translation\MessageCatalogue;
  15. use Twig\Environment;
  16. use Twig\Error\Error;
  17. use Twig\Loader\ArrayLoader;
  18. class TwigExtractorTest extends TestCase
  19. {
  20. /**
  21. * @dataProvider getExtractData
  22. */
  23. public function testExtract($template, $messages)
  24. {
  25. $loader = $this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock();
  26. $twig = new Environment($loader, array(
  27. 'strict_variables' => true,
  28. 'debug' => true,
  29. 'cache' => false,
  30. 'autoescape' => false,
  31. ));
  32. $twig->addExtension(new TranslationExtension($this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock()));
  33. $extractor = new TwigExtractor($twig);
  34. $extractor->setPrefix('prefix');
  35. $catalogue = new MessageCatalogue('en');
  36. $m = new \ReflectionMethod($extractor, 'extractTemplate');
  37. $m->setAccessible(true);
  38. $m->invoke($extractor, $template, $catalogue);
  39. foreach ($messages as $key => $domain) {
  40. $this->assertTrue($catalogue->has($key, $domain));
  41. $this->assertEquals('prefix'.$key, $catalogue->get($key, $domain));
  42. }
  43. }
  44. public function getExtractData()
  45. {
  46. return array(
  47. array('{{ "new key" | trans() }}', array('new key' => 'messages')),
  48. array('{{ "new key" | trans() | upper }}', array('new key' => 'messages')),
  49. array('{{ "new key" | trans({}, "domain") }}', array('new key' => 'domain')),
  50. array('{{ "new key" | transchoice(1) }}', array('new key' => 'messages')),
  51. array('{{ "new key" | transchoice(1) | upper }}', array('new key' => 'messages')),
  52. array('{{ "new key" | transchoice(1, {}, "domain") }}', array('new key' => 'domain')),
  53. array('{% trans %}new key{% endtrans %}', array('new key' => 'messages')),
  54. array('{% trans %} new key {% endtrans %}', array('new key' => 'messages')),
  55. array('{% trans from "domain" %}new key{% endtrans %}', array('new key' => 'domain')),
  56. array('{% set foo = "new key" | trans %}', array('new key' => 'messages')),
  57. array('{{ 1 ? "new key" | trans : "another key" | trans }}', array('new key' => 'messages', 'another key' => 'messages')),
  58. // make sure 'trans_default_domain' tag is supported
  59. array('{% trans_default_domain "domain" %}{{ "new key"|trans }}', array('new key' => 'domain')),
  60. array('{% trans_default_domain "domain" %}{{ "new key"|transchoice }}', array('new key' => 'domain')),
  61. array('{% trans_default_domain "domain" %}{% trans %}new key{% endtrans %}', array('new key' => 'domain')),
  62. // make sure this works with twig's named arguments
  63. array('{{ "new key" | trans(domain="domain") }}', array('new key' => 'domain')),
  64. array('{{ "new key" | transchoice(domain="domain", count=1) }}', array('new key' => 'domain')),
  65. );
  66. }
  67. /**
  68. * @expectedException \Twig\Error\Error
  69. * @dataProvider resourcesWithSyntaxErrorsProvider
  70. */
  71. public function testExtractSyntaxError($resources)
  72. {
  73. $twig = new Environment($this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock());
  74. $twig->addExtension(new TranslationExtension($this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock()));
  75. $extractor = new TwigExtractor($twig);
  76. try {
  77. $extractor->extract($resources, new MessageCatalogue('en'));
  78. } catch (Error $e) {
  79. if (method_exists($e, 'getSourceContext')) {
  80. $this->assertSame(dirname(__DIR__).strtr('/Fixtures/extractor/syntax_error.twig', '/', DIRECTORY_SEPARATOR), $e->getFile());
  81. $this->assertSame(1, $e->getLine());
  82. $this->assertSame('Unclosed "block".', $e->getMessage());
  83. } else {
  84. $this->expectExceptionMessageRegExp('/Unclosed "block" in ".*extractor(\\/|\\\\)syntax_error\\.twig" at line 1/');
  85. }
  86. throw $e;
  87. }
  88. }
  89. /**
  90. * @return array
  91. */
  92. public function resourcesWithSyntaxErrorsProvider()
  93. {
  94. return array(
  95. array(__DIR__.'/../Fixtures'),
  96. array(__DIR__.'/../Fixtures/extractor/syntax_error.twig'),
  97. array(new \SplFileInfo(__DIR__.'/../Fixtures/extractor/syntax_error.twig')),
  98. );
  99. }
  100. /**
  101. * @dataProvider resourceProvider
  102. */
  103. public function testExtractWithFiles($resource)
  104. {
  105. $loader = new ArrayLoader(array());
  106. $twig = new Environment($loader, array(
  107. 'strict_variables' => true,
  108. 'debug' => true,
  109. 'cache' => false,
  110. 'autoescape' => false,
  111. ));
  112. $twig->addExtension(new TranslationExtension($this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')->getMock()));
  113. $extractor = new TwigExtractor($twig);
  114. $catalogue = new MessageCatalogue('en');
  115. $extractor->extract($resource, $catalogue);
  116. $this->assertTrue($catalogue->has('Hi!', 'messages'));
  117. $this->assertEquals('Hi!', $catalogue->get('Hi!', 'messages'));
  118. }
  119. /**
  120. * @return array
  121. */
  122. public function resourceProvider()
  123. {
  124. $directory = __DIR__.'/../Fixtures/extractor/';
  125. return array(
  126. array($directory.'with_translations.html.twig'),
  127. array(array($directory.'with_translations.html.twig')),
  128. array(array(new \SplFileInfo($directory.'with_translations.html.twig'))),
  129. array(new \ArrayObject(array($directory.'with_translations.html.twig'))),
  130. array(new \ArrayObject(array(new \SplFileInfo($directory.'with_translations.html.twig')))),
  131. );
  132. }
  133. }