Completed
Push — 6.0 ( d97fdb...a05612 )
by
unknown
03:14
created

ContainerTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 239
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 108
dl 0
loc 239
rs 10
c 0
b 0
f 0
wmc 14

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testInvokeFunctionWithoutMethodThrowsException() 0 6 1
A testInvokeClassNotExists() 0 11 1
A testGet() 0 13 1
A tearDown() 0 3 1
A testClosureResolution() 0 13 1
A testExist() 0 13 1
A resolveContainer() 0 6 1
A testGetAndSetInstance() 0 15 1
A testInvoke() 0 39 1
A testInstance() 0 41 2
A testInvokeMethodNotExists() 0 6 1
A testAutoConcreteResolution() 0 8 1
A testBind() 0 37 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace think\tests;
4
5
use PHPUnit\Framework\TestCase;
6
use ReflectionMethod;
7
use stdClass;
8
use think\Container;
9
use think\Exception;
10
use think\exception\ClassNotFoundException;
11
12
class Taylor
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class Taylor
Loading history...
13
{
14
    public $name;
15
16
    public function __construct($name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
17
    {
18
        $this->name = $name;
19
    }
20
21
    public function some(Container $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

21
    public function some(/** @scrutinizer ignore-unused */ Container $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...
Coding Style introduced by
Missing doc comment for function some()
Loading history...
22
    {
23
24
    }
25
26
    public static function static(Container $container)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function static()
Loading history...
27
    {
28
        return $container;
29
    }
30
31
    public static function __make()
2 ignored issues
show
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
Coding Style introduced by
Method name "Taylor::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Public method name "Taylor::__make" must not be prefixed with an underscore
Loading history...
32
    {
33
        return new self('Taylor');
34
    }
35
}
36
37
class SomeClass
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class SomeClass
Loading history...
38
{
39
    public $container;
40
41
    public function __construct(Container $container)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
42
    {
43
        $this->container = $container;
44
    }
45
}
46
47
class ContainerTest extends TestCase
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class ContainerTest
Loading history...
48
{
49
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
50
    {
51
        Container::setInstance(null);
52
    }
53
54
    public function testClosureResolution()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testClosureResolution()
Loading history...
55
    {
56
        $container = new Container;
57
58
        Container::setInstance($container);
59
60
        $container->bind('name', function () {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
61
            return 'Taylor';
62
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
63
64
        $this->assertEquals('Taylor', $container->make('name'));
65
66
        $this->assertEquals('Taylor', Container::pull('name'));
67
    }
68
69
    public function testGet()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testGet()
Loading history...
70
    {
71
        $container = new Container;
72
73
        $this->expectException(ClassNotFoundException::class);
74
        $this->expectExceptionMessage('class not exists: name');
75
        $container->get('name');
76
77
        $container->bind('name', function () {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
78
            return 'Taylor';
79
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
80
81
        $this->assertSame('Taylor', $container->get('name'));
82
    }
83
84
    public function testExist()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testExist()
Loading history...
85
    {
86
        $container = new Container;
87
88
        $container->bind('name', function () {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
89
            return 'Taylor';
90
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
91
92
        $this->assertFalse($container->exists("name"));
93
94
        $container->make('name');
95
96
        $this->assertTrue($container->exists('name'));
97
    }
98
99
    public function testInstance()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testInstance()
Loading history...
100
    {
101
        $container = new Container;
102
103
        $container->bind('name', function () {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
104
            return 'Taylor';
105
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
106
107
        $this->assertEquals('Taylor', $container->get('name'));
108
109
        $container->bind('name2', Taylor::class);
110
111
        $object = new stdClass();
112
113
        $this->assertFalse($container->exists('name2'));
114
115
        $container->instance('name2', $object);
116
117
        $this->assertTrue($container->exists('name2'));
118
119
        $this->assertTrue($container->exists(Taylor::class));
120
121
        $this->assertEquals($object, $container->make(Taylor::class));
122
123
        $this->assertContains($object, $container->all());
124
125
        $this->assertEquals(count($container->all()), count($container));
126
127
        unset($container->name1);
0 ignored issues
show
Bug Best Practice introduced by
The property name1 does not exist on think\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
128
129
        $this->assertFalse($container->exists('name1'));
130
131
        $container->delete('name2');
132
133
        $this->assertFalse($container->exists('name2'));
134
135
        $container->flush();
136
137
        $this->assertEmpty($container->all());
138
139
        foreach ($container as $class => $instance) {
140
141
        }
142
    }
143
144
    public function testBind()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testBind()
Loading history...
145
    {
146
        $container = new Container;
147
148
        $object = new stdClass();
149
150
        $container->bind(['name' => Taylor::class]);
151
152
        $container->bind('name2', $object);
153
154
        $container->bind('name3', Taylor::class);
155
156
        $container->name4 = $object;
0 ignored issues
show
Bug Best Practice introduced by
The property name4 does not exist on think\Container. Since you implemented __set, consider adding a @property annotation.
Loading history...
157
158
        $container['name5'] = $object;
159
160
        $this->assertTrue(isset($container->name4));
0 ignored issues
show
Bug Best Practice introduced by
The property name4 does not exist on think\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
161
162
        $this->assertTrue(isset($container['name5']));
163
164
        $this->assertInstanceOf(Taylor::class, $container->get('name'));
165
166
        $this->assertSame($object, $container->get('name2'));
167
168
        $this->assertSame($object, $container->name4);
169
170
        $this->assertSame($object, $container['name5']);
171
172
        $this->assertInstanceOf(Taylor::class, $container->get('name3'));
173
174
        unset($container['name']);
175
176
        $this->assertFalse(isset($container['name']));
177
178
        unset($container->name3);
0 ignored issues
show
Bug Best Practice introduced by
The property name3 does not exist on think\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
179
180
        $this->assertFalse(isset($container->name3));
181
    }
182
183
    public function testAutoConcreteResolution()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testAutoConcreteResolution()
Loading history...
184
    {
185
        $container = new Container;
186
187
        $taylor = $container->make(Taylor::class);
188
189
        $this->assertInstanceOf(Taylor::class, $taylor);
190
        $this->assertAttributeSame('Taylor', 'name', $taylor);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertAttributeSame() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3338 ( Ignorable by Annotation )

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

190
        /** @scrutinizer ignore-deprecated */ $this->assertAttributeSame('Taylor', 'name', $taylor);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
191
    }
192
193
    public function testGetAndSetInstance()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testGetAndSetInstance()
Loading history...
194
    {
195
        $this->assertInstanceOf(Container::class, Container::getInstance());
196
197
        $object = new stdClass();
198
199
        Container::setInstance($object);
200
201
        $this->assertSame($object, Container::getInstance());
202
203
        Container::setInstance(function () {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
204
            return $this;
205
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
206
207
        $this->assertSame($this, Container::getInstance());
208
    }
209
210
    public function testInvokeFunctionWithoutMethodThrowsException()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testInvokeFunctionWithoutMethodThrowsException()
Loading history...
211
    {
212
        $this->expectException(Exception::class);
213
        $this->expectExceptionMessage('function not exists: ContainerTestCallStub()');
214
        $container = new Container;
215
        $container->invokeFunction('ContainerTestCallStub', []);
216
    }
217
218
    public function testInvoke()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testInvoke()
Loading history...
219
    {
220
        $container = new Container();
221
222
        Container::setInstance($container);
223
224
        $container->bind(Container::class, $container);
225
226
        $stub = $this->createMock(Taylor::class);
227
228
        $stub->expects($this->once())->method('some')->with($container)->will($this->returnSelf());
229
230
        $container->invokeMethod([$stub, 'some']);
231
232
        $this->assertSame($container, $container->invokeMethod(Taylor::class . '::static'));
233
234
        $reflect = new ReflectionMethod($container, 'exists');
235
236
        $this->assertTrue($container->invokeReflectMethod($container, $reflect, [Container::class]));
237
238
        $this->assertSame($container, $container->invoke(function (Container $container) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
239
            return $container;
240
        }));
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
241
242
        $this->assertSame($container, $container->invoke(Taylor::class . '::static'));
243
244
        $object = $container->invokeClass(SomeClass::class);
245
        $this->assertInstanceOf(SomeClass::class, $object);
246
        $this->assertSame($container, $object->container);
247
248
        $stdClass = new stdClass();
249
250
        $container->invoke(function (Container $container, stdClass $stdObject, $key1, $lowKey, $key2 = 'default') use ($stdClass) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
251
            $this->assertEquals('value1', $key1);
252
            $this->assertEquals('default', $key2);
253
            $this->assertEquals('value2', $lowKey);
254
            $this->assertSame($stdClass, $stdObject);
255
            return $container;
256
        }, ['some' => $stdClass, 'key1' => 'value1', 'low_key' => 'value2']);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
257
    }
258
259
    public function testInvokeMethodNotExists()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testInvokeMethodNotExists()
Loading history...
260
    {
261
        $container = $this->resolveContainer();
262
        $this->expectException(Exception::class);
263
264
        $container->invokeMethod([SomeClass::class, 'any']);
265
    }
266
267
    public function testInvokeClassNotExists()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testInvokeClassNotExists()
Loading history...
268
    {
269
        $container = new Container();
270
271
        Container::setInstance($container);
272
273
        $container->bind(Container::class, $container);
274
275
        $this->expectExceptionObject(new ClassNotFoundException('class not exists: SomeClass'));
276
277
        $container->invokeClass('SomeClass');
278
    }
279
280
    protected function resolveContainer()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function resolveContainer()
Loading history...
281
    {
282
        $container = new Container();
283
284
        Container::setInstance($container);
285
        return $container;
286
    }
287
288
}
289