Skip to content
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
5 changes: 2 additions & 3 deletions DependencyInjection/Compiler/TaggedServiceMappingPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Overblog\GraphQLBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

Expand Down Expand Up @@ -43,9 +42,9 @@ public function process(ContainerBuilder $container)
foreach ($mapping as $name => $options) {
$cleanOptions = $options;
$solutionID = $options['id'];
$solution = $container->get($solutionID);

if ($solution instanceof ContainerAwareInterface) {
$definition = $container->findDefinition($solutionID);
if (is_subclass_of($definition->getClass(), 'Symfony\Component\DependencyInjection\ContainerAwareInterface')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this. Using the ::class keywords would be better.

if (is_subclass_of($definition->getClass(), ContainerAwareInterface::class)) {

$solutionDefinition = $container->findDefinition($options['id']);
$solutionDefinition->addMethodCall('setContainer', [new Reference('service_container')]);
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/DependencyInjection/Compiler/FakeCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Overblog\GraphQLBundle\Tests\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Class FakeCompilerPass
* @package DependencyInjection\Compiler
*/
class FakeCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$container
->getDefinition('test_resolver')
->addArgument(new Reference('injected_service'))
;
}
}
14 changes: 14 additions & 0 deletions Tests/DependencyInjection/Compiler/FakeInjectedService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Overblog\GraphQLBundle\Tests\DependencyInjection\Compiler;

/**
* Class FakeInjectedService
*/
class FakeInjectedService
{
public function doSomething()
{
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Overblog\GraphQLBundle\Tests\DependencyInjection\Compiler;

use Overblog\GraphQLBundle\DependencyInjection\Compiler\ResolverTaggedServiceMappingPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

/**
* Class ResolverTaggedServiceMappingPassTest
* @package DependencyInjection
*/
class ResolverTaggedServiceMappingPassTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ContainerBuilder
*/
private $container;

public function setUp()
{
$container = new ContainerBuilder();
$container->setDefinition(
'injected_service',
new Definition('Overblog\GraphQLBundle\Tests\DependencyInjection\Compiler\FakeInjectedService')
);

$container->register(
'overblog_graphql.resolver_resolver',
"Overblog\\GraphQLBundle\\Resolver\\ResolverResolver"
);

$testResolver = new Definition('Overblog\GraphQLBundle\Tests\DependencyInjection\Compiler\ResolverTestService');
$testResolver
->addTag( 'overblog_graphql.resolver', [
'alias' => 'test_resolver', 'method' => 'doSomethingWithContainer'
]);

$container->setDefinition('test_resolver', $testResolver);

$this->container = $container;
}

private function addCompilerPassesAndCompile()
{
$this->container->addCompilerPass(new ResolverTaggedServiceMappingPass());
$this->container->addCompilerPass(new FakeCompilerPass());
$this->container->compile();
}

public function testCompilationWorksPassConfigDirective()
{
$this->addCompilerPassesAndCompile();

$this->assertTrue($this->container->has('test_resolver'));
}
}
24 changes: 24 additions & 0 deletions Tests/DependencyInjection/Compiler/ResolverTestService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;

/**
* Class ResolverTestService
* @package DependencyInjection\Compiler
*/
class ResolverTestService implements ContainerAwareInterface
{
use ContainerAwareTrait;

public function __construct($service)
{
}

public function doSomethingWithContainer()
{
return $this->container->get('injected_service')->doSomething();
}
}