1
|
|
|
<?php |
2
|
|
|
namespace Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler; |
3
|
|
|
|
4
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
5
|
|
|
use Doctrine\Common\Annotations\PhpParser; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Skrz\Bundle\AutowiringBundle\DependencyInjection\ClassMultiMap; |
8
|
|
|
use Skrz\Bundle\AutowiringBundle\DependencyInjection\Compiler\AutowiringCompilerPass; |
9
|
|
|
use Skrz\Bundle\AutowiringBundle\DependencyInjection\Compiler\ClassMapBuildCompilerPass; |
10
|
|
|
use Skrz\Bundle\AutowiringBundle\Exception\AutowiringException; |
11
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\AutowiredClass; |
12
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\InterfaceAutowiredClass; |
13
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\OptionalAutowiredClass; |
14
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\SomeClass; |
15
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\SomeClass2; |
16
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\SomeInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
class AutowiringCompilerPassTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
public function testAutowireConstructorWithMissingClass() |
25
|
|
|
{ |
26
|
|
|
$this->expectException(AutowiringException::class); |
27
|
|
|
|
28
|
|
|
$containerBuilder = new ContainerBuilder(); |
29
|
|
|
$classMultiMap = new ClassMultiMap($containerBuilder); |
30
|
|
|
$classMapBuildCompilerPass = new ClassMapBuildCompilerPass($classMultiMap); |
31
|
|
|
$autowiringCompilerPass = new AutowiringCompilerPass($classMultiMap, new AnnotationReader(), new PhpParser()); |
32
|
|
|
$containerBuilder->setDefinition("autowiredService", new Definition(AutowiredClass::class)); |
33
|
|
|
$classMapBuildCompilerPass->process($containerBuilder); |
34
|
|
|
$autowiringCompilerPass->process($containerBuilder); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function testAutowireConstructor() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$containerBuilder = new ContainerBuilder(); |
40
|
|
|
$classMultiMap = new ClassMultiMap($containerBuilder); |
41
|
|
|
$classMapBuildCompilerPass = new ClassMapBuildCompilerPass($classMultiMap); |
42
|
|
|
$autowiringCompilerPass = new AutowiringCompilerPass($classMultiMap, new AnnotationReader(), new PhpParser()); |
43
|
|
|
|
44
|
|
|
$autowiredServiceDefinition = $containerBuilder->setDefinition("autowiredService", new Definition(AutowiredClass::class)); |
45
|
|
|
$containerBuilder->setDefinition("someService", new Definition(SomeClass::class)); |
46
|
|
|
|
47
|
|
|
$this->assertSame([], $autowiredServiceDefinition->getArguments()); |
48
|
|
|
|
49
|
|
|
$classMapBuildCompilerPass->process($containerBuilder); |
50
|
|
|
$autowiringCompilerPass->process($containerBuilder); |
51
|
|
|
|
52
|
|
|
$arguments = $autowiredServiceDefinition->getArguments(); |
53
|
|
|
$this->assertNotSame([], $arguments); |
54
|
|
|
|
55
|
|
|
/** @var Reference $reference */ |
56
|
|
|
$reference = $arguments[0]; |
57
|
|
|
$this->assertInstanceOf(Reference::class, $reference); |
58
|
|
|
$this->assertSame("someService", (string)$reference); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public function testAutowireConstructorWithInterface() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$containerBuilder = new ContainerBuilder(); |
64
|
|
|
$classMultiMap = new ClassMultiMap($containerBuilder); |
65
|
|
|
$classMapBuildCompilerPass = new ClassMapBuildCompilerPass($classMultiMap); |
66
|
|
|
$autowiringCompilerPass = new AutowiringCompilerPass($classMultiMap, new AnnotationReader(), new PhpParser()); |
67
|
|
|
|
68
|
|
|
$autowiredServiceDefinition = $containerBuilder->setDefinition("autowiredService", new Definition(InterfaceAutowiredClass::class)); |
69
|
|
|
$containerBuilder->setDefinition("someService", new Definition(SomeClass::class)); |
70
|
|
|
|
71
|
|
|
$this->assertSame([], $autowiredServiceDefinition->getArguments()); |
72
|
|
|
|
73
|
|
|
$classMapBuildCompilerPass->process($containerBuilder); |
74
|
|
|
$autowiringCompilerPass->process($containerBuilder); |
75
|
|
|
|
76
|
|
|
$arguments = $autowiredServiceDefinition->getArguments(); |
77
|
|
|
$this->assertNotSame([], $arguments); |
78
|
|
|
|
79
|
|
|
/** @var Reference $reference */ |
80
|
|
|
$reference = $arguments[0]; |
81
|
|
|
$this->assertInstanceOf(Reference::class, $reference); |
82
|
|
|
$this->assertSame("someService", (string)$reference); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testAutowireConstructorWithInterfaceOptionally() |
86
|
|
|
{ |
87
|
|
|
$containerBuilder = new ContainerBuilder(); |
88
|
|
|
$classMultiMap = new ClassMultiMap($containerBuilder); |
89
|
|
|
$classMapBuildCompilerPass = new ClassMapBuildCompilerPass($classMultiMap); |
90
|
|
|
$autowiringCompilerPass = new AutowiringCompilerPass($classMultiMap, new AnnotationReader(), new PhpParser()); |
91
|
|
|
|
92
|
|
|
$service2Ref = new Reference("someService2"); |
93
|
|
|
|
94
|
|
|
$autowiredServiceDefinition = $containerBuilder->setDefinition("autowiredService", new Definition( |
95
|
|
|
OptionalAutowiredClass::class, |
96
|
|
|
[ |
97
|
|
|
"someClass2" => $service2Ref, |
98
|
|
|
] |
99
|
|
|
)); |
100
|
|
|
$containerBuilder->setDefinition("someService", new Definition(SomeClass::class)); |
101
|
|
|
$containerBuilder->setDefinition("someService2", new Definition(SomeClass2::class)); |
102
|
|
|
|
103
|
|
|
$containerBuilder->getParameterBag()->add([ |
104
|
|
|
"autowiring.preferred_services" => [ |
105
|
|
|
SomeInterface::class => "someService", |
106
|
|
|
] |
107
|
|
|
]); |
108
|
|
|
|
109
|
|
|
$this->assertSame( |
110
|
|
|
[ |
111
|
|
|
"someClass2" => $service2Ref, |
112
|
|
|
], |
113
|
|
|
$autowiredServiceDefinition->getArguments() |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$classMapBuildCompilerPass->process($containerBuilder); |
117
|
|
|
$autowiringCompilerPass->process($containerBuilder); |
118
|
|
|
|
119
|
|
|
$arguments = $autowiredServiceDefinition->getArguments(); |
120
|
|
|
$this->assertNotSame([], $arguments); |
121
|
|
|
|
122
|
|
|
/** @var Reference $reference */ |
123
|
|
|
$reference = $arguments[0]; |
124
|
|
|
$this->assertInstanceOf(Reference::class, $reference); |
125
|
|
|
$this->assertSame("someService", (string)$reference); |
126
|
|
|
|
127
|
|
|
$reference = $arguments[1]; |
128
|
|
|
$this->assertInstanceOf(Reference::class, $reference); |
129
|
|
|
$this->assertSame("someService2", (string)$reference); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
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.