Completed
Branch master (2230cd)
by Max
06:42 queued 03:04
created

ContainerTest::testAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * @copyright (c) 2018 Mendel <[email protected]>
4
 * @license see license.txt
5
 */
6
namespace drycart\di\tests;
7
8
9
/**
10
 * Description of DiTest
11
 *
12
 * @author mendel
13
 */
14
class ContainerTest extends \PHPUnit\Framework\TestCase
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
{
16
    
17
    public function testInstantiate()
18
    {
19
        $di = new \drycart\di\Container();
20
        $di->setConfig([
21
            'stdClass' => ['#class'=>'stdClass']
22
        ]);
23
        $obj = $di->get('stdClass');
24
        $this->assertTrue(is_a($obj, 'stdClass'));
25
    }
26
    
27
    public function testAlias()
28
    {
29
        $di = new \drycart\di\Container();
30
        $di->setConfig([
31
            '#other' => ['#class'=>'stdClass']
32
        ]);
33
        $obj = $di->get('#other');
34
        $this->assertTrue(is_a($obj, 'stdClass'));
35
    }
36
    
37
    public function testNotFoundParameters()
38
    {
39
        $di = new \drycart\di\Container();
40
        $this->expectException(\drycart\di\ContainerException::class);
41
        $this->expectExceptionMessage('Unknown parameter intDummy');
42
        $di->get('drycart\di\tests\DummyPlusParameter');
43
    }
44
    
45
    public function testDefaultParams()
46
    {
47
        $di = new \drycart\di\Container();
48
        $di->setConfig([
49
            'drycart\di\tests\DummyInterface' => ['#class'=>'drycart\di\tests\DummyComplex']
50
        ]);
51
        $obj = $di->get('drycart\di\tests\DummyInterface');
52
        $this->assertTrue(is_a($obj, 'drycart\di\tests\DummyInterface'));
53
    }
54
55
    public function testNoConfig()
56
    {
57
        $di = new \drycart\di\Container();
58
        $obj = $di->get('stdClass');
59
        $this->assertTrue(is_a($obj, 'stdClass'));
60
    }
61
62
    public function testNotFound()
63
    {
64
        $di = new \drycart\di\Container();
65
        $this->expectException(\drycart\di\NotFoundException::class);
66
        $di->get('NotExistClass');
67
    }
68
    
69
    public function testSinglton()
70
    {
71
        $di = new \drycart\di\Container();
72
        $di->setConfig([
73
            'drycart\di\tests\Dummy' => [
74
                '#singleton' => true
75
            ]
76
        ]);
77
        $obj1 = $di->get('drycart\di\tests\Dummy');
78
        $obj2 = $di->get('drycart\di\tests\Dummy');
79
        $this->assertEquals(spl_object_id($obj1), spl_object_id($obj2));
80
    }
81
    
82
    public function testFactory()
83
    {
84
        $di = new \drycart\di\Container();
85
        $di->setConfig([
86
            'drycart\di\tests\Dummy' => [
87
                '#singleton' => false
88
            ]
89
        ]);
90
        $obj1 = $di->get('drycart\di\tests\Dummy');
91
        $obj2 = $di->get('drycart\di\tests\Dummy');
92
        $this->assertNotEquals(spl_object_id($obj1), spl_object_id($obj2));
93
    }
94
    
95
    public function testWrongClass()
96
    {
97
        $di = new \drycart\di\Container();
98
        $di->setConfig([
99
            'drycart\di\tests\DummyInterface' => ['#class'=>'stdClass']
100
        ]);
101
        $this->expectException(\drycart\di\ContainerException::class);
102
        $this->expectExceptionMessage('Wrong class, will be drycart\di\tests\DummyInterface');
103
        $di->get('drycart\di\tests\DummyInterface');
104
    }
105
    
106
    public function testSpeed()
107
    {
108
        $di = new \drycart\di\Container();
109
        $di->setConfig([
110
            'drycart\di\tests\DummyInterface' => ['#class'=>'drycart\di\tests\DummyComplex']
111
        ]);
112
        for ($i=0;$i<1000;$i++) {
113
            $obj = $di->make('drycart\di\tests\DummyInterface', ['intDummy'=>$i]);
114
//            $obj = new DummyComplex($i, null);
115
        }
116
        $this->assertTrue(is_a($obj, 'drycart\di\tests\DummyInterface'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $obj does not seem to be defined for all execution paths leading up to this point.
Loading history...
117
    }
118
    
119
    public function testSpeed2()
120
    {
121
        $di = new \drycart\di\Container();
122
        $di->setConfig([
123
            'stdClass' => ['#class'=>'stdClass']
124
        ]);
125
        $className = 'stdClass';
126
        for ($i=0;$i<10000;$i++) {
127
            $obj = $di->get($className);
128
//            $obj = new $className;
129
        }
130
        $this->assertTrue(is_a($obj, 'stdClass'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $obj does not seem to be defined for all execution paths leading up to this point.
Loading history...
131
    }
132
    
133
}
134