PhpExtractorTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. ],
  50. 'not_messages' => [
  51. 'other-domain-test-no-params-short-array' => 'prefixother-domain-test-no-params-short-array',
  52. 'other-domain-test-no-params-long-array' => 'prefixother-domain-test-no-params-long-array',
  53. 'other-domain-test-params-short-array' => 'prefixother-domain-test-params-short-array',
  54. 'other-domain-test-params-long-array' => 'prefixother-domain-test-params-long-array',
  55. 'other-domain-test-trans-choice-short-array-%count%' => 'prefixother-domain-test-trans-choice-short-array-%count%',
  56. 'other-domain-test-trans-choice-long-array-%count%' => 'prefixother-domain-test-trans-choice-long-array-%count%',
  57. 'typecast' => 'prefixtypecast',
  58. 'msg1' => 'prefixmsg1',
  59. 'msg2' => 'prefixmsg2',
  60. ],
  61. ];
  62. $actualCatalogue = $catalogue->all();
  63. $this->assertEquals($expectedCatalogue, $actualCatalogue);
  64. }
  65. public function resourcesProvider()
  66. {
  67. $directory = __DIR__.'/../fixtures/extractor/';
  68. $splFiles = [];
  69. foreach (new \DirectoryIterator($directory) as $fileInfo) {
  70. if ($fileInfo->isDot()) {
  71. continue;
  72. }
  73. if ('translation.html.php' === $fileInfo->getBasename()) {
  74. $phpFile = $fileInfo->getPathname();
  75. }
  76. $splFiles[] = $fileInfo->getFileInfo();
  77. }
  78. return [
  79. [$directory],
  80. [$phpFile],
  81. [glob($directory.'*')],
  82. [$splFiles],
  83. [new \ArrayObject(glob($directory.'*'))],
  84. [new \ArrayObject($splFiles)],
  85. ];
  86. }
  87. }