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

ExtendedTest::testSingletonDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
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 ExtendedTest 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
    public function testMake()
17
    {
18
        $di = new \drycart\di\Container();
19
        $obj = $di->make('drycart\di\tests\DummyPlusParameter', ['intDummy'=> 0]);
20
        $this->assertTrue(is_a($obj, 'drycart\di\tests\DummyPlusParameter'));
21
    }
22
    
23
    public function testMakeException()
24
    {
25
        $di = new \drycart\di\Container();
26
        $di->setConfig([
27
            'drycart\di\tests\Dummy' => [
28
                '#singleton' => true
29
            ]
30
        ]);
31
        $this->expectException(\drycart\di\ContainerException::class);
32
        $this->expectExceptionMessage('drycart\di\tests\Dummy is singlton!');
33
        $di->make('drycart\di\tests\Dummy');
34
    }
35
    
36
    public function testMakeParamsNoType()
37
    {
38
        $di = new \drycart\di\Container();
39
        $di->setConfig([
40
            'drycart\di\tests\DummyInterface' => [
41
                '#class'=>'drycart\di\tests\DummyComplex',
42
                'noTypeParameter' => 5
43
            ]
44
        ]);
45
        $obj = $di->get('drycart\di\tests\DummyInterface');
46
        $this->assertTrue(is_a($obj, 'drycart\di\tests\DummyInterface'));
47
    }
48
    
49
    public function testMakePreparedParameters()
50
    {
51
        $di = new \drycart\di\Container();
52
        $dummy = $di->get('drycart\di\tests\Dummy');
53
        $obj = $di->make('drycart\di\tests\DummyPlusParameter', [
54
            'intDummy'=> 0,
55
            'dummy'=>$dummy
56
        ]);
57
        $this->assertTrue(is_a($obj, 'drycart\di\tests\DummyPlusParameter'));
58
    }
59
    
60
    public function testMakeFromArrayParameter()
