PhpExtractorTest.php 3.8KB

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