|
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
|
|
|
$actual = Container::resolveClosure(static fn () => ''); |
|
20
|
|
|
|
|
21
|
|
|
self::assertSame('', $actual); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function test_static_resolve_callable_with_inner_dependencies_without_dependencies(): void |
|
25
|
|
|
{ |
|
26
|
|
|
$actual = Container::resolveClosure( |
|
27
|
|
|
static fn (ClassWithoutDependencies $object) => serialize($object), |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
self::assertEquals( |
|
31
|
|
|
new ClassWithoutDependencies(), |
|
32
|
|
|
unserialize($actual), |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function test_static_resolve_callable_with_inner_dependencies_with_many_dependencies(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$actual = Container::resolveClosure( |
|
39
|
|
|
static fn (ClassWithRelationship $object) => serialize($object), |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
self::assertEquals( |
|
43
|
|
|
new ClassWithRelationship(new Person(), new Person()), |
|
44
|
|
|
unserialize($actual), |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function test_use_mapped_interface_dependency(): void |
|
49
|
|
|
{ |
|
50
|
|
|
$container = new Container([ |
|
51
|
|
|
PersonInterface::class => Person::class, |
|
52
|
|
|
]); |
|
53
|
|
|
|
|
54
|
|
|
$actual = $container->resolve( |
|
55
|
|
|
static fn (ClassWithInterfaceDependencies $object) => serialize($object), |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
self::assertEquals( |
|
59
|
|
|
new ClassWithInterfaceDependencies(new Person()), |
|
60
|
|
|
unserialize($actual), |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function test_resolve_object_from_callable(): void |
|
65
|
|
|
{ |
|
66
|
|
|
$person = new Person(); |
|
67
|
|
|
$person->name = 'person-name'; |
|
68
|
|
|
|
|
69
|
|
|
$container = new Container([ |
|
70
|
|
|
PersonInterface::class => static fn () => $person, |
|
71
|
|
|
]); |
|
72
|
|
|
|
|
73
|
|
|
$actual = $container->resolve( |
|
74
|
|
|
static fn (PersonInterface $object) => serialize($object), |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
self::assertEquals($person, unserialize($actual)); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|