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\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\AutowiredClassOverridesPropertyTrait; |
11
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\AutowiredClassUsesPropertyTrait; |
12
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\AutowiredPropertyClass; |
13
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\Foo\Bar; |
14
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\Foo2\Bar2; |
15
|
|
|
use Skrz\Bundle\AutowiringBundle\Tests\DependencyInjection\Compiler\AutowiringCompilerPassSource\SomeClass; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
19
|
|
|
|
20
|
|
|
class AutowiringCompilerPassPropertyTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
View Code Duplication |
public function testAutowireProperty() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$containerBuilder = new ContainerBuilder(); |
26
|
|
|
$classMultiMap = new ClassMultiMap($containerBuilder); |
27
|
|
|
|
28
|
|
|
$classMapBuildCompilerPass = new ClassMapBuildCompilerPass($classMultiMap); |
29
|
|
|
$autowiringCompilerPass = new AutowiringCompilerPass($classMultiMap, new AnnotationReader(), new PhpParser()); |
30
|
|
|
|
31
|
|
|
$autowiredServiceDefinition = $containerBuilder->setDefinition("autowiredService", new Definition(AutowiredPropertyClass::class)); |
32
|
|
|
$containerBuilder->setDefinition("someService", new Definition(SomeClass::class)); |
33
|
|
|
|
34
|
|
|
$this->assertSame([], $autowiredServiceDefinition->getProperties()); |
35
|
|
|
|
36
|
|
|
$classMapBuildCompilerPass->process($containerBuilder); |
37
|
|
|
$autowiringCompilerPass->process($containerBuilder); |
38
|
|
|
|
39
|
|
|
$reference = $autowiredServiceDefinition->getProperties()["property"]; |
40
|
|
|
$this->assertInstanceOf(Reference::class, $reference); |
41
|
|
|
$this->assertSame("someService", (string)$reference); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function testAutowireTraitProperty() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$containerBuilder = new ContainerBuilder(); |
47
|
|
|
$classMultiMap = new ClassMultiMap($containerBuilder); |
48
|
|
|
|
49
|
|
|
$classMapBuildCompilerPass = new ClassMapBuildCompilerPass($classMultiMap); |
50
|
|
|
$autowiringCompilerPass = new AutowiringCompilerPass($classMultiMap, new AnnotationReader(), new PhpParser()); |
51
|
|
|
|
52
|
|
|
$autowiredServiceDefinition = $containerBuilder->setDefinition("service", new Definition(AutowiredClassUsesPropertyTrait::class)); |
53
|
|
|
$containerBuilder->setDefinition("bar", new Definition(Bar::class)); |
54
|
|
|
|
55
|
|
|
$this->assertSame([], $autowiredServiceDefinition->getProperties()); |
56
|
|
|
|
57
|
|
|
$classMapBuildCompilerPass->process($containerBuilder); |
58
|
|
|
$autowiringCompilerPass->process($containerBuilder); |
59
|
|
|
|
60
|
|
|
$reference = $autowiredServiceDefinition->getProperties()["property"]; |
61
|
|
|
$this->assertInstanceOf(Reference::class, $reference); |
62
|
|
|
$this->assertSame("bar", (string)$reference); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function testAutowireOverriddenTraitProperty() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$containerBuilder = new ContainerBuilder(); |
68
|
|
|
$classMultiMap = new ClassMultiMap($containerBuilder); |
69
|
|
|
|
70
|
|
|
$classMapBuildCompilerPass = new ClassMapBuildCompilerPass($classMultiMap); |
71
|
|
|
$autowiringCompilerPass = new AutowiringCompilerPass($classMultiMap, new AnnotationReader(), new PhpParser()); |
72
|
|
|
|
73
|
|
|
$autowiredServiceDefinition = $containerBuilder->setDefinition("service", new Definition(AutowiredClassOverridesPropertyTrait::class)); |
74
|
|
|
$containerBuilder->setDefinition("bar", new Definition(Bar::class)); |
75
|
|
|
$containerBuilder->setDefinition("bar2", new Definition(Bar2::class)); |
76
|
|
|
|
77
|
|
|
$this->assertSame([], $autowiredServiceDefinition->getProperties()); |
78
|
|
|
|
79
|
|
|
$classMapBuildCompilerPass->process($containerBuilder); |
80
|
|
|
$autowiringCompilerPass->process($containerBuilder); |
81
|
|
|
|
82
|
|
|
$reference = $autowiredServiceDefinition->getProperties()["property"]; |
83
|
|
|
$this->assertInstanceOf(Reference::class, $reference); |
84
|
|
|
$this->assertSame("bar2", (string)$reference); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
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.