DITest   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 20
eloc 50
c 8
b 1
f 0
dl 0
loc 134
rs 10

20 Methods

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