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'); 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 new file mode 100644 index 00000000..5ef4e091 --- /dev/null +++ b/Tests/Unit/DependencyInjection/CompilerPass/EditInPlacePassTest.php @@ -0,0 +1,40 @@ + + * + * 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; + +class EditInPlacePassTest extends AbstractCompilerPassTestCase +{ + 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 new file mode 100644 index 00000000..155ae284 --- /dev/null +++ b/Tests/Unit/DependencyInjection/CompilerPass/ExternalTranslatorPassTest.php @@ -0,0 +1,47 @@ + + * + * 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; + +class ExternalTranslatorPassTest extends AbstractCompilerPassTestCase +{ + 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 new file mode 100644 index 00000000..ec02ea7a --- /dev/null +++ b/Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php @@ -0,0 +1,47 @@ + + * + * 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; + +class ExtractorPassTest extends AbstractCompilerPassTestCase +{ + 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 new file mode 100644 index 00000000..102a71d9 --- /dev/null +++ b/Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php @@ -0,0 +1,47 @@ + + * + * 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\StoragePass; + +class StoragePassTest extends AbstractCompilerPassTestCase +{ + 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')] + ); + } +} diff --git a/Tests/Unit/DependencyInjection/TranslationExtensionTest.php b/Tests/Unit/DependencyInjection/TranslationExtensionTest.php new file mode 100644 index 00000000..4dd81b7d --- /dev/null +++ b/Tests/Unit/DependencyInjection/TranslationExtensionTest.php @@ -0,0 +1,90 @@ + + * + * 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; +use Symfony\Component\Translation\DataCollector\TranslationDataCollector; +use Translation\Bundle\DependencyInjection\TranslationExtension; +use Translation\Bundle\EventListener\AutoAddMissingTranslations; +use Translation\Bundle\EventListener\EditInPlaceResponseListener; +use Translation\Bundle\Translator\FallbackTranslator; + +class TranslationExtensionTest extends AbstractExtensionTestCase +{ + protected function getContainerExtensions() + { + $this->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); + } +}