1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GacelaTest\Unit; |
6
|
|
|
|
7
|
|
|
use Gacela\Container\Container; |
8
|
|
|
use GacelaTest\Fake\ClassWithInterfaceDependencies; |
9
|
|
|
use GacelaTest\Fake\ClassWithoutDependencies; |
10
|
|
|
use GacelaTest\Fake\ClassWithRelationship; |
11
|
|
|
use GacelaTest\Fake\Person; |
12
|
|
|
use GacelaTest\Fake\PersonInterface; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
final class ClosureContainerTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function test_static_create_without_dependencies(): void |
18
|
|
|
{ |
19
|
|
|
$container = new Container(); |
20
|
|
|
|
21
|
|
|
$actual = $container->resolve(static fn () => ''); |
22
|
|
|
|
23
|
|
|
self::assertSame('', $actual); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function test_static_resolve_callable_with_inner_dependencies_without_dependencies(): void |
27
|
|
|
{ |
28
|
|
|
$container = new Container(); |
29
|
|
|
|
30
|
|
|
$actual = $container->resolve( |
31
|
|
|
static fn (ClassWithoutDependencies $object) => serialize($object), |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
self::assertEquals( |
35
|
|
|
new ClassWithoutDependencies(), |
36
|
|
|
unserialize($actual), |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function test_static_resolve_callable_with_inner_dependencies_with_many_dependencies(): void |
41
|
|
|
{ |
42
|
|
|
$container = new Container(); |
43
|
|
|
|
44
|
|
|
$actual = $container->resolve( |
45
|
|
|
static fn (ClassWithRelationship $object) => serialize($object), |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
self::assertEquals( |
49
|
|
|
new ClassWithRelationship(new Person(), new Person()), |
50
|
|
|
unserialize($actual), |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function test_use_mapped_interface_dependency(): void |
55
|
|
|
{ |
56
|
|
|
$container = new Container([ |
57
|
|
|
PersonInterface::class => Person::class, |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$actual = $container->resolve( |
61
|
|
|
static fn (ClassWithInterfaceDependencies $object) => serialize($object), |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
self::assertEquals( |
65
|
|
|
new ClassWithInterfaceDependencies(new Person()), |
66
|
|
|
unserialize($actual), |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function test_resolve_object_from_callable(): void |
71
|
|
|
{ |
72
|
|
|
$person = new Person(); |
73
|
|
|
$person->name = 'person-name'; |
74
|
|
|
|
75
|
|
|
$container = new Container([ |
76
|
|
|
PersonInterface::class => static fn () => $person, |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$actual = $container->resolve( |
80
|
|
|
static fn (PersonInterface $object) => serialize($object), |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
self::assertEquals($person, unserialize($actual)); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|