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 |
|
|
|
|
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')); |
|
|
|
|
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')); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths