PhpExtractorTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\Component\Translation\Tests\Extractor;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Translation\Extractor\PhpExtractor;
  13. use Symfony\Component\Translation\MessageCatalogue;
  14. class PhpExtractorTest extends TestCase
  15. {
  16. /**
  17. * @dataProvider resourcesProvider
  18. *
  19. * @param array|string $resource
  20. */
  21. public function testExtraction($resource)
  22. {
  23. // Arrange
  24. $extractor = new PhpExtractor();
  25. $extractor->setPrefix('prefix');
  26. $catalogue = new MessageCatalogue('en');
  27. // Act
  28. $extractor->extract($resource, $catalogue);
  29. $expectedHeredoc = <<<EOF
  30. heredoc key with whitespace and escaped \$\n sequences
  31. EOF;
  32. $expectedNowdoc = <<<'EOF'
  33. nowdoc key with whitespace and nonescaped \$\n sequences
  34. EOF;
  35. // Assert
  36. $expectedCatalogue = [
  37. 'messages' => [
  38. 'single-quoted key' => 'prefixsingle-quoted key',
  39. 'double-quoted key' => 'prefixdouble-quoted key',
  40. 'heredoc key' => 'prefixheredoc key',
  41. 'nowdoc key' => 'prefixnowdoc key',
  42. "double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixdouble-quoted key with whitespace and escaped \$\n\" sequences",
  43. 'single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixsingle-quoted key with whitespace and nonescaped \$\n\' sequences',
  44. 'single-quoted key with "quote mark at the end"' => 'prefixsingle-quoted key with "quote mark at the end"',
  45. $expectedHeredoc => 'prefix'.$expectedHeredoc,
  46. $expectedNowdoc => 'prefix'.$expectedNowdoc,
  47. '{0} There is no apples|{1} There is one apple|]1,Inf[ There are %count% apples' => 'prefix{0} There is no apples|{1} There is one apple|]1,Inf[ There are %count% apples',
  48. 'concatenated message with heredoc and nowdoc' => 'prefixconcatenated message with heredoc and nowdoc',
  49. 'default domain' => 'prefixdefault domain',
  50. ],
  51. 'not_messages' => [
  52. 'other-domain-test-no-params-short-array' => 'prefixother-domain-test-no-params-short-array',
  53. 'other-domain-test-no-params-long-array' => 'prefixother-domain-test-no-params-long-array',
  54. 'other-domain-test-params-short-array' => 'prefixother-domain-test-params-short-array',
  55. 'other-domain-test-params-long-array' => 'prefixother-domain-test-params-long-array',
  56. 'other-domain-test-trans-choice-short-array-%count%' => 'prefixother-domain-test-trans-choice-short-array-%count%',
  57. 'other-domain-test-trans-choice-long-array-%count%' => 'prefixother-domain-test-trans-choice-long-array-%count%',
  58. 'typecast' => 'prefixtypecast',
  59. 'msg1' => 'prefixmsg1',
  60. 'msg2' => 'prefixmsg2',
  61. ],
  62. ];
  63. $actualCatalogue = $catalogue->all();
  64. $this->assertEquals($expectedCatalogue, $actualCatalogue);
  65. $filename = str_replace(\DIRECTORY_SEPARATOR, '/', __DIR__).'/../fixtures/extractor/translation.html.php';
  66. $this->assertEquals(['sources' => [$filename.':2']], $catalogue->getMetadata('single-quoted key'));
  67. $this->assertEquals(['sources' => [$filename.':43']], $catalogue->getMetadata('other-domain-test-no-params-short-array', 'not_messages'));
  68. }
  69. public function resourcesProvider()
  70. {
  71. $directory = __DIR__.'/../fixtures/extractor/';
  72. $splFiles = [];
  73. foreach (new \DirectoryIterator($directory) as $fileInfo) {
  74. if ($fileInfo->isDot()) {
  75. continue;
  76. }
  77. if ('translation.html.php' === $fileInfo->getBasename()) {
  78. $phpFile = $fileInfo->getPathname();
  79. }
  80. $splFiles[] = $fileInfo->getFileInfo();
  81. }
  82. return [
  83. [$directory],
  84. [$phpFile],
  85. [glob($directory.'*')],
  86. [$splFiles],
  87. [new \ArrayObject(glob($directory.'*'))],
  88. [new \ArrayObject($splFiles)],
  89. ];
  90. }
  91. }