-
-
Notifications
You must be signed in to change notification settings - Fork 312
/
CompiledContainerTest.php
329 lines (292 loc) · 10.6 KB
/
CompiledContainerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
declare(strict_types=1);
namespace DI\Test\IntegrationTest;
use function DI\autowire;
use DI\ContainerBuilder;
use function DI\create;
use DI\Definition\Exception\InvalidDefinition;
use function DI\get;
/**
* Tests specific to the compiled container.
*/
class CompiledContainerTest extends BaseContainerTest
{
/** @test */
public function the_same_container_can_be_recreated_multiple_times()
{
$builder = new ContainerBuilder;
$builder->enableCompilation(self::COMPILATION_DIR, self::generateCompiledClassName());
$builder->addDefinitions([
'foo' => 'bar',
]);
// The container can be built twice without error
$builder->build();
$builder->build();
}
/** @test */
public function the_container_is_compiled_once_and_never_recompiled_after()
{
$compiledContainerClass = self::generateCompiledClassName();
// Create a first compiled container in the file
$builder = new ContainerBuilder;
$builder->addDefinitions([
'foo' => 'bar',
]);
$builder->enableCompilation(self::COMPILATION_DIR, $compiledContainerClass);
$builder->build();
// Create a second compiled container in the same file but with a DIFFERENT configuration
$builder = new ContainerBuilder;
$builder->addDefinitions([
'foo' => 'DIFFERENT',
]);
$builder->enableCompilation(self::COMPILATION_DIR, $compiledContainerClass);
$container = $builder->build();
// The second container is actually using the config of the first because the container was already compiled
// (the compiled file already existed so the second container did not recompile into it)
// This behavior is obvious for performance reasons.
self::assertEquals('bar', $container->get('foo'));
}
/** @test */
public function the_compiled_container_is_idempotent()
{
$compiledContainerClass1 = self::generateCompiledClassName();
$compiledContainerClass2 = self::generateCompiledClassName();
$definitions = [
'foo' => 'barFromFoo',
'fooReference' => \DI\get('foo'),
'factory' => function () {
return 'barFromFactory';
},
'factoryReference' => \DI\get('factory'),
'array' => [
1,
2,
3,
'fooBar',
],
'arrayValue' => \DI\value('array'),
CompiledContainerTest\AllKindsOfInjections::class => create()
->constructor(create('stdClass'))
->property('property', autowire(CompiledContainerTest\Autowireable::class))
->method('method', \DI\factory(
function () {
return new \stdClass;
}
)
),
CompiledContainerTest\Autowireable::class => \DI\autowire(),
];
// Create a compiled container in a specific file
$builder1 = new ContainerBuilder;
$builder1->addDefinitions($definitions);
$builder1->enableCompilation(self::COMPILATION_DIR, $compiledContainerClass1);
$builder1->build();
// Create a second compiled container with the same configuration but in a different file
$builder2 = new ContainerBuilder;
$builder2->addDefinitions($definitions);
$builder2->enableCompilation(self::COMPILATION_DIR, $compiledContainerClass2);
$builder2->build();
// The method mapping of the resulting CompiledContainers should be equal
self::assertEquals($compiledContainerClass1::METHOD_MAPPING, $compiledContainerClass2::METHOD_MAPPING);
}
/**
* @test
* @expectedException \DI\Definition\Exception\InvalidDefinition
* @expectedExceptionMessage Entry "foo" cannot be compiled: anonymous classes cannot be compiled
*/
public function anonymous_classes_cannot_be_compiled()
{
$class = get_class(new class() {
});
$builder = new ContainerBuilder;
$builder->addDefinitions([
'foo' => create($class),
]);
$builder->enableCompilation(self::COMPILATION_DIR, self::generateCompiledClassName());
$builder->build();
}
/**
* @test
* @expectedException \DI\Definition\Exception\InvalidDefinition
* @expectedExceptionMessage Entry "stdClass" cannot be compiled: An object was found but objects cannot be compiled
*/
public function object_nested_in_other_definitions_cannot_be_compiled()
{
$builder = new ContainerBuilder;
$builder->addDefinitions([
\stdClass::class => create()
->property('foo', new \stdClass),
]);
$builder->enableCompilation(self::COMPILATION_DIR, self::generateCompiledClassName());
$builder->build();
}
/**
* @test
* @expectedException \DI\DependencyException
* @expectedExceptionMessage Error while compiling foo. Error while compiling <nested definition>. Error while compiling <nested definition>. An object was found but objects cannot be compiled
*/
public function object_nested_in_arrays_cannot_be_compiled()
{
$builder = new ContainerBuilder;
$builder->addDefinitions([
'foo' => [
'bar' => [
'baz' => [
new \stdClass,
],
],
],
]);
$builder->enableCompilation(self::COMPILATION_DIR, self::generateCompiledClassName());
$builder->build();
}
/**
* @test
* @expectedException \LogicException
* @expectedExceptionMessage You cannot set a definition at runtime on a compiled container. You can either put your definitions in a file, disable compilation or ->set() a raw value directly (PHP object, string, int, ...) instead of a PHP-DI definition.
*/
public function entries_cannot_be_overridden_by_definitions_in_the_compiled_container()
{
$builder = new ContainerBuilder;
$builder->enableCompilation(self::COMPILATION_DIR, self::generateCompiledClassName());
$builder->addDefinitions([
'foo' => create(\stdClass::class),
]);
$container = $builder->build();
$container->set('foo', create(ContainerSetTest\Dummy::class));
}
/**
* @test
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The container cannot be compiled: `123-abc` is not a valid PHP class name
*/
public function compiling_to_an_invalid_class_name_throws_an_error()
{
$builder = new ContainerBuilder;
$builder->enableCompilation(self::COMPILATION_DIR, '123-abc');
$builder->build();
}
/**
* @test
*/
public function the_compiled_container_can_extend_a_custom_class()
{
$builder = new ContainerBuilder;
$builder->enableCompilation(
self::COMPILATION_DIR,
self::generateCompiledClassName(),
// Customize the parent class
CompiledContainerTest\CustomParentContainer::class
);
$container = $builder->build();
self::assertInstanceOf(CompiledContainerTest\CustomParentContainer::class, $container);
}
/**
* @test
* @see https://github.com/PHP-DI/PHP-DI/issues/565
*/
public function recursively_compiles_referenced_definitions_found()
{
$builder = new ContainerBuilder;
$builder->addDefinitions([
'foo' => create(CompiledContainerTest\Property::class)
->property('foo', get(CompiledContainerTest\ConstructorWithTypehint::class)),
]);
$builder->enableCompilation(self::COMPILATION_DIR, self::generateCompiledClassName());
$this->assertEntryIsCompiled($builder->build(), CompiledContainerTest\ConstructorWithTypehint::class);
// Dependency of a dependency
$this->assertEntryIsCompiled($builder->build(), CompiledContainerTest\ConstructorWithAnotherTypehint::class);
}
/**
* @test
* @see https://github.com/PHP-DI/PHP-DI/issues/567
*/
public function invalid_definitions_referenced_in_the_configuration_throw_an_error()
{
$message = <<<MESSAGE
Entry "DI\Test\IntegrationTest\CompiledContainerTest\AbstractClass" cannot be compiled: the class is not instantiable
Full definition:
Object (
class = #NOT INSTANTIABLE# DI\Test\IntegrationTest\CompiledContainerTest\AbstractClass
lazy = false
)
MESSAGE;
$this->expectException(InvalidDefinition::class);
$this->expectExceptionMessage($message);
$builder = new ContainerBuilder;
$builder->addDefinitions([
CompiledContainerTest\ConstructorWithAbstractClassTypehint::class => autowire(),
CompiledContainerTest\AbstractClass::class => autowire(),
]);
$builder->enableCompilation(self::COMPILATION_DIR, self::generateCompiledClassName());
$builder->build();
}
/**
* @test
* @see https://github.com/PHP-DI/PHP-DI/issues/567
*/
public function invalid_definitions_transitively_referenced_are_skipped_and_do_not_throw_an_error()
{
$builder = new ContainerBuilder;
$builder->addDefinitions([
CompiledContainerTest\ConstructorWithAbstractClassTypehint::class => autowire(),
]);
$builder->enableCompilation(self::COMPILATION_DIR, self::generateCompiledClassName());
$builder->build();
$this->assertEntryIsNotCompiled($builder->build(), CompiledContainerTest\AbstractClass::class);
}
}
namespace DI\Test\IntegrationTest\CompiledContainerTest;
class Property
{
public $foo;
}
class CustomParentContainer extends \DI\Container
{
}
class ConstructorWithTypehint
{
public function __construct(ConstructorWithAnotherTypehint $param)
{
}
}
class ConstructorWithAnotherTypehint
{
public function __construct(\stdClass $param)
{
}
}
class ConstructorWithAbstractClassTypehint
{
public function __construct(AbstractClass $param)
{
}
}
abstract class AbstractClass
{
}
class AllKindsOfInjections
{
public $property;
public $constructorParameter;
public $methodParameter;
public function __construct($constructorParameter)
{
$this->constructorParameter = $constructorParameter;
}
public function method($methodParameter)
{
$this->methodParameter = $methodParameter;
}
}
class Autowireable
{
private $dependency;
public function __construct(AutowireableDependency $dependency)
{
$this->dependency = $dependency;
}
}
class AutowireableDependency
{
}