1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Symplify\DefaultAutowire\Tests\DependencyInjection\Definition; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
11
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
12
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
13
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
14
|
|
|
use Symplify\DefaultAutowire\DependencyInjection\Definition\DefinitionAnalyzer; |
15
|
|
|
use Symplify\DefaultAutowire\DependencyInjection\Definition\DefinitionValidator; |
16
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\ |
17
|
|
|
MissingArgumentsTypehintsFactory; |
18
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\ |
19
|
|
|
NotMissingArgumentsTypehintsFactory; |
20
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\EmptyConstructor; |
21
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\EmptyConstructorFactory; |
22
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\MissingArgumentsTypehints; |
23
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\NotMissingArgumentsTypehints; |
24
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\TestEntity; |
25
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\TestEntityRepository; |
26
|
|
|
|
27
|
|
|
final class DefinitionAnalyzerForFactoryTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var DefinitionAnalyzer |
31
|
|
|
*/ |
32
|
|
|
private $definitionAnalyzer; |
33
|
|
|
|
34
|
|
|
protected function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->definitionAnalyzer = new DefinitionAnalyzer(new DefinitionValidator()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function testServiceFactoryMethodDoesNotHaveArguments() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$containerBuilder = new ContainerBuilder(); |
42
|
|
|
$containerBuilder->addDefinitions([ |
43
|
|
|
'factory' => new Definition(EmptyConstructorFactory::class), |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
$definition = new Definition(EmptyConstructor::class); |
47
|
|
|
$definition->setFactory([ |
48
|
|
|
new Reference('factory'), |
49
|
|
|
'create', |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired($containerBuilder, $definition)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testFactoryMethodDoesNotHaveArguments() |
56
|
|
|
{ |
57
|
|
|
$definition = new Definition(EmptyConstructor::class); |
58
|
|
|
$definition->setFactory([ |
59
|
|
|
EmptyConstructorFactory::class, |
60
|
|
|
'create', |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired(new ContainerBuilder(), $definition)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function testServiceFactoryBuiltClassHaveMissingArgumentsTypehints() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$containerBuilder = new ContainerBuilder(); |
69
|
|
|
$containerBuilder->addDefinitions([ |
70
|
|
|
'factory' => new Definition(MissingArgumentsTypehintsFactory::class), |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
$definition = new Definition(MissingArgumentsTypehints::class); |
74
|
|
|
$definition->setFactory([ |
75
|
|
|
new Reference('factory'), |
76
|
|
|
'create', |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$definition->setArguments(['@someService']); |
80
|
|
|
|
81
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired($containerBuilder, $definition)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
View Code Duplication |
public function testFactoryBuiltClassHaveMissingArgumentsTypehints() |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$definition = new Definition(MissingArgumentsTypehints::class); |
87
|
|
|
$definition->setFactory([ |
88
|
|
|
MissingArgumentsTypehintsFactory::class, |
89
|
|
|
'create', |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
$definition->setArguments(['@someService']); |
93
|
|
|
|
94
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired(new ContainerBuilder(), $definition)); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
public function testServiceFactoryBuiltClassHaveNotMissingArgumentsTypehints() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$containerBuilder = new ContainerBuilder(); |
100
|
|
|
$containerBuilder->addDefinitions([ |
101
|
|
|
'factory' => new Definition(NotMissingArgumentsTypehintsFactory::class), |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
$definition = new Definition(NotMissingArgumentsTypehints::class); |
105
|
|
|
$definition->setFactory([ |
106
|
|
|
new Reference('factory'), |
107
|
|
|
'create', |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
$definition->setArguments(['@someService']); |
111
|
|
|
|
112
|
|
|
$this->assertTrue($this->definitionAnalyzer->shouldDefinitionBeAutowired($containerBuilder, $definition)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
View Code Duplication |
public function testFactoryBuiltClassHaveNotMissingArgumentsTypehints() |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$definition = new Definition(NotMissingArgumentsTypehints::class); |
118
|
|
|
$definition->setFactory([ |
119
|
|
|
NotMissingArgumentsTypehintsFactory::class, |
120
|
|
|
'create', |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
$definition->setArguments(['@someService']); |
124
|
|
|
|
125
|
|
|
$this->assertTrue($this->definitionAnalyzer->shouldDefinitionBeAutowired(new ContainerBuilder(), $definition)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
View Code Duplication |
public function testFactoryServiceIsUsedByAlias() |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$containerBuilder = new ContainerBuilder(); |
131
|
|
|
$containerBuilder->addDefinitions([ |
132
|
|
|
'factory' => new Definition(EmptyConstructorFactory::class), |
133
|
|
|
]); |
134
|
|
|
$containerBuilder->addAliases([ |
135
|
|
|
'factory_alias' => 'factory', |
136
|
|
|
]); |
137
|
|
|
|
138
|
|
|
$definition = new Definition(EmptyConstructor::class); |
139
|
|
|
$definition->setFactory([ |
140
|
|
|
new Reference('factory_alias'), |
141
|
|
|
'create', |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired($containerBuilder, $definition)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
View Code Duplication |
public function testFactoryServiceCanBeDecorated() |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$containerBuilder = new ContainerBuilder(); |
150
|
|
|
$containerBuilder->addDefinitions([ |
151
|
|
|
'factory' => new Definition(EmptyConstructorFactory::class), |
152
|
|
|
'decorated_factory' => new DefinitionDecorator('factory'), |
153
|
|
|
]); |
154
|
|
|
|
155
|
|
|
$definition = new Definition(EmptyConstructor::class); |
156
|
|
|
$definition->setFactory([ |
157
|
|
|
new Reference('decorated_factory'), |
158
|
|
|
'create', |
159
|
|
|
]); |
160
|
|
|
|
161
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired($containerBuilder, $definition)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
View Code Duplication |
public function testFactoryClassNameIsDefinedByParameter() |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
$containerBuilder = new ContainerBuilder(); |
167
|
|
|
$containerBuilder->addDefinitions([ |
168
|
|
|
'factory' => new Definition('%factory_class_param%'), |
169
|
|
|
]); |
170
|
|
|
$containerBuilder->setParameter('factory_class_param', EmptyConstructorFactory::class); |
171
|
|
|
|
172
|
|
|
$definition = new Definition(EmptyConstructor::class); |
173
|
|
|
$definition->setFactory([ |
174
|
|
|
new Reference('factory'), |
175
|
|
|
'create', |
176
|
|
|
]); |
177
|
|
|
|
178
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired($containerBuilder, $definition)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testDoctrineRepositoryAsService() |
182
|
|
|
{ |
183
|
|
|
$containerBuilder = new ContainerBuilder(); |
184
|
|
|
$containerBuilder->addDefinitions([ |
185
|
|
|
'doctrine.orm.default_entity_manager' => new DefinitionDecorator('doctrine.orm.entity_manager.abstract'), |
186
|
|
|
'doctrine.orm.entity_manager.abstract' => new Definition('%doctrine.orm.entity_manager.class%'), |
187
|
|
|
]); |
188
|
|
|
$containerBuilder->setAlias('doctrine.orm.entity_manager', new Alias('doctrine.orm.default_entity_manager')); |
189
|
|
|
$containerBuilder->setParameter('doctrine.orm.entity_manager.class', EntityManager::class); |
190
|
|
|
|
191
|
|
|
$testRepository = new Definition(TestEntityRepository::class); |
192
|
|
|
$testRepository->setFactory([ |
193
|
|
|
new Reference('doctrine.orm.entity_manager'), |
194
|
|
|
'getRepository', |
195
|
|
|
]); |
196
|
|
|
$testRepository->addArgument(TestEntity::class); |
197
|
|
|
|
198
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired($containerBuilder, $testRepository)); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
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.