4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Tests\Extractor;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Translation\Extractor\PhpExtractor;
16 use Symfony\Component\Translation\MessageCatalogue;
18 class PhpExtractorTest extends TestCase
21 * @dataProvider resourcesProvider
23 * @param array|string $resource
25 public function testExtraction($resource)
28 $extractor = new PhpExtractor();
29 $extractor->setPrefix('prefix');
30 $catalogue = new MessageCatalogue('en');
33 $extractor->extract($resource, $catalogue);
35 $expectedHeredoc = <<<EOF
36 heredoc key with whitespace and escaped \$\n sequences
38 $expectedNowdoc = <<<'EOF'
39 nowdoc key with whitespace and nonescaped \$\n sequences
42 $expectedCatalogue = array(
44 'single-quoted key' => 'prefixsingle-quoted key',
45 'double-quoted key' => 'prefixdouble-quoted key',
46 'heredoc key' => 'prefixheredoc key',
47 'nowdoc key' => 'prefixnowdoc key',
48 "double-quoted key with whitespace and escaped \$\n\" sequences" => "prefixdouble-quoted key with whitespace and escaped \$\n\" sequences",
49 'single-quoted key with whitespace and nonescaped \$\n\' sequences' => 'prefixsingle-quoted key with whitespace and nonescaped \$\n\' sequences',
50 'single-quoted key with "quote mark at the end"' => 'prefixsingle-quoted key with "quote mark at the end"',
51 $expectedHeredoc => 'prefix'.$expectedHeredoc,
52 $expectedNowdoc => 'prefix'.$expectedNowdoc,
53 '{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',
55 'not_messages' => array(
56 'other-domain-test-no-params-short-array' => 'prefixother-domain-test-no-params-short-array',
57 'other-domain-test-no-params-long-array' => 'prefixother-domain-test-no-params-long-array',
58 'other-domain-test-params-short-array' => 'prefixother-domain-test-params-short-array',
59 'other-domain-test-params-long-array' => 'prefixother-domain-test-params-long-array',
60 'other-domain-test-trans-choice-short-array-%count%' => 'prefixother-domain-test-trans-choice-short-array-%count%',
61 'other-domain-test-trans-choice-long-array-%count%' => 'prefixother-domain-test-trans-choice-long-array-%count%',
62 'typecast' => 'prefixtypecast',
63 'msg1' => 'prefixmsg1',
64 'msg2' => 'prefixmsg2',
67 $actualCatalogue = $catalogue->all();
69 $this->assertEquals($expectedCatalogue, $actualCatalogue);
72 public function resourcesProvider()
74 $directory = __DIR__.'/../fixtures/extractor/';
76 foreach (new \DirectoryIterator($directory) as $fileInfo) {
77 if ($fileInfo->isDot()) {
80 if ('translation.html.php' === $fileInfo->getBasename()) {
81 $phpFile = $fileInfo->getPathname();
83 $splFiles[] = $fileInfo->getFileInfo();
89 array(glob($directory.'*')),
91 array(new \ArrayObject(glob($directory.'*'))),
92 array(new \ArrayObject($splFiles)),