Passed
Push — main ( d186ad...4a5d16 )
by ANDREY
02:20 queued 13s
created

DITest::testHasNotInjectableClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
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
#[Injectable]
16
class B {
17
18
    public function __construct(protected A $a, private int $num) {
19
    }
20
}
21
22
#[Injectable]
23
class D {
24
25
    public function __construct(protected A $a) {
26
    }
27
}
28
29
class F extends A {
30
}
31
32
class C {
33
34
    public function __construct(protected A $a) {
35
    }
36
}
37
38
39
class DITest extends TestCase
40
{
41
    /**
42
     * @var Container
43
     */
44
    private Container $di;
45
46
    public function setUp(): void
47
    {
48
        parent::setUp();
49
        $this->di = new Container();
50
        $this->di->registerContainers([
51
            '\E'=>A::class
52
        ]);
53
    }
54
55
    public function testInitClassWithoutDependencies()
56
    {
57
        $a = $this->di->get(A::class);
58
        $this->assertTrue($a instanceof A);
59
    }
60
61
    public function testInitClassWithDependencies()
62
    {
63
        $d = $this->di->get(D::class);
64
        $this->assertTrue($d instanceof D);
65
    }
66
67
    public function testInitClassWithParams()
68
    {
69
        $b = $this->di->get(B::class, ['num'=>10]);
70
        $this->assertTrue($b instanceof B);
71
    }
72
73
    public function testInitClassWithoutAttributeInjection()
74
    {
75
        try {
76
            $this->di->get(C::class);
77
            $this->assertTrue(false);
78
        } catch (NotFoundException $e) {
79
            $this->assertTrue(true);
80
        }
81
    }
82
83
    public function testInitAilasedClass()
84
    {
85
        $a = $this->di->get('\E');
86
        $this->assertTrue($a instanceof A);
87
    }
88
89
    public function testInitDISingleton()
90
    {
91
        $di = new Container();
92
        $a = $di->get(A::class);
93
        $this->assertTrue($a instanceof A);
94
    }
95
96
    public function testInitDISingletonAliasedClass()
97
    {
98
        $di = new Container();
99
        $a = $di->get('\E');
100
        $this->assertTrue($a instanceof A);
101
    }
102
103
    public function testGetClassIfInjectableParentClass()
104
    {
105
        $a = $this->di->get(F::class);
106
        $this->assertTrue($a instanceof F);
107
    }
108
109
    public function testHasInjectableClass()
110
    {
111
        $this->assertTrue($this->di->has(A::class));
112
    }
113
114
    public function testHasInjectableParentClass()
115
    {
116
        $this->assertTrue($this->di->has(F::class));
117
    }
118
119
    public function testHasNotInjectableClass()
120
    {
121
        $this->assertFalse($this->di->has(C::class));
122
    }
123
124
    public function testHasInjectableParentClassWithDisabledBubblePropagation()
125
    {
126
        $this->di->setBubblePropagation(false);
127
        $this->assertFalse($this->di->has(C::class));
128
    }
129
130
    public function testGetClassIfInjectableParentClassWithDisabledBubblePropagation()
131
    {
132
        try {
133
            $this->di->setBubblePropagation(false);
134
            $this->di->get(F::class);
135
        } catch (NotFoundException $e) {
136
            $this->assertTrue(true);
137
        }
138
    }
139
140
141
}