From 528f29f3d76dbeebc1eeed05836f2b61e4f7f49b Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 20 Aug 2017 14:55:23 +0200 Subject: [PATCH 01/10] Adding test for extract command --- Catalogue/Operation/ReplaceOperation.php | 8 +- .../Functional/Command/ExtractCommandTest.php | 88 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 Tests/Functional/Command/ExtractCommandTest.php diff --git a/Catalogue/Operation/ReplaceOperation.php b/Catalogue/Operation/ReplaceOperation.php index 2e294e2e..ce9c298f 100644 --- a/Catalogue/Operation/ReplaceOperation.php +++ b/Catalogue/Operation/ReplaceOperation.php @@ -48,9 +48,15 @@ protected function processDomain($domain) } foreach ($sourceMessages as $id => $message) { - $this->messages[$domain]['all'][$id] = $message; + if (!empty($message)) { + $this->messages[$domain]['all'][$id] = $message; + } + if (!$this->target->has($id, $domain)) { $this->messages[$domain]['new'][$id] = $message; + + // Make sure to add it to the source if even if empty($message) + $this->messages[$domain]['all'][$id] = $message; } } diff --git a/Tests/Functional/Command/ExtractCommandTest.php b/Tests/Functional/Command/ExtractCommandTest.php new file mode 100644 index 00000000..056d0c9c --- /dev/null +++ b/Tests/Functional/Command/ExtractCommandTest.php @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Bundle\Tests\Functional\Command; + +use Symfony\Bundle\FrameworkBundle\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use Translation\Bundle\Command\ExtractCommand; +use Translation\Bundle\Tests\Functional\BaseTestCase; + +class ExtractCommandTest extends BaseTestCase +{ + protected function setUp() + { + parent::setUp(); + $this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yml'); + + file_put_contents(__DIR__.'/../app/Resources/translations/messages.sv.xlf', <<<'XML' + + + + + + translated.heading + My translated heading + + + + + translated.paragraph0 + My translated paragraph0 + + + + + translated.paragraph1 + My translated paragraph1 + + + + + not.in.source + This is not in the source code + + + + + +XML + ); + } + + public function testExecute() + { + $application = new Application($this->kernel); + + $application->add(new ExtractCommand()); + + $command = $application->find('translation:extract'); + $commandTester = new CommandTester($command); + $commandTester->execute(array( + 'command' => $command->getName(), + 'configuration' => 'app', + 'locale' => 'sv', + )); + + // the output of the command in the console + $output = $commandTester->getDisplay(); + + // Make sure we have 4 new messages + $this->assertRegExp('|New messages +4|s', $output); + $this->assertRegExp('|Total defined messages +8|s', $output); + + $container = $this->getContainer(); + $config = $container->get('php_translation.configuration_manager')->getConfiguration('app'); + $catalogues = $container->get('php_translation.catalogue_fetcher')->getCatalogues($config, ['sv']); + + $this->assertEquals('My translated heading', $catalogues[0]->get('translated.heading')); + } +} From 1ac22a9a4c385cb2ccff943d57af6f186b43a74d Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 20 Aug 2017 15:02:58 +0200 Subject: [PATCH 02/10] Added more tests --- .../Functional/Command/ExtractCommandTest.php | 20 ++++++++++++++++++- Tests/Functional/app/config/normal_config.yml | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Tests/Functional/Command/ExtractCommandTest.php b/Tests/Functional/Command/ExtractCommandTest.php index 056d0c9c..fc7e34bb 100644 --- a/Tests/Functional/Command/ExtractCommandTest.php +++ b/Tests/Functional/Command/ExtractCommandTest.php @@ -14,6 +14,7 @@ use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Tester\CommandTester; use Translation\Bundle\Command\ExtractCommand; +use Translation\Bundle\Model\Metadata; use Translation\Bundle\Tests\Functional\BaseTestCase; class ExtractCommandTest extends BaseTestCase @@ -40,6 +41,9 @@ protected function setUp() + + foobar.html.twig:9 + translated.paragraph1 My translated paragraph1 @@ -83,6 +87,20 @@ public function testExecute() $config = $container->get('php_translation.configuration_manager')->getConfiguration('app'); $catalogues = $container->get('php_translation.catalogue_fetcher')->getCatalogues($config, ['sv']); - $this->assertEquals('My translated heading', $catalogues[0]->get('translated.heading')); + $catalogue = $catalogues[0]; + $this->assertEquals('My translated heading', $catalogue->get('translated.heading'), 'Translated strings MUST NOT disappear.'); + + // Test meta, source-location + $meta = new Metadata($catalogue->getMetadata('translated.paragraph1')); + $this->assertFalse($meta->getState() === 'new'); + foreach ($meta->getSourceLocations() as $sourceLocation) { + $this->assertNotEquals('foobar.html.twig', $sourceLocation['path']); + } + + $meta = new Metadata($catalogue->getMetadata('not.in.source')); + $this->assertTrue($meta->getState() === 'obsolete'); + + $meta = new Metadata($catalogue->getMetadata('translated.title')); + $this->assertTrue($meta->getState() === 'new'); } } diff --git a/Tests/Functional/app/config/normal_config.yml b/Tests/Functional/app/config/normal_config.yml index a6bdd8e1..01f5a04f 100644 --- a/Tests/Functional/app/config/normal_config.yml +++ b/Tests/Functional/app/config/normal_config.yml @@ -8,3 +8,4 @@ translation: app: dirs: ["%test.root_dir%/Resources/views"] output_dir: "%test.root_dir%/Resources/translations" + project_root: "%test.root_dir%" From 1ba122f17eaae71165122fbaa5da9791e381aa2b Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 20 Aug 2017 15:06:58 +0200 Subject: [PATCH 03/10] Improve gitignore for test files --- Tests/Functional/app/Resources/translations/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/Functional/app/Resources/translations/.gitignore b/Tests/Functional/app/Resources/translations/.gitignore index cd4f20e6..1351b683 100644 --- a/Tests/Functional/app/Resources/translations/.gitignore +++ b/Tests/Functional/app/Resources/translations/.gitignore @@ -1 +1,2 @@ messages.sv.xlf +*~ From 69b2058c4b473cb585cf99a5837779d9c901039c Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 20 Aug 2017 15:07:16 +0200 Subject: [PATCH 04/10] Applied changes from StyleCI --- Tests/Functional/Command/ExtractCommandTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/Functional/Command/ExtractCommandTest.php b/Tests/Functional/Command/ExtractCommandTest.php index fc7e34bb..fe303778 100644 --- a/Tests/Functional/Command/ExtractCommandTest.php +++ b/Tests/Functional/Command/ExtractCommandTest.php @@ -70,11 +70,11 @@ public function testExecute() $command = $application->find('translation:extract'); $commandTester = new CommandTester($command); - $commandTester->execute(array( - 'command' => $command->getName(), + $commandTester->execute([ + 'command' => $command->getName(), 'configuration' => 'app', 'locale' => 'sv', - )); + ]); // the output of the command in the console $output = $commandTester->getDisplay(); From dd5b98b48f79e07168155ffdd8cb1d58a6a07620 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 20 Aug 2017 15:18:05 +0200 Subject: [PATCH 05/10] Added test for Status command --- Catalogue/CatalogueCounter.php | 2 + .../Functional/Command/StatusCommandTest.php | 63 +++++++++++++++++++ composer.json | 1 + 3 files changed, 66 insertions(+) create mode 100644 Tests/Functional/Command/StatusCommandTest.php diff --git a/Catalogue/CatalogueCounter.php b/Catalogue/CatalogueCounter.php index 205c4fe5..536861bc 100644 --- a/Catalogue/CatalogueCounter.php +++ b/Catalogue/CatalogueCounter.php @@ -64,6 +64,7 @@ public function getCatalogueStatistics(MessageCatalogueInterface $catalogue) foreach ($domains as $domain) { $result[$domain]['new'] = 0; $result[$domain]['obsolete'] = 0; + $result[$domain]['approved'] = 0; foreach ($catalogue->all($domain) as $key => $text) { $metadata = new Metadata($catalogue->getMetadata($key, $domain)); @@ -85,6 +86,7 @@ public function getCatalogueStatistics(MessageCatalogueInterface $catalogue) // Sum the number of new and obsolete messages. $result['_total']['new'] = 0; $result['_total']['obsolete'] = 0; + $result['_total']['approved'] = 0; foreach ($domains as $domain) { $result['_total']['new'] += $result[$domain]['new']; $result['_total']['obsolete'] += $result[$domain]['obsolete']; diff --git a/Tests/Functional/Command/StatusCommandTest.php b/Tests/Functional/Command/StatusCommandTest.php new file mode 100644 index 00000000..67289601 --- /dev/null +++ b/Tests/Functional/Command/StatusCommandTest.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Bundle\Tests\Functional\Command; + +use Symfony\Bundle\FrameworkBundle\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; +use Translation\Bundle\Command\ExtractCommand; +use Translation\Bundle\Model\Metadata; +use Translation\Bundle\Tests\Functional\BaseTestCase; + +class StatusCommandTest extends BaseTestCase +{ + protected function setUp() + { + parent::setUp(); + $this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yml'); + } + + public function testExecute() + { + $application = new Application($this->kernel); + + $application->add(new ExtractCommand()); + + $command = $application->find('translation:status'); + $commandTester = new CommandTester($command); + $commandTester->execute([ + 'command' => $command->getName(), + 'configuration' => 'app', + 'locale' => 'en', + '--json' => true, + ]); + + // the output of the command in the console + $output = $commandTester->getDisplay(); + $data = json_decode($output, true); + + $this->assertArrayHasKey('en', $data); + $this->assertArrayHasKey('messages', $data['en']); + $this->assertArrayHasKey('_total', $data['en']); + + $total = $data['en']['_total']; + $this->assertArrayHasKey('defined', $total); + $this->assertArrayHasKey('new', $total); + $this->assertArrayHasKey('obsolete', $total); + $this->assertArrayHasKey('approved', $total); + $this->assertEquals(2, $total['defined']); + $this->assertEquals(1, $total['new']); + $this->assertEquals(0, $total['obsolete']); + $this->assertEquals(1, $total['approved']); + + + } +} diff --git a/composer.json b/composer.json index d7713c34..741bf94b 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "php-http/curl-client": "^1.7", "php-http/message": "^1.6", "php-http/message-factory": "^1.0.2", + "symfony/console": "^2.7 || ^3.0", "symfony/twig-bundle": "^2.7 || ^3.0", "symfony/twig-bridge": "^2.7 || ^3.0", "symfony/asset": "^2.7 || ^3.0", From b486f3964a10a81c2c0d660da65dddb1925a30c5 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 20 Aug 2017 15:29:43 +0200 Subject: [PATCH 06/10] Allow user to specify xliff_version --- Catalogue/CatalogueWriter.php | 1 + DependencyInjection/Configuration.php | 3 ++- Model/Configuration.php | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Catalogue/CatalogueWriter.php b/Catalogue/CatalogueWriter.php index 90c7e12e..59ea0aa1 100644 --- a/Catalogue/CatalogueWriter.php +++ b/Catalogue/CatalogueWriter.php @@ -59,6 +59,7 @@ public function writeCatalogues(Configuration $config, array $catalogues) [ 'path' => $config->getOutputDir(), 'default_locale' => $this->defaultLocale, + 'xliff_version' => $config->getXliffVersion(), ] ); } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c22b00c8..cdbfe617 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -158,7 +158,8 @@ private function configsNode(ArrayNodeDefinition $root) ->prototype('scalar')->end() ->end() ->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end() - ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent. ")->end() + ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent.")->end() + ->scalarNode('xliff_version')->info("The version of XLIFF XML you want to use (if dumping to this format).")->defaultValue('2.0')->end() ->variableNode('local_file_storage_options') ->info('Options passed to the local file storage\'s dumper.') ->defaultValue([]) diff --git a/Model/Configuration.php b/Model/Configuration.php index bff1adde..512aaa34 100644 --- a/Model/Configuration.php +++ b/Model/Configuration.php @@ -87,6 +87,11 @@ final class Configuration */ private $whitelistDomains; + /** + * @var string + */ + private $xliffVersion; + /** * @param array $data */ @@ -103,6 +108,7 @@ public function __construct(array $data) $this->outputFormat = $data['output_format']; $this->blacklistDomains = $data['blacklist_domains']; $this->whitelistDomains = $data['whitelist_domains']; + $this->xliffVersion = $data['xliff_version']; } /** @@ -203,4 +209,12 @@ public function getPathsToTranslationFiles() { return array_merge($this->externalTranslationsDirs, [$this->getOutputDir()]); } + + /** + * @return string + */ + public function getXliffVersion() + { + return $this->xliffVersion; + } } From 99a93809ca09ac43de904c33cc159c532773b1be Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 20 Aug 2017 15:30:06 +0200 Subject: [PATCH 07/10] Applied changes from StyleCI --- DependencyInjection/Configuration.php | 2 +- Tests/Functional/Command/StatusCommandTest.php | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index cdbfe617..c6eec9d1 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -159,7 +159,7 @@ private function configsNode(ArrayNodeDefinition $root) ->end() ->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end() ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent.")->end() - ->scalarNode('xliff_version')->info("The version of XLIFF XML you want to use (if dumping to this format).")->defaultValue('2.0')->end() + ->scalarNode('xliff_version')->info('The version of XLIFF XML you want to use (if dumping to this format).')->defaultValue('2.0')->end() ->variableNode('local_file_storage_options') ->info('Options passed to the local file storage\'s dumper.') ->defaultValue([]) diff --git a/Tests/Functional/Command/StatusCommandTest.php b/Tests/Functional/Command/StatusCommandTest.php index 67289601..b64a70a8 100644 --- a/Tests/Functional/Command/StatusCommandTest.php +++ b/Tests/Functional/Command/StatusCommandTest.php @@ -14,7 +14,6 @@ use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Tester\CommandTester; use Translation\Bundle\Command\ExtractCommand; -use Translation\Bundle\Model\Metadata; use Translation\Bundle\Tests\Functional\BaseTestCase; class StatusCommandTest extends BaseTestCase @@ -57,7 +56,5 @@ public function testExecute() $this->assertEquals(1, $total['new']); $this->assertEquals(0, $total['obsolete']); $this->assertEquals(1, $total['approved']); - - } } From 3757b6cd6e6c3589ce67407cb070ff4b8c877c39 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 20 Aug 2017 15:37:30 +0200 Subject: [PATCH 08/10] Bugfix --- Tests/Unit/Model/ConfigurationTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/Unit/Model/ConfigurationTest.php b/Tests/Unit/Model/ConfigurationTest.php index 9f7b92fb..cd8f3f1b 100644 --- a/Tests/Unit/Model/ConfigurationTest.php +++ b/Tests/Unit/Model/ConfigurationTest.php @@ -58,6 +58,7 @@ public static function getDefaultData() 'output_format' => 'getOutputFormat', 'blacklist_domains' => ['getBlacklistDomains'], 'whitelist_domains' => ['getWhitelistDomains'], + 'xliff_version' => ['getXliffVersion'], ]; } } From 2a03f885c1520cb9a1121bd5c6d7a6ea09737cb3 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 20 Aug 2017 15:54:25 +0200 Subject: [PATCH 09/10] Make sure to boot kernel --- Tests/Functional/Command/ExtractCommandTest.php | 1 + Tests/Functional/Command/StatusCommandTest.php | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Tests/Functional/Command/ExtractCommandTest.php b/Tests/Functional/Command/ExtractCommandTest.php index fe303778..cd9f4343 100644 --- a/Tests/Functional/Command/ExtractCommandTest.php +++ b/Tests/Functional/Command/ExtractCommandTest.php @@ -64,6 +64,7 @@ protected function setUp() public function testExecute() { + $this->bootKernel(); $application = new Application($this->kernel); $application->add(new ExtractCommand()); diff --git a/Tests/Functional/Command/StatusCommandTest.php b/Tests/Functional/Command/StatusCommandTest.php index b64a70a8..0038c260 100644 --- a/Tests/Functional/Command/StatusCommandTest.php +++ b/Tests/Functional/Command/StatusCommandTest.php @@ -14,6 +14,7 @@ use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Tester\CommandTester; use Translation\Bundle\Command\ExtractCommand; +use Translation\Bundle\Command\StatusCommand; use Translation\Bundle\Tests\Functional\BaseTestCase; class StatusCommandTest extends BaseTestCase @@ -26,9 +27,10 @@ protected function setUp() public function testExecute() { + $this->bootKernel(); $application = new Application($this->kernel); - $application->add(new ExtractCommand()); + $application->add(new StatusCommand()); $command = $application->find('translation:status'); $commandTester = new CommandTester($command); From 391a4fda0e2e22762e188bda9d4bd9468e5c6265 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 20 Aug 2017 15:55:53 +0200 Subject: [PATCH 10/10] Removed unused import --- Tests/Functional/Command/StatusCommandTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/Functional/Command/StatusCommandTest.php b/Tests/Functional/Command/StatusCommandTest.php index 0038c260..36b32b1a 100644 --- a/Tests/Functional/Command/StatusCommandTest.php +++ b/Tests/Functional/Command/StatusCommandTest.php @@ -13,7 +13,6 @@ use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Tester\CommandTester; -use Translation\Bundle\Command\ExtractCommand; use Translation\Bundle\Command\StatusCommand; use Translation\Bundle\Tests\Functional\BaseTestCase;