Test Failed
Push — main ( 64a808...fd2bfe )
by ANDREY
02:26
created

DITest::testNotExistedClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
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
class DITest extends TestCase
68
{
69
    /**
70
     * @var Container
71
     */
72
    private Container $di;
73
74
    public function setUp(): void
75
    {
76
        parent::setUp();
77
        $this->di = new Container();
78
        $this->di->registerContainers([
79
            '\E' => A::class,
80
            'Tests\H' => I::class,
81
        ]);
82
    }
83
84
    public function testNotExistedClass()
85
    {
86
        $this->expectException(NotFoundException::class);
87
        $this->di->get('notExist');
88
    }
89
90
    public function testInitAilasedInterface()
91
    {
92
        $a = $this->di->get('Tests\H');
93
        $this->assertTrue($a instanceof I);
94
    }
95
96
    public function testInitClassWithEmptyConstructor()
97
    {
98
        $k = $this->di->get(K::class);
99
        $this->assertTrue($k instanceof K);
100
    }
101
102
    public function testInitClassWithoutDependencies()
103
    {
104
        $a = $this->di->get(A::class);
105
        $this->assertTrue($a instanceof A);
106
    }
107
108
    public function testInitClassWithDependencies()
109
    {
110
        $d = $this->di->get(D::class);
111
        $this->assertTrue($d instanceof D);
112
    }
113
114
    public function testInitClassWithParams()
115
    {
116
        $b = $this->di->get(B::class, ['num' => 10]);
117
        $this->assertTrue($b instanceof B);
118
        $num = $b->getNum();
119
        $this->assertTrue($num === 10);
120
    }
121
122
    public function testInitClassWithoutAttributeInjection()
123
    {
124
        $this->expectException(NotFoundException::class);
125
        $this->di->get(C::class);
126
    }
127
128
    public function testInitAilasedClass()
129
    {
130
        $a = $this->di->get('\E');
131
        $this->assertTrue($a instanceof A);
132
    }
133
134
    public function testInitDISingleton()
135
    {
136
        $di = new Container();
137
        $a = $di->get(A::class);
138
        $this->assertTrue($a instanceof A);
139
    }
140
141
    public function testInitDISingletonAliasedClass()
142
    {
143
        $di = new Container();
144
        $a = $di->get('\E');
145
        $this->assertTrue($a instanceof A);
146
    }
147
148
    public function testGetClassIfInjectableParentClass()
149
    {
150
        $a = $this->di->get(F::class);
151
        $this->assertTrue($a instanceof F);
152
    }
153
154
    public function testHasInjectableClass()
155
    {
156
        $this->assertTrue($this->di->has(A::class));
157
    }
158
159
    public function testHasInjectableParentClass()
160
    {
161
        $this->assertTrue($this->di->has(F::class));
162
    }
163
164
    public function testHasNotInjectableClass()
165
    {
166
        $this->assertFalse($this->di->has(C::class));
167
    }
168
169
    public function testGetClassIfInjectableInterface()
170
    {
171
        $a = $this->di->get(I::class);
172
        $this->assertTrue($a instanceof I);
173
    }
174
175
    public function testGetClassIfInjectableInterfaceWithDisabledBubblePropagation()
176
    {
177
        $this->expectException(NotFoundException::class);
178
        $this->di->setBubblePropagation(false);
179
        $a = $this->di->get(I::class);
180
        $this->assertFalse($a instanceof I);
181
    }
182
183
    public function testHasInjectableParentClassWithDisabledBubblePropagation()
184
    {
185
        $this->di->setBubblePropagation(false);
186
        $this->assertFalse($this->di->has(C::class));
187
    }
188
189
    public function testGetClassIfInjectableParentClassWithDisabledBubblePropagation()
190
    {
191
        $this->expectException(NotFoundException::class);
192
        $this->di->setBubblePropagation(false);
193
        $this->di->get(F::class);
194
    }
195
196
197
}