1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: gseidel |
5
|
|
|
* Date: 2020-06-08 |
6
|
|
|
* Time: 09:49 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Enhavo\Component\Type\Tests; |
10
|
|
|
|
11
|
|
|
use Enhavo\Component\Type\AbstractType; |
12
|
|
|
use Enhavo\Component\Type\Registry; |
13
|
|
|
use Enhavo\Component\Type\TypeInterface; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
16
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
17
|
|
|
|
18
|
|
|
class RegistryTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
private function createDependencies() |
21
|
|
|
{ |
22
|
|
|
$dependencies = new RegistryDependencies(); |
23
|
|
|
$dependencies->container = $this->getMockBuilder(ContainerInterface::class)->getMock(); |
24
|
|
|
$dependencies->namespace = 'Test'; |
25
|
|
|
return $dependencies; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function createInstance(RegistryDependencies $dependencies): Registry |
29
|
|
|
{ |
30
|
|
|
$registry = new Registry($dependencies->namespace); |
31
|
|
|
$registry->setContainer($dependencies->container); |
32
|
|
|
return $registry; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testGetTypeByFQCN() |
36
|
|
|
{ |
37
|
|
|
$dependencies = $this->createDependencies(); |
38
|
|
|
$dependencies->container->method('get')->willReturnCallback(function($id) { |
39
|
|
|
$services = [ |
40
|
|
|
'test_type_id' => new TestType(), |
41
|
|
|
'parent_test_type_id' => new ParentTestType(), |
42
|
|
|
'root_type_id' => new RootType(), |
43
|
|
|
]; |
44
|
|
|
return $services[$id]; |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
$registry = $this->createInstance($dependencies); |
48
|
|
|
$registry->register(TestType::class, 'test_type_id'); |
49
|
|
|
$registry->register(ParentTestType::class, 'parent_test_type_id'); |
50
|
|
|
$registry->register(RootType::class, 'root_type_id'); |
51
|
|
|
|
52
|
|
|
$type = $registry->getType(TestType::class); |
53
|
|
|
|
54
|
|
|
$this->assertEquals('test', $type::getName()); |
55
|
|
|
$this->assertEquals('parent_test', $type->getParent()::getName()); |
|
|
|
|
56
|
|
|
$this->assertEquals('root', $type->getParent()->getParent()::getName()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetTypeByName() |
60
|
|
|
{ |
61
|
|
|
$dependencies = $this->createDependencies(); |
62
|
|
|
$dependencies->container->method('get')->willReturnCallback(function($id) { |
63
|
|
|
$services = [ |
64
|
|
|
'root_type_id' => new RootType(), |
65
|
|
|
]; |
66
|
|
|
return $services[$id]; |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
$registry = $this->createInstance($dependencies); |
70
|
|
|
$registry->register(RootType::class, 'root_type_id'); |
71
|
|
|
|
72
|
|
|
$type = $registry->getType('root'); |
73
|
|
|
|
74
|
|
|
$this->assertEquals('root', $type::getName()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @expectedException \Enhavo\Component\Type\Exception\TypeNotFoundException |
79
|
|
|
*/ |
80
|
|
|
public function testNotFound() |
81
|
|
|
{ |
82
|
|
|
$dependencies = $this->createDependencies(); |
83
|
|
|
$registry = $this->createInstance($dependencies); |
84
|
|
|
$registry->register(RootType::class, 'root_type_id'); |
85
|
|
|
$registry->getType('type_not_exists'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @expectedException \Enhavo\Component\Type\Exception\TypeNotValidException |
90
|
|
|
*/ |
91
|
|
|
public function testCircularDetection() |
92
|
|
|
{ |
93
|
|
|
$dependencies = $this->createDependencies(); |
94
|
|
|
$registry = $this->createInstance($dependencies); |
95
|
|
|
$registry->register(CircularTypeOne::class, CircularTypeOne::class); |
96
|
|
|
$registry->register(CircularTypeTwo::class, CircularTypeTwo::class); |
97
|
|
|
$registry->register(CircularTypeThree::class, CircularTypeThree::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @expectedException \Enhavo\Component\Type\Exception\TypeNotValidException |
102
|
|
|
*/ |
103
|
|
|
public function testSelfCircularDetection() |
104
|
|
|
{ |
105
|
|
|
$dependencies = $this->createDependencies(); |
106
|
|
|
$registry = $this->createInstance($dependencies); |
107
|
|
|
$registry->register(SelfReference::class, SelfReference::class); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @expectedException \Enhavo\Component\Type\Exception\TypeNotValidException |
112
|
|
|
*/ |
113
|
|
|
public function testMissingInterface() |
114
|
|
|
{ |
115
|
|
|
$dependencies = $this->createDependencies(); |
116
|
|
|
$registry = $this->createInstance($dependencies); |
117
|
|
|
$registry->register(NoInterface::class, NoInterface::class); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @doesNotPerformAssertions |
122
|
|
|
*/ |
123
|
|
|
public function testIndirectInterface() |
124
|
|
|
{ |
125
|
|
|
$dependencies = $this->createDependencies(); |
126
|
|
|
$registry = $this->createInstance($dependencies); |
127
|
|
|
$registry->register(IndirectClass::class, IndirectClass::class); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @expectedException \Enhavo\Component\Type\Exception\TypeNotValidException |
132
|
|
|
*/ |
133
|
|
|
public function testParentClassInterface() |
134
|
|
|
{ |
135
|
|
|
$dependencies = $this->createDependencies(); |
136
|
|
|
$registry = $this->createInstance($dependencies); |
137
|
|
|
$registry->register(ParentNotExitsType::class, ParentNotExitsType::class); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @expectedException \Enhavo\Component\Type\Exception\TypeNotValidException |
142
|
|
|
*/ |
143
|
|
|
public function testInvalidParent() |
144
|
|
|
{ |
145
|
|
|
$dependencies = $this->createDependencies(); |
146
|
|
|
$registry = $this->createInstance($dependencies); |
147
|
|
|
$registry->register(InvalidParentType::class, InvalidParentType::class); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @expectedException \Enhavo\Component\Type\Exception\TypeNotValidException |
152
|
|
|
*/ |
153
|
|
|
public function testDoubleClassNames() |
154
|
|
|
{ |
155
|
|
|
$dependencies = $this->createDependencies(); |
156
|
|
|
$registry = $this->createInstance($dependencies); |
157
|
|
|
$registry->register(TestType::class, TestType::class); |
158
|
|
|
$registry->register(TestType::class, TestType::class); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @expectedException \Enhavo\Component\Type\Exception\TypeNotValidException |
163
|
|
|
*/ |
164
|
|
|
public function testDoubleNames() |
165
|
|
|
{ |
166
|
|
|
$dependencies = $this->createDependencies(); |
167
|
|
|
$registry = $this->createInstance($dependencies); |
168
|
|
|
$registry->register(TestType::class, TestType::class); |
169
|
|
|
$registry->register(OtherTestType::class, OtherTestType::class); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
class RegistryDependencies |
174
|
|
|
{ |
175
|
|
|
/** @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject */ |
176
|
|
|
public $container; |
177
|
|
|
/** @var String */ |
178
|
|
|
public $namespace; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
class TestType extends AbstractType |
182
|
|
|
{ |
183
|
|
|
public function getParent() |
184
|
|
|
{ |
185
|
|
|
return $this->parent; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public static function getParentType(): ?string |
189
|
|
|
{ |
190
|
|
|
return ParentTestType::class; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public static function getName(): ?string |
194
|
|
|
{ |
195
|
|
|
return 'test'; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
class ParentTestType extends AbstractType |
200
|
|
|
{ |
201
|
|
|
public function getParent() |
202
|
|
|
{ |
203
|
|
|
return $this->parent; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public static function getParentType(): ?string |
207
|
|
|
{ |
208
|
|
|
return RootType::class; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public static function getName(): ?string |
212
|
|
|
{ |
213
|
|
|
return 'parent_test'; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
class RootType extends AbstractType |
218
|
|
|
{ |
219
|
|
|
public function getParent() |
220
|
|
|
{ |
221
|
|
|
return $this->parent; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public static function getName(): ?string |
225
|
|
|
{ |
226
|
|
|
return 'root'; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
class CircularTypeOne extends AbstractType |
231
|
|
|
{ |
232
|
|
|
public static function getParentType(): ?string |
233
|
|
|
{ |
234
|
|
|
return CircularTypeTwo::class; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
class CircularTypeTwo extends AbstractType |
239
|
|
|
{ |
240
|
|
|
public static function getParentType(): ?string |
241
|
|
|
{ |
242
|
|
|
return CircularTypeTwo::class; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
class CircularTypeThree extends AbstractType |
247
|
|
|
{ |
248
|
|
|
public static function getParentType(): ?string |
249
|
|
|
{ |
250
|
|
|
return CircularTypeOne::class; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
class SelfReference extends AbstractType |
255
|
|
|
{ |
256
|
|
|
public static function getParentType(): ?string |
257
|
|
|
{ |
258
|
|
|
return self::class; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
class InvalidParentType extends AbstractType |
263
|
|
|
{ |
264
|
|
|
public static function getParentType(): ?string |
265
|
|
|
{ |
266
|
|
|
return NoInterface::class; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
class ParentNotExitsType extends AbstractType |
271
|
|
|
{ |
272
|
|
|
public static function getParentType(): ?string |
273
|
|
|
{ |
274
|
|
|
return 'Something\But\NotValidAClass'; |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
class OtherTestType extends AbstractType |
279
|
|
|
{ |
280
|
|
|
public static function getName(): ?string |
281
|
|
|
{ |
282
|
|
|
return 'test'; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
class NoInterface |
287
|
|
|
{ |
288
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
interface IndirectInterface extends TypeInterface |
292
|
|
|
{ |
293
|
|
|
|
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
class IndirectClass implements IndirectInterface |
297
|
|
|
{ |
298
|
|
|
public function setKey(?string $key) |
299
|
|
|
{ |
300
|
|
|
|
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public static function getName(): ?string |
304
|
|
|
{ |
305
|
|
|
return null; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public static function getParentType(): ?string |
309
|
|
|
{ |
310
|
|
|
return null; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
public function setParent(TypeInterface $parent) |
314
|
|
|
{ |
315
|
|
|
|
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
public function configureOptions(OptionsResolver $resolver) |
319
|
|
|
{ |
320
|
|
|
|
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.