|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GacelaTest\Unit\Framework\Container; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\Container\Container; |
|
8
|
|
|
use Gacela\Framework\Container\Exception\ContainerException; |
|
9
|
|
|
use Gacela\Framework\Container\Exception\ContainerKeyNotFoundException; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use stdClass; |
|
12
|
|
|
|
|
13
|
|
|
final class ContainerTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
private Container $container; |
|
16
|
|
|
|
|
17
|
|
|
public function setUp(): void |
|
18
|
|
|
{ |
|
19
|
|
|
$this->container = new Container(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function test_get_non_existing_service(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$this->expectException(ContainerKeyNotFoundException::class); |
|
25
|
|
|
$this->container->get('unknown-service_name'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function test_has_service(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->container->set('service_name', 'value'); |
|
31
|
|
|
|
|
32
|
|
|
self::assertTrue($this->container->has('service_name')); |
|
33
|
|
|
self::assertFalse($this->container->has('unknown-service_name')); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function test_remove_existing_service(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->container->set('service_name', 'value'); |
|
39
|
|
|
$this->container->remove('service_name'); |
|
40
|
|
|
|
|
41
|
|
|
$this->expectException(ContainerKeyNotFoundException::class); |
|
42
|
|
|
$this->container->get('service_name'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function test_resolve_service_as_raw_string(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$this->container->set('service_name', 'value'); |
|
48
|
|
|
|
|
49
|
|
|
$resolvedService = $this->container->get('service_name'); |
|
50
|
|
|
self::assertSame('value', $resolvedService); |
|
51
|
|
|
|
|
52
|
|
|
$cachedResolvedService = $this->container->get('service_name'); |
|
53
|
|
|
self::assertSame('value', $cachedResolvedService); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function test_resolve_service_as_function(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$this->container->set('service_name', static fn (): string => 'value'); |
|
59
|
|
|
|
|
60
|
|
|
$resolvedService = $this->container->get('service_name'); |
|
61
|
|
|
self::assertSame('value', $resolvedService); |
|
62
|
|
|
|
|
63
|
|
|
$cachedResolvedService = $this->container->get('service_name'); |
|
64
|
|
|
self::assertSame('value', $cachedResolvedService); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function test_resolve_service_as_callable_class(): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->container->set( |
|
70
|
|
|
'service_name', |
|
71
|
|
|
new class() { |
|
72
|
|
|
public function __invoke(): string |
|
73
|
|
|
{ |
|
74
|
|
|
return 'value'; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
$resolvedService = $this->container->get('service_name'); |
|
80
|
|
|
self::assertSame('value', $resolvedService); |
|
81
|
|
|
|
|
82
|
|
|
$cachedResolvedService = $this->container->get('service_name'); |
|
83
|
|
|
self::assertSame('value', $cachedResolvedService); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function test_resolve_non_factory_service_with_random(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->container->set( |
|
89
|
|
|
'service_name', |
|
90
|
|
|
new class() { |
|
91
|
|
|
public function __invoke(): string |
|
92
|
|
|
{ |
|
93
|
|
|
return 'value_' . random_int(0, PHP_INT_MAX); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
self::assertSame( |
|
99
|
|
|
$this->container->get('service_name'), |
|
100
|
|
|
$this->container->get('service_name') |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function test_resolve_factory_service_with_random(): void |
|
105
|
|
|
{ |
|
106
|
|
|
$this->container->set( |
|
107
|
|
|
'service_name', |
|
108
|
|
|
$this->container->factory( |
|
109
|
|
|
new class() { |
|
110
|
|
|
public function __invoke(): string |
|
111
|
|
|
{ |
|
112
|
|
|
return 'value_' . random_int(0, PHP_INT_MAX); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
) |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
self::assertNotSame( |
|
119
|
|
|
$this->container->get('service_name'), |
|
120
|
|
|
$this->container->get('service_name') |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function test_resolve_factory_service_not_invokable(): void |
|
125
|
|
|
{ |
|
126
|
|
|
$this->expectExceptionObject(ContainerException::serviceNotInvokable()); |
|
127
|
|
|
|
|
128
|
|
|
$this->container->set( |
|
129
|
|
|
'service_name', |
|
130
|
|
|
$this->container->factory(new stdClass()) |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|