|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the PHP Translation package. |
| 5 | + * |
| 6 | + * (c) PHP Translation team <tobias.nyholm@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Translation\Bundle\Tests\Functional\Command; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 15 | +use Symfony\Component\Console\Tester\CommandTester; |
| 16 | +use Translation\Bundle\Command\ExtractCommand; |
| 17 | +use Translation\Bundle\Model\Metadata; |
| 18 | +use Translation\Bundle\Tests\Functional\BaseTestCase; |
| 19 | + |
| 20 | +class ExtractCommandTest extends BaseTestCase |
| 21 | +{ |
| 22 | + protected function setUp() |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + $this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yml'); |
| 26 | + |
| 27 | + file_put_contents(__DIR__.'/../app/Resources/translations/messages.sv.xlf', <<<'XML' |
| 28 | +<?xml version="1.0" encoding="utf-8"?> |
| 29 | +<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="fr-FR" trgLang="en-US"> |
| 30 | + <file id="messages.en_US"> |
| 31 | + <unit id="xx1"> |
| 32 | + <segment> |
| 33 | + <source>translated.heading</source> |
| 34 | + <target>My translated heading</target> |
| 35 | + </segment> |
| 36 | + </unit> |
| 37 | + <unit id="xx2"> |
| 38 | + <segment> |
| 39 | + <source>translated.paragraph0</source> |
| 40 | + <target>My translated paragraph0</target> |
| 41 | + </segment> |
| 42 | + </unit> |
| 43 | + <unit id="xx3"> |
| 44 | + <notes> |
| 45 | + <note category="file-source" priority="1">foobar.html.twig:9</note> |
| 46 | + </notes> |
| 47 | + <segment> |
| 48 | + <source>translated.paragraph1</source> |
| 49 | + <target>My translated paragraph1</target> |
| 50 | + </segment> |
| 51 | + </unit> |
| 52 | + <unit id="xx4"> |
| 53 | + <segment> |
| 54 | + <source>not.in.source</source> |
| 55 | + <target>This is not in the source code</target> |
| 56 | + </segment> |
| 57 | + </unit> |
| 58 | + </file> |
| 59 | +</xliff> |
| 60 | + |
| 61 | +XML |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function testExecute() |
| 66 | + { |
| 67 | + $this->bootKernel(); |
| 68 | + $application = new Application($this->kernel); |
| 69 | + |
| 70 | + $application->add(new ExtractCommand()); |
| 71 | + |
| 72 | + $command = $application->find('translation:extract'); |
| 73 | + $commandTester = new CommandTester($command); |
| 74 | + $commandTester->execute([ |
| 75 | + 'command' => $command->getName(), |
| 76 | + 'configuration' => 'app', |
| 77 | + 'locale' => 'sv', |
| 78 | + ]); |
| 79 | + |
| 80 | + // the output of the command in the console |
| 81 | + $output = $commandTester->getDisplay(); |
| 82 | + |
| 83 | + // Make sure we have 4 new messages |
| 84 | + $this->assertRegExp('|New messages +4|s', $output); |
| 85 | + $this->assertRegExp('|Total defined messages +8|s', $output); |
| 86 | + |
| 87 | + $container = $this->getContainer(); |
| 88 | + $config = $container->get('php_translation.configuration_manager')->getConfiguration('app'); |
| 89 | + $catalogues = $container->get('php_translation.catalogue_fetcher')->getCatalogues($config, ['sv']); |
| 90 | + |
| 91 | + $catalogue = $catalogues[0]; |
| 92 | + $this->assertEquals('My translated heading', $catalogue->get('translated.heading'), 'Translated strings MUST NOT disappear.'); |
| 93 | + |
| 94 | + // Test meta, source-location |
| 95 | + $meta = new Metadata($catalogue->getMetadata('translated.paragraph1')); |
| 96 | + $this->assertFalse($meta->getState() === 'new'); |
| 97 | + foreach ($meta->getSourceLocations() as $sourceLocation) { |
| 98 | + $this->assertNotEquals('foobar.html.twig', $sourceLocation['path']); |
| 99 | + } |
| 100 | + |
| 101 | + $meta = new Metadata($catalogue->getMetadata('not.in.source')); |
| 102 | + $this->assertTrue($meta->getState() === 'obsolete'); |
| 103 | + |
| 104 | + $meta = new Metadata($catalogue->getMetadata('translated.title')); |
| 105 | + $this->assertTrue($meta->getState() === 'new'); |
| 106 | + } |
| 107 | +} |
0 commit comments