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
|
|
|
|
24
|
|
|
#[Injectable] |
25
|
|
|
class D |
26
|
|
|
{ |
27
|
|
|
public function __construct(protected A $a) |
28
|
|
|
{ |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
class F extends A |
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
#[Injectable] |
37
|
|
|
interface H |
38
|
|
|
{ |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
class I implements H |
42
|
|
|
{ |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
class C |
46
|
|
|
{ |
47
|
|
|
public function __construct(protected A $a) |
48
|
|
|
{ |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
class DITest extends TestCase |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* @var Container |
57
|
|
|
*/ |
58
|
|
|
private Container $di; |
59
|
|
|
|
60
|
|
|
public function setUp(): void |
61
|
|
|
{ |
62
|
|
|
parent::setUp(); |
63
|
|
|
$this->di = new Container(); |
64
|
|
|
$this->di->registerContainers([ |
65
|
|
|
'\E' => A::class, |
66
|
|
|
'Tests\H' => I::class |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testInitAilasedInterface() |
71
|
|
|
{ |
72
|
|
|
$a = $this->di->get('Tests\H'); |
73
|
|
|
$this->assertTrue($a instanceof I); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testInitClassWithoutDependencies() |
77
|
|
|
{ |
78
|
|
|
$a = $this->di->get(A::class); |
79
|
|
|
$this->assertTrue($a instanceof A); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testInitClassWithDependencies() |
83
|
|
|
{ |
84
|
|
|
$d = $this->di->get(D::class); |
85
|
|
|
$this->assertTrue($d instanceof D); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testInitClassWithParams() |
89
|
|
|
{ |
90
|
|
|
$b = $this->di->get(B::class, ['num' => 10]); |
91
|
|
|
$this->assertTrue($b instanceof B); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testInitClassWithoutAttributeInjection() |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
|
|
$this->di->get(C::class); |
98
|
|
|
$this->assertTrue(false); |
99
|
|
|
} catch (NotFoundException $e) { |
100
|
|
|
$this->assertTrue(true); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testInitAilasedClass() |
105
|
|
|
{ |
106
|
|
|
$a = $this->di->get('\E'); |
107
|
|
|
$this->assertTrue($a instanceof A); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testInitDISingleton() |
111
|
|
|
{ |
112
|
|
|
$di = new Container(); |
113
|
|
|
$a = $di->get(A::class); |
114
|
|
|
$this->assertTrue($a instanceof A); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testInitDISingletonAliasedClass() |
118
|
|
|
{ |
119
|
|
|
$di = new Container(); |
120
|
|
|
$a = $di->get('\E'); |
121
|
|
|
$this->assertTrue($a instanceof A); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testGetClassIfInjectableParentClass() |
125
|
|
|
{ |
126
|
|
|
$a = $this->di->get(F::class); |
127
|
|
|
$this->assertTrue($a instanceof F); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testHasInjectableClass() |
131
|
|
|
{ |
132
|
|
|
$this->assertTrue($this->di->has(A::class)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testHasInjectableParentClass() |
136
|
|
|
{ |
137
|
|
|
$this->assertTrue($this->di->has(F::class)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testHasNotInjectableClass() |
141
|
|
|
{ |
142
|
|
|
$this->assertFalse($this->di->has(C::class)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testGetClassIfInjectableInterface() |
146
|
|
|
{ |
147
|
|
|
$a = $this->di->get(I::class); |
148
|
|
|
$this->assertTrue($a instanceof I); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function testGetClassIfInjectableInterfaceWithDisabledBubblePropagation() |
152
|
|
|
{ |
153
|
|
|
try { |
154
|
|
|
$this->di->setBubblePropagation(false); |
155
|
|
|
$a = $this->di->get(I::class); |
156
|
|
|
$this->assertFalse($a instanceof I); |
157
|
|
|
} catch (NotFoundException $e) { |
158
|
|
|
$this->assertTrue(true); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testHasInjectableParentClassWithDisabledBubblePropagation() |
163
|
|
|
{ |
164
|
|
|
$this->di->setBubblePropagation(false); |
165
|
|
|
$this->assertFalse($this->di->has(C::class)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testGetClassIfInjectableParentClassWithDisabledBubblePropagation() |
169
|
|
|
{ |
170
|
|
|
try { |
171
|
|
|
$this->di->setBubblePropagation(false); |
172
|
|
|
$this->di->get(F::class); |
173
|
|
|
} catch (NotFoundException $e) { |
174
|
|
|
$this->assertTrue(true); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
} |