From d2d1f56df1e6c4452587c32ccff801d9ae952788 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 12 Mar 2017 08:44:13 +0100 Subject: [PATCH 1/6] Added tests for TranslationExtension --- .../TranslationExtensionTest.php | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Tests/Unit/DependencyInjection/TranslationExtensionTest.php diff --git a/Tests/Unit/DependencyInjection/TranslationExtensionTest.php b/Tests/Unit/DependencyInjection/TranslationExtensionTest.php new file mode 100644 index 00000000..45fa3c7d --- /dev/null +++ b/Tests/Unit/DependencyInjection/TranslationExtensionTest.php @@ -0,0 +1,81 @@ +setParameter('kernel.default_locale', 'ar'); + $this->setParameter('kernel.root_dir', __DIR__); + + return [ + new TranslationExtension(), + ]; + } + + public function testLocales() + { + $locales = ['fr', 'sv']; + $this->load(['locales' => $locales]); + + $this->assertContainerBuilderHasParameter('php_translation.locales', $locales); + $this->assertContainerBuilderHasParameter('php_translation.default_locale', 'ar'); + } + + public function testDefaultLocales() + { + $this->load(['default_locale' => 'sv']); + + $this->assertContainerBuilderHasParameter('php_translation.default_locale', 'sv'); + } + + public function testWebUiEnabled() + { + $this->load(['webui' => ['enabled' => true]]); + + $this->assertContainerBuilderHasParameter('php_translation.webui.enabled', true); + } + + public function testWebUiDisabled() + { + $this->load(['webui' => ['enabled' => false]]); + + $this->assertContainerBuilderHasParameter('php_translation.webui.enabled', false); + } + + public function testSymfonyProfilerEnabled() + { + $this->load(['symfony_profiler' => ['enabled' => true]]); + + $this->assertContainerBuilderHasService('php_translation.data_collector', TranslationDataCollector::class); + } + + public function testEditInPlaceEnabled() + { + $this->load(['edit_in_place' => ['enabled' => true]]); + + $this->assertContainerBuilderHasService('php_translation.edit_in_place.response_listener', EditInPlaceResponseListener::class); + } + + public function testAutoAddEnabled() + { + $this->load(['auto_add_missing_translations' => ['enabled' => true]]); + + $this->assertContainerBuilderHasService('php_translator.auto_adder', AutoAddMissingTranslations::class); + } + + public function testFallbackTranslationEnabled() + { + $this->load(['fallback_translation' => ['enabled' => true]]); + + $this->assertContainerBuilderHasService('php_translator.fallback_translator', FallbackTranslator::class); + } +} From 5bd6d62b2dbc6b424347695bd16796b9642e33a6 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 12 Mar 2017 08:51:00 +0100 Subject: [PATCH 2/6] Added test for empty container on CompilerPasses --- .../CompilerPass/EditInPlacePassTest.php | 18 ++++++++++++++++++ .../ExternalTranslatorPassTest.php | 18 ++++++++++++++++++ .../CompilerPass/ExtractorPassTest.php | 18 ++++++++++++++++++ .../CompilerPass/StoragePassTest.php | 17 +++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php create mode 100644 Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php create mode 100644 Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php create mode 100644 Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php diff --git a/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php new file mode 100644 index 00000000..c92240e8 --- /dev/null +++ b/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php @@ -0,0 +1,18 @@ +addCompilerPass(new EditInPlacePass()); + } +} diff --git a/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php new file mode 100644 index 00000000..12e8b872 --- /dev/null +++ b/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php @@ -0,0 +1,18 @@ +addCompilerPass(new ExternalTranslatorPass()); + } +} diff --git a/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php new file mode 100644 index 00000000..4d4ee9b5 --- /dev/null +++ b/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php @@ -0,0 +1,18 @@ +addCompilerPass(new ExtractorPass()); + } +} diff --git a/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php new file mode 100644 index 00000000..c827f1b4 --- /dev/null +++ b/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php @@ -0,0 +1,17 @@ +addCompilerPass(new StoragePass()); + } +} From 712197cad834b0610b746349c9d00d637a64e617 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 12 Mar 2017 08:51:10 +0100 Subject: [PATCH 3/6] Namespace fix --- Tests/Unit/DependencyInjection/TranslationExtensionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Unit/DependencyInjection/TranslationExtensionTest.php b/Tests/Unit/DependencyInjection/TranslationExtensionTest.php index 45fa3c7d..1cc7173b 100644 --- a/Tests/Unit/DependencyInjection/TranslationExtensionTest.php +++ b/Tests/Unit/DependencyInjection/TranslationExtensionTest.php @@ -1,6 +1,6 @@ Date: Sun, 12 Mar 2017 08:51:15 +0100 Subject: [PATCH 4/6] bugfix --- DependencyInjection/CompilerPass/ExtractorPass.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DependencyInjection/CompilerPass/ExtractorPass.php b/DependencyInjection/CompilerPass/ExtractorPass.php index 87929d16..8cd3ecde 100644 --- a/DependencyInjection/CompilerPass/ExtractorPass.php +++ b/DependencyInjection/CompilerPass/ExtractorPass.php @@ -34,6 +34,10 @@ public function process(ContainerBuilder $container) */ private function getExtractors(ContainerBuilder $container) { + if (!$container->hasDefinition('php_translation.extractor')) { + return []; + } + /** @var Definition $def */ $def = $container->getDefinition('php_translation.extractor'); $services = $container->findTaggedServiceIds('php_translation.extractor'); From d0b6f87983012b810d21b064173aa12e978f3778 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 12 Mar 2017 09:24:49 +0100 Subject: [PATCH 5/6] Added more tests --- DependencyInjection/TranslationExtension.php | 2 +- .../CompilerPass/EditInPlacePassTest.php | 16 +++++++++++++ .../ExternalTranslatorPassTest.php | 24 +++++++++++++++++++ .../CompilerPass/ExtractorPassTest.php | 23 ++++++++++++++++++ .../CompilerPass/StoragePassTest.php | 23 ++++++++++++++++++ 5 files changed, 87 insertions(+), 1 deletion(-) diff --git a/DependencyInjection/TranslationExtension.php b/DependencyInjection/TranslationExtension.php index da1a6514..43cd1274 100644 --- a/DependencyInjection/TranslationExtension.php +++ b/DependencyInjection/TranslationExtension.php @@ -103,7 +103,7 @@ private function handleConfigNode(ContainerBuilder $container, array $config) $configurationManager->addMethodCall('addConfiguration', [$name, new Reference($configurationServiceId)]); /* - * Configure storage service + * Configure storage chain service */ $storageDefinition = new DefinitionDecorator('php_translation.storage.abstract'); $storageDefinition->replaceArgument(2, new Reference($configurationServiceId)); diff --git a/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php index c92240e8..c9f8510e 100644 --- a/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php +++ b/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php @@ -6,6 +6,8 @@ use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Reference; use Translation\Bundle\DependencyInjection\CompilerPass\EditInPlacePass; use Translation\Bundle\DependencyInjection\CompilerPass\StoragePass; @@ -15,4 +17,18 @@ protected function registerCompilerPass(ContainerBuilder $container) { $container->addCompilerPass(new EditInPlacePass()); } + + public function testReplacement() + { + $def = new Definition(); + $this->setDefinition('php_translator.edit_in_place.xtrans_html_translator', $def); + + $twigExtension = new Definition(); + $twigExtension->addArgument('should_be_replaced'); + $this->setDefinition('twig.extension.trans', $twigExtension); + + $this->compile(); + + $this->assertContainerBuilderHasServiceDefinitionWithArgument('twig.extension.trans', 0, new Reference('php_translator.edit_in_place.xtrans_html_translator')); + } } diff --git a/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php index 12e8b872..c96f92d1 100644 --- a/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php +++ b/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php @@ -6,6 +6,8 @@ use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Reference; use Translation\Bundle\DependencyInjection\CompilerPass\ExternalTranslatorPass; use Translation\Bundle\DependencyInjection\CompilerPass\StoragePass; @@ -15,4 +17,26 @@ protected function registerCompilerPass(ContainerBuilder $container) { $container->addCompilerPass(new ExternalTranslatorPass()); } + + + /** + * @test + */ + public function if_compiler_pass_collects_services_by_adding_method_calls_these_will_exist() + { + $collectingService = new Definition(); + $this->setDefinition('php_translation.translator_service.external_translator', $collectingService); + + $collectedService = new Definition(); + $collectedService->addTag('php_translation.external_translator'); + $this->setDefinition('collected_service', $collectedService); + + $this->compile(); + + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( + 'php_translation.translator_service.external_translator', + 'addTranslatorService', + [new Reference('collected_service')] + ); + } } diff --git a/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php index 4d4ee9b5..9fa5118b 100644 --- a/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php +++ b/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php @@ -6,6 +6,8 @@ use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Reference; use Translation\Bundle\DependencyInjection\CompilerPass\ExtractorPass; use Translation\Bundle\DependencyInjection\CompilerPass\StoragePass; @@ -15,4 +17,25 @@ protected function registerCompilerPass(ContainerBuilder $container) { $container->addCompilerPass(new ExtractorPass()); } + + /** + * @test + */ + public function if_compiler_pass_collects_services_by_adding_method_calls_these_will_exist() + { + $collectingService = new Definition(); + $this->setDefinition('php_translation.extractor', $collectingService); + + $collectedService = new Definition(); + $collectedService->addTag('php_translation.extractor', ['type'=>'html']); + $this->setDefinition('collected_service', $collectedService); + + $this->compile(); + + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( + 'php_translation.extractor', + 'addFileExtractor', + [new Reference('collected_service')] + ); + } } diff --git a/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php index c827f1b4..309827e7 100644 --- a/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php +++ b/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php @@ -6,6 +6,8 @@ use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Reference; use Translation\Bundle\DependencyInjection\CompilerPass\StoragePass; class StoragePassTest extends AbstractCompilerPassTestCase @@ -14,4 +16,25 @@ protected function registerCompilerPass(ContainerBuilder $container) { $container->addCompilerPass(new StoragePass()); } + + /** + * @test + */ + public function if_compiler_pass_collects_services_by_adding_method_calls_these_will_exist() + { + $collectingService = new Definition(); + $this->setDefinition('php_translation.storage.foobar', $collectingService); + + $collectedService = new Definition(); + $collectedService->addTag('php_translation.storage', ['name'=>'foobar', 'type'=>'remote']); + $this->setDefinition('collected_service', $collectedService); + + $this->compile(); + + $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( + 'php_translation.storage.foobar', + 'addRemoteStorage', + [new Reference('collected_service')] + ); + } } From 8d97a9d5cce3af89567b90aff12daf2810e9c355 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 12 Mar 2017 09:25:38 +0100 Subject: [PATCH 6/6] cs --- .../CompilerPass/EditInPlacePassTest.php | 12 +++++++++--- .../CompilerPass/ExternalTranslatorPassTest.php | 13 +++++++++---- .../CompilerPass/ExtractorPassTest.php | 14 ++++++++++---- .../CompilerPass/StoragePassTest.php | 13 ++++++++++--- .../TranslationExtensionTest.php | 9 +++++++++ 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php index c9f8510e..5ef4e091 100644 --- a/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php +++ b/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php @@ -1,15 +1,21 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Translation\Bundle\Tests\Unit\DependencyInjection\CompilerPass; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; use Translation\Bundle\DependencyInjection\CompilerPass\EditInPlacePass; -use Translation\Bundle\DependencyInjection\CompilerPass\StoragePass; class EditInPlacePassTest extends AbstractCompilerPassTestCase { diff --git a/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php index c96f92d1..155ae284 100644 --- a/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php +++ b/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php @@ -1,15 +1,21 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Translation\Bundle\Tests\Unit\DependencyInjection\CompilerPass; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; use Translation\Bundle\DependencyInjection\CompilerPass\ExternalTranslatorPass; -use Translation\Bundle\DependencyInjection\CompilerPass\StoragePass; class ExternalTranslatorPassTest extends AbstractCompilerPassTestCase { @@ -18,7 +24,6 @@ protected function registerCompilerPass(ContainerBuilder $container) $container->addCompilerPass(new ExternalTranslatorPass()); } - /** * @test */ diff --git a/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php index 9fa5118b..ec02ea7a 100644 --- a/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php +++ b/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php @@ -1,15 +1,21 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Translation\Bundle\Tests\Unit\DependencyInjection\CompilerPass; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; use Translation\Bundle\DependencyInjection\CompilerPass\ExtractorPass; -use Translation\Bundle\DependencyInjection\CompilerPass\StoragePass; class ExtractorPassTest extends AbstractCompilerPassTestCase { @@ -27,7 +33,7 @@ public function if_compiler_pass_collects_services_by_adding_method_calls_these_ $this->setDefinition('php_translation.extractor', $collectingService); $collectedService = new Definition(); - $collectedService->addTag('php_translation.extractor', ['type'=>'html']); + $collectedService->addTag('php_translation.extractor', ['type' => 'html']); $this->setDefinition('collected_service', $collectedService); $this->compile(); diff --git a/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php b/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php index 309827e7..102a71d9 100644 --- a/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php +++ b/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php @@ -1,8 +1,15 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +namespace Translation\Bundle\Tests\Unit\DependencyInjection\CompilerPass; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -26,7 +33,7 @@ public function if_compiler_pass_collects_services_by_adding_method_calls_these_ $this->setDefinition('php_translation.storage.foobar', $collectingService); $collectedService = new Definition(); - $collectedService->addTag('php_translation.storage', ['name'=>'foobar', 'type'=>'remote']); + $collectedService->addTag('php_translation.storage', ['name' => 'foobar', 'type' => 'remote']); $this->setDefinition('collected_service', $collectedService); $this->compile(); diff --git a/Tests/Unit/DependencyInjection/TranslationExtensionTest.php b/Tests/Unit/DependencyInjection/TranslationExtensionTest.php index 1cc7173b..4dd81b7d 100644 --- a/Tests/Unit/DependencyInjection/TranslationExtensionTest.php +++ b/Tests/Unit/DependencyInjection/TranslationExtensionTest.php @@ -1,5 +1,14 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace Translation\Bundle\Tests\Unit\DependencyInjection; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;