drycart /
di
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * @copyright (c) 2018 Mendel <[email protected]> |
||
| 4 | * @license see license.txt |
||
| 5 | */ |
||
| 6 | namespace drycart\di\tests; |
||
| 7 | use PHPUnit\Framework\TestCase; |
||
|
0 ignored issues
–
show
|
|||
| 8 | |||
| 9 | /** |
||
| 10 | * Description of DiTest |
||
| 11 | * |
||
| 12 | * @author mendel |
||
| 13 | */ |
||
| 14 | class ExtendedTest extends TestCase |
||
| 15 | { |
||
| 16 | public function testMake() |
||
| 17 | { |
||
| 18 | $di = new \drycart\di\Container(); |
||
| 19 | $obj = $di->make('drycart\di\tests\dummy\DummyPlusParameter', ['intDummy'=> 0]); |
||
| 20 | $this->assertTrue(is_a($obj, 'drycart\di\tests\dummy\DummyPlusParameter')); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function testMakeException() |
||
| 24 | { |
||
| 25 | $di = new \drycart\di\Container(); |
||
| 26 | $di->setConfig([ |
||
| 27 | 'drycart\di\tests\dummy\Dummy' => [ |
||
| 28 | '#singleton' => true |
||
| 29 | ] |
||
| 30 | ]); |
||
| 31 | $this->expectException(\drycart\di\ContainerException::class); |
||
| 32 | $this->expectExceptionMessage('drycart\di\tests\dummy\Dummy is singlton!'); |
||
| 33 | $di->make('drycart\di\tests\dummy\Dummy'); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function testMakeParamsNoType() |
||
| 37 | { |
||
| 38 | $di = new \drycart\di\Container(); |
||
| 39 | $di->setConfig([ |
||
| 40 | 'drycart\di\tests\dummy\DummyInterface' => [ |
||
| 41 | '#class'=>'drycart\di\tests\dummy\DummyComplex', |
||
| 42 | 'noTypeParameter' => 5 |
||
| 43 | ] |
||
| 44 | ]); |
||
| 45 | $obj = $di->get('drycart\di\tests\dummy\DummyInterface'); |
||
| 46 | $this->assertTrue(is_a($obj, 'drycart\di\tests\dummy\DummyInterface')); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function testMakePreparedParameters() |
||
| 50 | { |
||
| 51 | $di = new \drycart\di\Container(); |
||
| 52 | $dummy = $di->get('drycart\di\tests\dummy\Dummy'); |
||
| 53 | $obj = $di->make('drycart\di\tests\dummy\DummyPlusParameter', [ |
||
| 54 | 'intDummy'=> 0, |
||
| 55 | 'dummy'=>$dummy |
||
| 56 | ]); |
||
| 57 | $this->assertTrue(is_a($obj, 'drycart\di\tests\dummy\DummyPlusParameter')); |
||
| 58 | } |
||
| 59 | |||
| 60 | public function testMakeFromArrayParameter() |
||
| 61 | { |
||
| 62 | $di = new \drycart\di\Container(); |
||
| 63 | $obj = $di->make('drycart\di\tests\dummy\DummyPlusParameter', [ |
||
| 64 | 'intDummy'=> 0, |
||
| 65 | 'dummy'=>['#class'=>'drycart\di\tests\dummy\Dummy'] |
||
| 66 | ]); |
||
| 67 | $this->assertTrue(is_a($obj, 'drycart\di\tests\dummy\DummyPlusParameter')); |
||
| 68 | } |
||
| 69 | |||
| 70 | public function testTransformer() |
||
| 71 | { |
||
| 72 | $di = new \drycart\di\Container(); |
||
| 73 | $di->addTransformer(function($value, string $className, \drycart\di\DiInterface $container) { |
||
| 74 | return $container->get($className); |
||
| 75 | }); |
||
| 76 | $obj = $di->make('drycart\di\tests\dummy\DummyPlusParameter', [ |
||
| 77 | 'intDummy'=> 0, |
||
| 78 | 'dummy'=> new \stdClass() |
||
| 79 | ]); |
||
| 80 | $this->assertTrue(is_a($obj, 'drycart\di\tests\dummy\DummyPlusParameter')); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function testParent() |
||
| 84 | { |
||
| 85 | $di = new \drycart\di\Container(); |
||
| 86 | $di->setConfig([ |
||
| 87 | 'drycart\di\tests\dummy\Dummy' => [ |
||
| 88 | '#singleton' => true |
||
| 89 | ] |
||
| 90 | ]); |
||
| 91 | $obj = $di->singleton('drycart\di\tests\dummy\DummyExtended'); |
||
| 92 | $this->assertTrue(is_a($obj, 'drycart\di\tests\dummy\DummyExtended')); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function testParentAlias() |
||
| 96 | { |
||
| 97 | $di = new \drycart\di\Container(); |
||
| 98 | $di->setConfig([ |
||
| 99 | '#other' => ['#class'=>'stdClass'], |
||
| 100 | '#other:children' => [] |
||
| 101 | ]); |
||
| 102 | $obj = $di->get('#other:children'); |
||
| 103 | $this->assertTrue(is_a($obj, 'stdClass')); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function testFactory1() |
||
| 107 | { |
||
| 108 | $di = new \drycart\di\Container(); |
||
| 109 | $di->addClass(\stdClass::class, [ |
||
| 110 | '#factory' => function($config, $container) { |
||
| 111 | unset($config['#factory']); |
||
| 112 | unset($config['#class']); |
||
| 113 | unset($config['#singleton']); |
||
| 114 | $obj = (object) $config; |
||
| 115 | $obj->meta = 'meta'; |
||
| 116 | return $obj; |
||
| 117 | }]); |
||
| 118 | $obj = $di->make(\stdClass::class, [ |
||
| 119 | 'id'=> 555, |
||
| 120 | ]); |
||
| 121 | $this->assertEquals($obj->id, 555); |
||
| 122 | $this->assertEquals($obj->meta, 'meta'); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function testFactory2() |
||
| 126 | { |
||
| 127 | $di = new \drycart\di\Container(); |
||
| 128 | $di->addClass(\drycart\di\tests\dummy\Dummy::class, [ |
||
| 129 | '#factory' => function($config, $container) { |
||
| 130 | $className = $config['#class']; |
||
| 131 | $obj = new $className; |
||
| 132 | $obj->meta = 'meta'; |
||
| 133 | return $obj; |
||
| 134 | }]); |
||
| 135 | $obj = $di->make('drycart\di\tests\dummy\DummyExtended'); |
||
| 136 | $this->assertTrue(is_a($obj, 'drycart\di\tests\dummy\DummyExtended')); |
||
| 137 | $this->assertEquals($obj->meta, 'meta'); |
||
| 138 | } |
||
| 139 | |||
| 140 | public function testMakeWrongParameterTypeException() |
||
| 141 | { |
||
| 142 | $di = new \drycart\di\Container(); |
||
| 143 | $this->expectException(\drycart\di\ContainerException::class); |
||
| 144 | $this->expectExceptionMessage('Wrong type of value for parameter'); |
||
| 145 | $di->make('drycart\di\tests\dummy\DummyPlusParameter', [ |
||
| 146 | 'intDummy'=> 0, |
||
| 147 | 'dummy'=> new \stdClass() |
||
| 148 | ]); |
||
| 149 | } |
||
| 150 | |||
| 151 | public function testSingletoneException() |
||
| 152 | { |
||
| 153 | $di = new \drycart\di\Container(); |
||
| 154 | $di->setConfig([ |
||
| 155 | 'drycart\di\tests\dummy\Dummy' => [ |
||
| 156 | '#singleton' => false |
||
| 157 | ] |
||
| 158 | ]); |
||
| 159 | $this->expectException(\drycart\di\ContainerException::class); |
||
| 160 | $this->expectExceptionMessage('drycart\di\tests\dummy\Dummy NOT singlton!'); |
||
| 161 | $di->singleton('drycart\di\tests\dummy\Dummy'); |
||
| 162 | } |
||
| 163 | |||
| 164 | public function testMakeConfig() |
||
| 165 | { |
||
| 166 | $di = new \drycart\di\Container(); |
||
| 167 | $di->setConfig([ |
||
| 168 | 'drycart\di\tests\dummy\DummyMakeConfig' => [ |
||
| 169 | 'dummy' => ['i'=>5], |
||
| 170 | 'intDummy'=> 5 |
||
| 171 | ], |
||
| 172 | 'drycart\di\tests\dummy\DummyInterface' => [ |
||
| 173 | '#class'=>'drycart\di\tests\dummy\DummyToMake' |
||
| 174 | ] |
||
| 175 | ]); |
||
| 176 | $obj = $di->get('drycart\di\tests\dummy\DummyMakeConfig'); |
||
| 177 | $this->assertTrue(is_a($obj->dummy, 'drycart\di\tests\dummy\DummyToMake')); |
||
| 178 | } |
||
| 179 | public function testService() |
||
| 180 | { |
||
| 181 | $di = new \drycart\di\Container(); |
||
| 182 | $di->setConfig([ |
||
| 183 | 'drycart\di\tests\dummy\Dummy' => [ |
||
| 184 | '#singleton' => true |
||
| 185 | ] |
||
| 186 | ]); |
||
| 187 | $obj1 = $di->singleton('drycart\di\tests\dummy\Dummy'); |
||
| 188 | $obj2 = $di->get('drycart\di\tests\dummy\Dummy'); |
||
| 189 | $this->assertEquals(spl_object_id($obj1), spl_object_id($obj2)); |
||
| 190 | } |
||
| 191 | |||
| 192 | public function testSingletonDefault() |
||
| 193 | { |
||
| 194 | // For reset if not new.. Yes, static is not tested, but.... |
||
| 195 | \drycart\di\Di::setInstance(null); |
||
| 196 | // |
||
| 197 | $di1 = \drycart\di\Di::getInstance(); |
||
| 198 | $di2 = \drycart\di\Di::getInstance(); |
||
| 199 | $this->assertEquals(spl_object_id($di1), spl_object_id($di2)); |
||
| 200 | } |
||
| 201 | |||
| 202 | public function testSingleton() |
||
| 203 | { |
||
| 204 | $di = new \drycart\di\Container(); |
||
| 205 | \drycart\di\Di::setInstance($di); |
||
| 206 | // |
||
| 207 | $di1 = \drycart\di\Di::getInstance(); |
||
| 208 | $di2 = \drycart\di\Di::getInstance(); |
||
| 209 | $this->assertEquals(spl_object_id($di1), spl_object_id($di2)); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function testRequired() |
||
| 213 | { |
||
| 214 | $this->assertFalse(defined('DI_REQUIRED_TEST_CONST')); |
||
| 215 | $di = new \drycart\di\Container(); |
||
| 216 | $di->setConfig([ |
||
| 217 | 'drycart\di\tests\dummy\DummyInterface' => [ |
||
| 218 | '#class'=>'drycart\di\tests\dummy\DummyServiceProvider', |
||
| 219 | '#singleton' => true, |
||
| 220 | 'answer' => 42 |
||
| 221 | ], |
||
| 222 | 'drycart\di\tests\dummy\Dummy' => [ |
||
| 223 | '#required'=> [ |
||
| 224 | 'drycart\di\tests\dummy\DummyInterface' |
||
| 225 | ] |
||
| 226 | ] |
||
| 227 | ]); |
||
| 228 | $di->get('drycart\di\tests\dummy\Dummy'); |
||
| 229 | $this->assertTrue(defined('DI_REQUIRED_TEST_CONST')); |
||
| 230 | $this->assertEquals(DI_REQUIRED_TEST_CONST, 42); |
||
| 231 | } |
||
| 232 | } |
||
| 233 |
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