1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Tests\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\DependencyInjection\Compiler\ResolverTaggedServiceMappingPass; |
6
|
|
|
use Overblog\GraphQLBundle\Resolver\ResolverResolver; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
10
|
|
|
|
11
|
|
|
class ResolverTaggedServiceMappingPassTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** @var ContainerBuilder */ |
14
|
|
|
private $container; |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
$container = new ContainerBuilder(); |
19
|
|
|
$container->setDefinition('injected_service', new Definition(FakeInjectedService::class)); |
20
|
|
|
|
21
|
|
|
$container->register('overblog_graphql.resolver_resolver', ResolverResolver::class); |
22
|
|
|
|
23
|
|
|
$this->container = $container; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function addCompilerPassesAndCompile() |
27
|
|
|
{ |
28
|
|
|
$this->container->addCompilerPass(new ResolverTaggedServiceMappingPass()); |
29
|
|
|
$this->container->addCompilerPass(new FakeCompilerPass()); |
30
|
|
|
$this->container->compile(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testCompilationWorksPassConfigDirective() |
34
|
|
|
{ |
35
|
|
|
$testResolver = new Definition(ResolverTestService::class); |
36
|
|
|
$testResolver |
37
|
|
|
->addTag('overblog_graphql.resolver', [ |
38
|
|
|
'alias' => 'test_resolver', 'method' => 'doSomethingWithContainer', |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
$this->container->setDefinition('test_resolver', $testResolver); |
42
|
|
|
|
43
|
|
|
$this->addCompilerPassesAndCompile(); |
44
|
|
|
|
45
|
|
|
$this->assertTrue($this->container->has('test_resolver')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testTagAliasIsValid() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$testResolver = new Definition(ResolverTestService::class); |
51
|
|
|
$testResolver |
52
|
|
|
->addTag('overblog_graphql.resolver', [ |
53
|
|
|
'alias' => false, 'method' => 'doSomethingWithContainer', |
54
|
|
|
]); |
55
|
|
|
|
56
|
|
|
$this->container->setDefinition('test_resolver', $testResolver); |
57
|
|
|
|
58
|
|
|
$this->expectException(\InvalidArgumentException::class); |
59
|
|
|
$this->expectExceptionMessage('Service tagged "test_resolver" must have valid "alias" argument.'); |
60
|
|
|
|
61
|
|
|
$this->addCompilerPassesAndCompile(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function testTagMethodIsValid() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$testResolver = new Definition(ResolverTestService::class); |
67
|
|
|
$testResolver |
68
|
|
|
->addTag('overblog_graphql.resolver', [ |
69
|
|
|
'alias' => 'test_resolver', 'method' => false, |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$this->container->setDefinition('test_resolver', $testResolver); |
73
|
|
|
|
74
|
|
|
$this->expectException(\InvalidArgumentException::class); |
75
|
|
|
$this->expectExceptionMessage('Service tagged "test_resolver" must have valid "method" argument.'); |
76
|
|
|
|
77
|
|
|
$this->addCompilerPassesAndCompile(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.