Skip to content

Commit 4f234a4

Browse files
authored
Added test on extension and compiler passes (#82)
* Added tests for TranslationExtension * Added test for empty container on CompilerPasses * Namespace fix * bugfix * Added more tests * cs
1 parent fbc7690 commit 4f234a4

File tree

7 files changed

+276
-1
lines changed

7 files changed

+276
-1
lines changed

DependencyInjection/CompilerPass/ExtractorPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function process(ContainerBuilder $container)
3434
*/
3535
private function getExtractors(ContainerBuilder $container)
3636
{
37+
if (!$container->hasDefinition('php_translation.extractor')) {
38+
return [];
39+
}
40+
3741
/** @var Definition $def */
3842
$def = $container->getDefinition('php_translation.extractor');
3943
$services = $container->findTaggedServiceIds('php_translation.extractor');

DependencyInjection/TranslationExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private function handleConfigNode(ContainerBuilder $container, array $config)
103103
$configurationManager->addMethodCall('addConfiguration', [$name, new Reference($configurationServiceId)]);
104104

105105
/*
106-
* Configure storage service
106+
* Configure storage chain service
107107
*/
108108
$storageDefinition = new DefinitionDecorator('php_translation.storage.abstract');
109109
$storageDefinition->replaceArgument(2, new Reference($configurationServiceId));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\Unit\DependencyInjection\CompilerPass;
13+
14+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Translation\Bundle\DependencyInjection\CompilerPass\EditInPlacePass;
19+
20+
class EditInPlacePassTest extends AbstractCompilerPassTestCase
21+
{
22+
protected function registerCompilerPass(ContainerBuilder $container)
23+
{
24+
$container->addCompilerPass(new EditInPlacePass());
25+
}
26+
27+
public function testReplacement()
28+
{
29+
$def = new Definition();
30+
$this->setDefinition('php_translator.edit_in_place.xtrans_html_translator', $def);
31+
32+
$twigExtension = new Definition();
33+
$twigExtension->addArgument('should_be_replaced');
34+
$this->setDefinition('twig.extension.trans', $twigExtension);
35+
36+
$this->compile();
37+
38+
$this->assertContainerBuilderHasServiceDefinitionWithArgument('twig.extension.trans', 0, new Reference('php_translator.edit_in_place.xtrans_html_translator'));
39+
}
40+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Unit\DependencyInjection\CompilerPass;
13+
14+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Translation\Bundle\DependencyInjection\CompilerPass\ExternalTranslatorPass;
19+
20+
class ExternalTranslatorPassTest extends AbstractCompilerPassTestCase
21+
{
22+
protected function registerCompilerPass(ContainerBuilder $container)
23+
{
24+
$container->addCompilerPass(new ExternalTranslatorPass());
25+
}
26+
27+
/**
28+
* @test
29+
*/
30+
public function if_compiler_pass_collects_services_by_adding_method_calls_these_will_exist()
31+
{
32+
$collectingService = new Definition();
33+
$this->setDefinition('php_translation.translator_service.external_translator', $collectingService);
34+
35+
$collectedService = new Definition();
36+
$collectedService->addTag('php_translation.external_translator');
37+
$this->setDefinition('collected_service', $collectedService);
38+
39+
$this->compile();
40+
41+
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
42+
'php_translation.translator_service.external_translator',
43+
'addTranslatorService',
44+
[new Reference('collected_service')]
45+
);
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Unit\DependencyInjection\CompilerPass;
13+
14+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Translation\Bundle\DependencyInjection\CompilerPass\ExtractorPass;
19+
20+
class ExtractorPassTest extends AbstractCompilerPassTestCase
21+
{
22+
protected function registerCompilerPass(ContainerBuilder $container)
23+
{
24+
$container->addCompilerPass(new ExtractorPass());
25+
}
26+
27+
/**
28+
* @test
29+
*/
30+
public function if_compiler_pass_collects_services_by_adding_method_calls_these_will_exist()
31+
{
32+
$collectingService = new Definition();
33+
$this->setDefinition('php_translation.extractor', $collectingService);
34+
35+
$collectedService = new Definition();
36+
$collectedService->addTag('php_translation.extractor', ['type' => 'html']);
37+
$this->setDefinition('collected_service', $collectedService);
38+
39+
$this->compile();
40+
41+
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
42+
'php_translation.extractor',
43+
'addFileExtractor',
44+
[new Reference('collected_service')]
45+
);
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Unit\DependencyInjection\CompilerPass;
13+
14+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Translation\Bundle\DependencyInjection\CompilerPass\StoragePass;
19+
20+
class StoragePassTest extends AbstractCompilerPassTestCase
21+
{
22+
protected function registerCompilerPass(ContainerBuilder $container)
23+
{
24+
$container->addCompilerPass(new StoragePass());
25+
}
26+
27+
/**
28+
* @test
29+
*/
30+
public function if_compiler_pass_collects_services_by_adding_method_calls_these_will_exist()
31+
{
32+
$collectingService = new Definition();
33+
$this->setDefinition('php_translation.storage.foobar', $collectingService);
34+
35+
$collectedService = new Definition();
36+
$collectedService->addTag('php_translation.storage', ['name' => 'foobar', 'type' => 'remote']);
37+
$this->setDefinition('collected_service', $collectedService);
38+
39+
$this->compile();
40+
41+
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
42+
'php_translation.storage.foobar',
43+
'addRemoteStorage',
44+
[new Reference('collected_service')]
45+
);
46+
}
47+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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\Unit\DependencyInjection;
13+
14+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
15+
use Symfony\Component\Translation\DataCollector\TranslationDataCollector;
16+
use Translation\Bundle\DependencyInjection\TranslationExtension;
17+
use Translation\Bundle\EventListener\AutoAddMissingTranslations;
18+
use Translation\Bundle\EventListener\EditInPlaceResponseListener;
19+
use Translation\Bundle\Translator\FallbackTranslator;
20+
21+
class TranslationExtensionTest extends AbstractExtensionTestCase
22+
{
23+
protected function getContainerExtensions()
24+
{
25+
$this->setParameter('kernel.default_locale', 'ar');
26+
$this->setParameter('kernel.root_dir', __DIR__);
27+
28+
return [
29+
new TranslationExtension(),
30+
];
31+
}
32+
33+
public function testLocales()
34+
{
35+
$locales = ['fr', 'sv'];
36+
$this->load(['locales' => $locales]);
37+
38+
$this->assertContainerBuilderHasParameter('php_translation.locales', $locales);
39+
$this->assertContainerBuilderHasParameter('php_translation.default_locale', 'ar');
40+
}
41+
42+
public function testDefaultLocales()
43+
{
44+
$this->load(['default_locale' => 'sv']);
45+
46+
$this->assertContainerBuilderHasParameter('php_translation.default_locale', 'sv');
47+
}
48+
49+
public function testWebUiEnabled()
50+
{
51+
$this->load(['webui' => ['enabled' => true]]);
52+
53+
$this->assertContainerBuilderHasParameter('php_translation.webui.enabled', true);
54+
}
55+
56+
public function testWebUiDisabled()
57+
{
58+
$this->load(['webui' => ['enabled' => false]]);
59+
60+
$this->assertContainerBuilderHasParameter('php_translation.webui.enabled', false);
61+
}
62+
63+
public function testSymfonyProfilerEnabled()
64+
{
65+
$this->load(['symfony_profiler' => ['enabled' => true]]);
66+
67+
$this->assertContainerBuilderHasService('php_translation.data_collector', TranslationDataCollector::class);
68+
}
69+
70+
public function testEditInPlaceEnabled()
71+
{
72+
$this->load(['edit_in_place' => ['enabled' => true]]);
73+
74+
$this->assertContainerBuilderHasService('php_translation.edit_in_place.response_listener', EditInPlaceResponseListener::class);
75+
}
76+
77+
public function testAutoAddEnabled()
78+
{
79+
$this->load(['auto_add_missing_translations' => ['enabled' => true]]);
80+
81+
$this->assertContainerBuilderHasService('php_translator.auto_adder', AutoAddMissingTranslations::class);
82+
}
83+
84+
public function testFallbackTranslationEnabled()
85+
{
86+
$this->load(['fallback_translation' => ['enabled' => true]]);
87+
88+
$this->assertContainerBuilderHasService('php_translator.fallback_translator', FallbackTranslator::class);
89+
}
90+
}

0 commit comments

Comments
 (0)