|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Symplify\DefaultAutowire\Tests\DependencyInjection\Definition; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
9
|
|
|
use Symplify\DefaultAutowire\DependencyInjection\Definition\DefinitionAnalyzer; |
|
10
|
|
|
use Symplify\DefaultAutowire\DependencyInjection\Definition\DefinitionValidator; |
|
11
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\EmptyConstructor; |
|
12
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\EmptyConstructorFactory; |
|
13
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\MissingArgumentsTypehints; |
|
14
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\MissingArgumentsTypehintsFactory; |
|
15
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\NotMissingArgumentsTypehints; |
|
16
|
|
|
use Symplify\DefaultAutowire\Tests\DependencyInjection\Definition\DefinitionAnalyzerSource\NotMissingArgumentsTypehintsFactory; |
|
17
|
|
|
|
|
18
|
|
|
final class DefinitionAnalyzerTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var DefinitionAnalyzer |
|
22
|
|
|
*/ |
|
23
|
|
|
private $definitionAnalyzer; |
|
24
|
|
|
|
|
25
|
|
|
protected function setUp() |
|
26
|
|
|
{ |
|
27
|
|
|
$this->definitionAnalyzer = new DefinitionAnalyzer(new DefinitionValidator()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testClassHasConstructorArguments() |
|
31
|
|
|
{ |
|
32
|
|
|
$definition = new Definition(EmptyConstructor::class); |
|
33
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired(new ContainerBuilder(), $definition)); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function testClassHaveMissingArgumentsTypehints() |
|
37
|
|
|
{ |
|
38
|
|
|
$definition = new Definition(MissingArgumentsTypehints::class); |
|
39
|
|
|
$definition->setArguments(['@someService']); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired(new ContainerBuilder(), $definition)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testClassHaveNotMissingArgumentsTypehints() |
|
45
|
|
|
{ |
|
46
|
|
|
$definition = new Definition(NotMissingArgumentsTypehints::class); |
|
47
|
|
|
$definition->setArguments(['@someService']); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertTrue($this->definitionAnalyzer->shouldDefinitionBeAutowired(new ContainerBuilder(), $definition)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
public function testServiceFactoryMethodDoesNotHaveArguments() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
$containerBuilder = new ContainerBuilder(); |
|
55
|
|
|
$containerBuilder->addDefinitions([ |
|
56
|
|
|
'factory' => new Definition(EmptyConstructorFactory::class), |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
$definition = new Definition(EmptyConstructor::class); |
|
60
|
|
|
$definition->setFactory([ |
|
61
|
|
|
new Reference('factory'), |
|
62
|
|
|
'create', |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired($containerBuilder, $definition)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function testFactoryMethodDoesNotHaveArguments() |
|
69
|
|
|
{ |
|
70
|
|
|
$definition = new Definition(EmptyConstructor::class); |
|
71
|
|
|
$definition->setFactory([ |
|
72
|
|
|
EmptyConstructorFactory::class, |
|
73
|
|
|
'create', |
|
74
|
|
|
]); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired(new ContainerBuilder(), $definition)); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
View Code Duplication |
public function testServiceFactoryBuiltClassHaveMissingArgumentsTypehints() |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
$containerBuilder = new ContainerBuilder(); |
|
82
|
|
|
$containerBuilder->addDefinitions([ |
|
83
|
|
|
'factory' => new Definition(MissingArgumentsTypehintsFactory::class), |
|
84
|
|
|
]); |
|
85
|
|
|
|
|
86
|
|
|
$definition = new Definition(MissingArgumentsTypehints::class); |
|
87
|
|
|
$definition->setFactory([ |
|
88
|
|
|
new Reference('factory'), |
|
89
|
|
|
'create', |
|
90
|
|
|
]); |
|
91
|
|
|
|
|
92
|
|
|
$definition->setArguments(['@someService']); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired($containerBuilder, $definition)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
View Code Duplication |
public function testFactoryBuiltClassHaveMissingArgumentsTypehints() |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
$definition = new Definition(MissingArgumentsTypehints::class); |
|
100
|
|
|
$definition->setFactory([ |
|
101
|
|
|
MissingArgumentsTypehintsFactory::class, |
|
102
|
|
|
'create', |
|
103
|
|
|
]); |
|
104
|
|
|
|
|
105
|
|
|
$definition->setArguments(['@someService']); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertFalse($this->definitionAnalyzer->shouldDefinitionBeAutowired(new ContainerBuilder(), $definition)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
public function testServiceFactoryBuiltClassHaveNotMissingArgumentsTypehints() |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
$containerBuilder = new ContainerBuilder(); |
|
113
|
|
|
$containerBuilder->addDefinitions([ |
|
114
|
|
|
'factory' => new Definition(NotMissingArgumentsTypehintsFactory::class), |
|
115
|
|
|
]); |
|
116
|
|
|
|
|
117
|
|
|
$definition = new Definition(NotMissingArgumentsTypehints::class); |
|
118
|
|
|
$definition->setFactory([ |
|
119
|
|
|
new Reference('factory'), |
|
120
|
|
|
'create', |
|
121
|
|
|
]); |
|
122
|
|
|
|
|
123
|
|
|
$definition->setArguments(['@someService']); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertTrue($this->definitionAnalyzer->shouldDefinitionBeAutowired($containerBuilder, $definition)); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
View Code Duplication |
public function testFactoryBuiltClassHaveNotMissingArgumentsTypehints() |
|
|
|
|
|
|
129
|
|
|
{ |
|
130
|
|
|
$definition = new Definition(NotMissingArgumentsTypehints::class); |
|
131
|
|
|
$definition->setFactory([ |
|
132
|
|
|
NotMissingArgumentsTypehintsFactory::class, |
|
133
|
|
|
'create', |
|
134
|
|
|
]); |
|
135
|
|
|
|
|
136
|
|
|
$definition->setArguments(['@someService']); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertTrue($this->definitionAnalyzer->shouldDefinitionBeAutowired(new ContainerBuilder(), $definition)); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
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.