Skip to content

Added test on extension and compiler passes #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DependencyInjection/CompilerPass/ExtractorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* 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'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* 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')]
);
}
}
47 changes: 47 additions & 0 deletions Tests/Unit/DependencyInjection/CompilerPass/ExtractorPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* 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')]
);
}
}
47 changes: 47 additions & 0 deletions Tests/Unit/DependencyInjection/CompilerPass/StoragePassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* 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')]
);
}
}
90 changes: 90 additions & 0 deletions Tests/Unit/DependencyInjection/TranslationExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* 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);
}
}