Passed
Pull Request — main (#2)
by ANDREY
02:11
created

B   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 10
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 2
eloc 3
c 4
b 0
f 0
dl 0
loc 10
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A getNum() 0 3 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
58
class DITest extends TestCase
59
{
60
    /**
61
     * @var Container
62
     */
63
    private Container $di;
64
65
    public function setUp(): void
66
    {
67
        parent::setUp();
68
        $this->di = new Container();
69
        $this->di->registerContainers([
70
            '\E' => A::class,
71
            'Tests\H' => I::class
72
        ]);
73
    }
74
75
    public function testInitAilasedInterface()
76
    {
77
        $a = $this->di->get('Tests\H');
78
        $this->assertTrue($a instanceof I);
79
    }
80
81
    public function testInitClassWithoutDependencies()
82
    {
83
        $a = $this->di->get(A::class);
84
        $this->assertTrue($a instanceof A);
85
    }
86
87
    public function testInitClassWithDependencies()
88
    {
89
        $d = $this->di->get(D::class);
90
        $this->assertTrue($d instanceof D);
91
    }
92
93
    public function testInitClassWithParams()
94
    {
95
        $b = $this->di->get(B::class, ['num' => 10]);
96
        $this->assertTrue($b instanceof B);
97
        $num = $b->getNum();
98
        $this->assertTrue($num === 10);
99
    }
100
101
    public function testInitClassWithoutAttributeInjection()
102
    {
103
        $this->expectException(NotFoundException::class);
104
        $this->di->get(C::class);
105
    }
106
107
    public function testInitAilasedClass()
108
    {
109
        $a = $this->di->get('\E');
110
        $this->assertTrue($a instanceof A);
111
    }
112
113
    public function testInitDISingleton()
114
    {
115
        $di = new Container();
116
        $a = $di->get(A::class);
117
        $this->assertTrue($a instanceof A);
118
    }
119
120
    public function testInitDISingletonAliasedClass()
121
    {
122
        $di = new Container();
123
        $a = $di->get('\E');
124
        $this->assertTrue($a instanceof A);
125
    }
126
127
    public function testGetClassIfInjectableParentClass()
128
    {
129
        $a = $this->di->get(F::class);
130
        $this->assertTrue($a instanceof F);
131
    }
132
133
    public function testHasInjectableClass()
134
    {
135
        $this->assertTrue($this->di->has(A::class));
136
    }
137
138
    public function testHasInjectableParentClass()
139
    {
140
        $this->assertTrue($this->di->has(F::class));
141
    }
142
143
    public function testHasNotInjectableClass()
144
    {
145
        $this->assertFalse($this->di->has(C::class));
146
    }
147
148
    public function testGetClassIfInjectableInterface()
149
    {
150
        $a = $this->di->get(I::class);
151
        $this->assertTrue($a instanceof I);
152
    }
153
154
    public function testGetClassIfInjectableInterfaceWithDisabledBubblePropagation()
155
    {
156
        $this->expectException(NotFoundException::class);
157
        $this->di->setBubblePropagation(false);
158
        $a = $this->di->get(I::class);
159
        $this->assertFalse($a instanceof I);
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
        $this->expectException(NotFoundException::class);
171
        $this->di->setBubblePropagation(false);
172
        $this->di->get(F::class);
173
    }
174
175
176
}