1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Tests; |
5
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use VPA\DI\Container; |
8
|
|
|
use VPA\DI\Injectable; |
9
|
|
|
use VPA\DI\NotFoundException; |
10
|
|
|
|
11
|
|
|
#[Injectable] |
12
|
|
|
class A |
13
|
|
|
{ |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
#[Injectable] |
17
|
|
|
class B |
18
|
|
|
{ |
19
|
|
|
public function __construct(protected A $a, private int $num) |
20
|
|
|
{ |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function getNum(): int |
24
|
|
|
{ |
25
|
|
|
return $this->num; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
#[Injectable] |
30
|
|
|
class D |
31
|
|
|
{ |
32
|
|
|
public function __construct(protected A $a) |
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
class F extends A |
38
|
|
|
{ |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
#[Injectable] |
42
|
|
|
interface H |
43
|
|
|
{ |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
class I implements H |
47
|
|
|
{ |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
class C |
51
|
|
|
{ |
52
|
|
|
public function __construct(protected A $a) |
53
|
|
|
{ |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
#[Injectable] |
58
|
|
|
class K |
59
|
|
|
{ |
60
|
|
|
// Constructor with empty params |
61
|
|
|
public function __construct() |
62
|
|
|
{ |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
class DITest extends TestCase |
69
|
|
|
{ |
70
|
|
|
/** |
71
|
|
|
* @var Container |
72
|
|
|
*/ |
73
|
|
|
private Container $di; |
74
|
|
|
|
75
|
|
|
public function setUp(): void |
76
|
|
|
{ |
77
|
|
|
parent::setUp(); |
78
|
|
|
$this->di = new Container(); |
79
|
|
|
$this->di->registerContainers([ |
80
|
|
|
'\E' => A::class, |
81
|
|
|
'Tests\H' => I::class |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testInitAilasedInterface() |
86
|
|
|
{ |
87
|
|
|
$a = $this->di->get('Tests\H'); |
88
|
|
|
$this->assertTrue($a instanceof I); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testInitClassWithEmptyConstructor() |
92
|
|
|
{ |
93
|
|
|
$k = $this->di->get(K::class); |
94
|
|
|
$this->assertTrue($k instanceof K); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testInitClassWithoutDependencies() |
98
|
|
|
{ |
99
|
|
|
$a = $this->di->get(A::class); |
100
|
|
|
$this->assertTrue($a instanceof A); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testInitClassWithDependencies() |
104
|
|
|
{ |
105
|
|
|
$d = $this->di->get(D::class); |
106
|
|
|
$this->assertTrue($d instanceof D); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testInitClassWithParams() |
110
|
|
|
{ |
111
|
|
|
$b = $this->di->get(B::class, ['num' => 10]); |
112
|
|
|
$this->assertTrue($b instanceof B); |
113
|
|
|
$num = $b->getNum(); |
114
|
|
|
$this->assertTrue($num === 10); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testInitClassWithoutAttributeInjection() |
118
|
|
|
{ |
119
|
|
|
$this->expectException(NotFoundException::class); |
120
|
|
|
$this->di->get(C::class); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testInitAilasedClass() |
124
|
|
|
{ |
125
|
|
|
$a = $this->di->get('\E'); |
126
|
|
|
$this->assertTrue($a instanceof A); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testInitDISingleton() |
130
|
|
|
{ |
131
|
|
|
$di = new Container(); |
132
|
|
|
$a = $di->get(A::class); |
133
|
|
|
$this->assertTrue($a instanceof A); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testInitDISingletonAliasedClass() |
137
|
|
|
{ |
138
|
|
|
$di = new Container(); |
139
|
|
|
$a = $di->get('\E'); |
140
|
|
|
$this->assertTrue($a instanceof A); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testGetClassIfInjectableParentClass() |
144
|
|
|
{ |
145
|
|
|
$a = $this->di->get(F::class); |
146
|
|
|
$this->assertTrue($a instanceof F); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testHasInjectableClass() |
150
|
|
|
{ |
151
|
|
|
$this->assertTrue($this->di->has(A::class)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testHasInjectableParentClass() |
155
|
|
|
{ |
156
|
|
|
$this->assertTrue($this->di->has(F::class)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testHasNotInjectableClass() |
160
|
|
|
{ |
161
|
|
|
$this->assertFalse($this->di->has(C::class)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testGetClassIfInjectableInterface() |
165
|
|
|
{ |
166
|
|
|
$a = $this->di->get(I::class); |
167
|
|
|
$this->assertTrue($a instanceof I); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function testGetClassIfInjectableInterfaceWithDisabledBubblePropagation() |
171
|
|
|
{ |
172
|
|
|
$this->expectException(NotFoundException::class); |
173
|
|
|
$this->di->setBubblePropagation(false); |
174
|
|
|
$a = $this->di->get(I::class); |
175
|
|
|
$this->assertFalse($a instanceof I); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testHasInjectableParentClassWithDisabledBubblePropagation() |
179
|
|
|
{ |
180
|
|
|
$this->di->setBubblePropagation(false); |
181
|
|
|
$this->assertFalse($this->di->has(C::class)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function testGetClassIfInjectableParentClassWithDisabledBubblePropagation() |
185
|
|
|
{ |
186
|
|
|
$this->expectException(NotFoundException::class); |
187
|
|
|
$this->di->setBubblePropagation(false); |
188
|
|
|
$this->di->get(F::class); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
} |