Skip to content

Commit

Permalink
Merge pull request #305 from mikadamczyk/ezp-28722-add-unit-tests-to-…
Browse files Browse the repository at this point in the history
…compiler-passes-

EZP-28722: Add unit tests to Compiler Passes
  • Loading branch information
Łukasz Serwatka committed Jan 22, 2018
2 parents 4754bc9 + eab56e7 commit bd679ba
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "~2.7.1",
"phpunit/phpunit": "^6.4"
"phpunit/phpunit": "^6.4",
"matthiasnoback/symfony-dependency-injection-test": "^2.3"
},
"scripts": {
"fix-cs": "@php ./vendor/bin/php-cs-fixer fix -v --show-progress=estimating"
Expand Down
7 changes: 6 additions & 1 deletion src/bundle/DependencyInjection/Compiler/ComponentPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Exception;

class ComponentPass implements CompilerPassInterface
{
const TAG_NAME = 'ezplatform.admin_ui.component';

/**
* You can modify the container here before it is dumped to PHP code.
* @param ContainerBuilder $container
*
* @throws Exception\InvalidArgumentException
* @throws Exception\ServiceNotFoundException
* @throws InvalidArgumentException
*/
public function process(ContainerBuilder $container)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

/**
* {@inheritdoc}
*/
class SystemInfoTabGroupPass implements CompilerPassInterface
{
/**
* @param ContainerBuilder $container
*
* @throws InvalidArgumentException
* @throws ServiceNotFoundException
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition(TabRegistry::class)) {
Expand Down
5 changes: 4 additions & 1 deletion src/bundle/DependencyInjection/Compiler/TabPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class TabPass implements CompilerPassInterface
{
const TAG_TAB = 'ezplatform.tab';

public function process(ContainerBuilder $container)
/**
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition(TabRegistry::class)) {
return;
Expand Down
17 changes: 14 additions & 3 deletions src/bundle/DependencyInjection/Compiler/UiConfigProviderPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,35 @@
use EzSystems\EzPlatformAdminUi\UI\Config\Aggregator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Reference;

/**
* Supplies config Providers to the Aggregator.
*/
class UiConfigProviderPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
const TAG_CONFIG_PROVIDER = 'ezplatform.admin_ui.config_provider';

/**
* @param ContainerBuilder $container
*
* @throws InvalidArgumentException
* @throws ServiceNotFoundException
*/
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition(Aggregator::class)) {
return;
}

$aggregatorDefinition = $container->getDefinition(Aggregator::class);
$taggedServiceIds = $container->findTaggedServiceIds('ezplatform.admin_ui.config_provider');
$taggedServiceIds = $container->findTaggedServiceIds(self::TAG_CONFIG_PROVIDER);

foreach ($taggedServiceIds as $taggedServiceId => $tags) {
foreach ($tags as $tag) {
$key = isset($tag['key']) ? $tag['key'] : $taggedServiceId;
$key = $tag['key'] ?? $taggedServiceId;
$aggregatorDefinition->addMethodCall('addProvider', [$key, new Reference($taggedServiceId)]);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUiBundle\DependencyInjection\Compiler;

use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use EzSystems\EzPlatformAdminUi\Component\Registry;
use Symfony\Component\DependencyInjection\Reference;
use EzSystems\EzPlatformAdminUi\Exception\InvalidArgumentException;

class ComponentPassTest extends AbstractCompilerPassTestCase
{
protected function setUp()
{
parent::setUp();
$this->setDefinition(Registry::class, new Definition());
}

/**
* @param ContainerBuilder $container
*/
protected function registerCompilerPass(ContainerBuilder $container)
{
$container->addCompilerPass(new ComponentPass());
}

public function testProcess()
{
$taggedServiceId = 'collected_service';
$collectedService = new Definition();
$collectedService->addTag(ComponentPass::TAG_NAME, ['group' => 'someGroup']);
$this->setDefinition($taggedServiceId, $collectedService);

$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithTag(
$taggedServiceId,
ComponentPass::TAG_NAME,
['group' => 'someGroup']
);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
Registry::class,
'addComponent',
['someGroup', $taggedServiceId, new Reference($taggedServiceId)]
);
}

public function testProcessWithNoGroup()
{
$taggedServiceId = 'collected_service';

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('Argument \'%s\' is invalid: Tag %s must contain "group" argument.', $taggedServiceId, ComponentPass::TAG_NAME));

$collectedService = new Definition();
$collectedService->addTag(ComponentPass::TAG_NAME);
$this->setDefinition($taggedServiceId, $collectedService);

$this->compile();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUiBundle\DependencyInjection\Compiler;

use EzSystems\EzPlatformAdminUi\Tab\TabGroup;
use EzSystems\EzPlatformAdminUi\Tab\TabRegistry;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class SystemInfoTabGroupPassTest extends AbstractCompilerPassTestCase
{
protected function setUp()
{
parent::setUp();
$this->setDefinition(TabRegistry::class, new Definition());
}

/**
* @param ContainerBuilder $container
*/
protected function registerCompilerPass(ContainerBuilder $container)
{
$container->addCompilerPass(new SystemInfoTabGroupPass());
}

public function testProcess()
{
$tabGroupDefinition = new Definition(TabGroup::class, ['systeminfo']);

$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
TabRegistry::class,
'addTabGroup',
[$tabGroupDefinition]
);
}
}
54 changes: 54 additions & 0 deletions src/bundle/Tests/DependencyInjection/Compiler/TabPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUiBundle\DependencyInjection\Compiler;

use EzSystems\EzPlatformAdminUi\Tab\TabRegistry;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class TabPassTest extends AbstractCompilerPassTestCase
{
protected function setUp()
{
parent::setUp();
$this->setDefinition(TabRegistry::class, new Definition());
}

/**
* @param ContainerBuilder $container
*/
protected function registerCompilerPass(ContainerBuilder $container)
{
$container->addCompilerPass(new TabPass());
}

public function testProcess()
{
$taggedServiceId = 'collected_service';
$collectedService = new Definition();
$collectedService->addTag(TabPass::TAG_TAB, ['group' => 'someGroup']);
$this->setDefinition($taggedServiceId, $collectedService);

$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithTag(
$taggedServiceId,
TabPass::TAG_TAB,
['group' => 'someGroup']
);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
TabRegistry::class,
'addTab',
[new Reference($taggedServiceId), 'someGroup']
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUiBundle\DependencyInjection\Compiler;

use EzSystems\EzPlatformAdminUi\UI\Config\Aggregator;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class UiConfigProviderPassTest extends AbstractCompilerPassTestCase
{
protected function setUp()
{
parent::setUp();
$this->setDefinition(Aggregator::class, new Definition());
}

/**
* @param ContainerBuilder $container
*/
protected function registerCompilerPass(ContainerBuilder $container)
{
$container->addCompilerPass(new UiConfigProviderPass());
}

public function testProcess()
{
$taggedServiceId = 'collected_service';
$collectedService = new Definition();
$collectedService->addTag(UiConfigProviderPass::TAG_CONFIG_PROVIDER, ['key' => 'someKey']);
$this->setDefinition($taggedServiceId, $collectedService);

$taggedServiceWithoutKeyId = 'collected_service_without_key';
$collectedServiceWithoutKey = new Definition();
$collectedServiceWithoutKey->addTag(UiConfigProviderPass::TAG_CONFIG_PROVIDER);
$this->setDefinition($taggedServiceWithoutKeyId, $collectedServiceWithoutKey);

$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithTag(
$taggedServiceId,
UiConfigProviderPass::TAG_CONFIG_PROVIDER,
['key' => 'someKey']
);

$this->assertContainerBuilderHasServiceDefinitionWithTag(
$taggedServiceWithoutKeyId,
UiConfigProviderPass::TAG_CONFIG_PROVIDER
);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
Aggregator::class,
'addProvider',
['someKey', new Reference($taggedServiceId)]
);

$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
Aggregator::class,
'addProvider',
[$taggedServiceWithoutKeyId, new Reference($taggedServiceWithoutKeyId)]
);
}
}

0 comments on commit bd679ba

Please sign in to comment.