61
    {
62
        $di = new \drycart\di\Container();
63
        $obj = $di->make('drycart\di\tests\DummyPlusParameter', [
64
            'intDummy'=> 0,
65
            'dummy'=>['#class'=>'drycart\di\tests\Dummy']
66
        ]);
67
        $this->assertTrue(is_a($obj, 'drycart\di\tests\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\DummyPlusParameter', [
77
            'intDummy'=> 0,
78
            'dummy'=> new \stdClass()
79
        ]);
80
        $this->assertTrue(is_a($obj, 'drycart\di\tests\DummyPlusParameter'));
81
    }
82
    
83
    public function testParent()
84
    {
85
        $di = new \drycart\di\Container();
86
        $di->setConfig([
87
            'drycart\di\tests\Dummy' => [
88
                '#singleton' => true
89
            ]
90
        ]);
91
        $obj = $di->singleton('drycart\di\tests\DummyExtended');
92
        $this->assertTrue(is_a($obj, 'drycart\di\tests\DummyExtended'));
93
    }
94
    
95
    public function testFactory1()
96
    {
97
        $di = new \drycart\di\Container();
98
        $di->addClass(\stdClass::class, [
99
            '#factory' => function($config, $container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
            '#factory' => function($config, /** @scrutinizer ignore-unused */ $container) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
            unset($config['#factory']);
101
            unset($config['#class']);
102
            unset($config['#singleton']);
103
            $obj = (object) $config;
104
            $obj->meta = 'meta';
105
            return $obj;
106
        }]);
107
        $obj = $di->make(\stdClass::class, [
108
            'id'=> 555,
109
        ]);
110
        $this->assertEquals($obj->id, 555);
111
        $this->assertEquals($obj->meta, 'meta');
112
    }
113
    
114
    public function testFactory2()
115
    {
116
        $di = new \drycart\di\Container();
117
        $di->addClass(\drycart\di\tests\Dummy::class, [
118
            '#factory' => function($config, $container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

118
            '#factory' => function($config, /** @scrutinizer ignore-unused */ $container) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
            $className = $config['#class'];
120
            $obj = new $className;
121
            $obj->meta = 'meta';
122
            return $obj;
123
        }]);
124
        $obj = $di->make('drycart\di\tests\DummyExtended');
125
        $this->assertTrue(is_a($obj, 'drycart\di\tests\DummyExtended'));
126
        $this->assertEquals($obj->meta, 'meta');
127
    }
128
    
129
    public function testMakeWrongParameterTypeException()
130
    {
131
        $di = new \drycart\di\Container();
132
        $this->expectException(\drycart\di\ContainerException::class);
133
        $this->expectExceptionMessage('Wrong type of value for parameter');
134
        $di->make('drycart\di\tests\DummyPlusParameter', [
135
            'intDummy'=> 0,
136
            'dummy'=> new \stdClass()
137
        ]);
138
    }
139
    
140
    public function testSingletoneException()
141
    {
142
        $di = new \drycart\di\Container();
143
        $di->setConfig([
144
            'drycart\di\tests\Dummy' => [
145
                '#singleton' => false
146
            ]
147
        ]);
148
        $this->expectException(\drycart\di\ContainerException::class);
149
        $this->expectExceptionMessage('drycart\di\tests\Dummy NOT singlton!');
150
        $di->singleton('drycart\di\tests\Dummy');
151
    }
152
    
153
    public function testMakeConfig()
154
    {
155
        $di = new \drycart\di\Container();
156
        $di->setConfig([
157
            'drycart\di\tests\DummyMakeConfig' => [
158
                'dummy' => ['i'=>5],
159
                'intDummy'=> 5
160
            ],
161
            'drycart\di\tests\DummyInterface' => [
162
                '#class'=>'drycart\di\tests\DummyToMake'
163
            ]
164
        ]);
165
        $obj = $di->get('drycart\di\tests\DummyMakeConfig');
166
        $this->assertTrue(is_a($obj->dummy, 'drycart\di\tests\DummyToMake'));
167
    }
168
    public function testService()
169
    {
170
        $di = new \drycart\di\Container();
171
        $di->setConfig([
172
            'drycart\di\tests\Dummy' => [
173
                '#singleton' => true
174
            ]
175
        ]);
176
        $obj1 = $di->singleton('drycart\di\tests\Dummy');
177
        $obj2 = $di->get('drycart\di\tests\Dummy');
178
        $this->assertEquals(spl_object_id($obj1), spl_object_id($obj2));
179
    }
180
    
181
    public function testSingletonDefault()
182
    {
183
        // For reset if not new.. Yes, static is not tested, but....
184
        \drycart\di\Di::setInstance(null);
185
        //
186
        $di1 = \drycart\di\Di::getInstance();
187
        $di2 = \drycart\di\Di::getInstance();
188
        $this->assertEquals(spl_object_id($di1), spl_object_id($di2));
189
    }
190
    
191
    public function testSingleton()
192
    {
193
        $di = new \drycart\di\Container();
194
        \drycart\di\Di::setInstance($di);
195
        //
196
        $di1 = \drycart\di\Di::getInstance();
197
        $di2 = \drycart\di\Di::getInstance();
198
        $this->assertEquals(spl_object_id($di1), spl_object_id($di2));
199
    }
200
    
201
    public function testRequired()
202
    {
203
        $this->assertFalse(defined('DI_REQUIRED_TEST_CONST'));
204
        $di = new \drycart\di\Container();
205
        $di->setConfig([
206
            'drycart\di\tests\DummyInterface' => [
207
                '#class'=>'drycart\di\tests\DummyServiceProvider',
208
                '#singleton' => true,
209
                'answer' => 42
210
            ],
211
            'drycart\di\tests\Dummy' => [
212
                '#required'=> [
213
                    'drycart\di\tests\DummyInterface'
214
                ]
215
            ]
216
        ]);
217
        $di->get('drycart\di\tests\Dummy');
218
        $this->assertTrue(defined('DI_REQUIRED_TEST_CONST'));
219
        $this->assertEquals(DI_REQUIRED_TEST_CONST, 42);
220
    }
221
}
222