Test Failed
Push — 6.0 ( dd7dda...d2ec01 )
by liu
02:30
created

ContainerTest::testGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
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 stdClass;
7
use think\Container;
8
use think\Exception;
9
use think\exception\ClassNotFoundException;
10
11
class Taylor
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class Taylor
Loading history...
12
{
13
    public $name;
14
15
    public function __construct($name)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
16
    {
17
        $this->name = $name;
18
    }
19
20
    public static function __make()
2 ignored issues
show
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...
Coding Style introduced by
Missing doc comment for function __make()
Loading history...
21
    {
22
        return new self('Taylor');
23
    }
24
}
25
26
class ContainerTest extends TestCase
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class ContainerTest
Loading history...
27
{
28
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
29
    {
30
        Container::setInstance(null);
31
    }
32
33
    public function testClosureResolution()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testClosureResolution()
Loading history...
34
    {
35
        $container = new Container;
36
37
        Container::setInstance($container);
38
39
        $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...
40
            return 'Taylor';
41
        });
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...
42
43
        $this->assertEquals('Taylor', $container->make('name'));
44
45
        $this->assertEquals('Taylor', Container::pull('name'));
46
    }
47
48
    public function testGet()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testGet()
Loading history...
49
    {
50
        $container = new Container;
51
52
        $this->expectException(ClassNotFoundException::class);
53
        $this->expectExceptionMessage('class not exists: name');
54
        $container->get('name');
55
56
        $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...
57
            return 'Taylor';
58
        });
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...
59
60
        $this->assertSame('Taylor', $container->get('name'));
61
    }
62
63
    public function testExist()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testExist()
Loading history...
64
    {
65
        $container = new Container;
66
67
        $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...
68
            return 'Taylor';
69
        });
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...
70
71
        $this->assertFalse($container->exists("name"));
72
73
        $container->make('name');
74
75
        $this->assertTrue($container->exists('name'));
76
    }
77
78
    public function testInstance()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testInstance()
Loading history...
79
    {
80
        $container = new Container;
81
82
        $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...
83
            return 'Taylor';
84
        });
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...
85
86
        $this->assertEquals('Taylor', $container->get('name'));
87
88
        $object = new stdClass();
89
90
        $container->instance('name', $object);
91
92
        $this->assertEquals($object, $container->get('name'));
93
    }
94
95
    public function testBind()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testBind()
Loading history...
96
    {
97
        $container = new Container;
98
99
        $object = new stdClass();
100
101
        $container->bind(['name' => Taylor::class]);
102
103
        $container->bind('name2', $object);
104
105
        $this->assertInstanceOf(Taylor::class, $container->get('name'));
106
107
        $this->assertSame($object, $container->get('name2'));
108
    }
109
110
    public function testAutoConcreteResolution()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testAutoConcreteResolution()
Loading history...
111
    {
112
        $container = new Container;
113
114
        $taylor = $container->make(Taylor::class);
115
116
        $this->assertInstanceOf(Taylor::class, $taylor);
117
        $this->assertAttributeSame('Taylor', 'name', $taylor);
118
    }
119
120
    public function testGetAndSetInstance()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testGetAndSetInstance()
Loading history...
121
    {
122
        $this->assertInstanceOf(Container::class, Container::getInstance());
123
124
        $object = new stdClass();
125
126
        Container::setInstance($object);
127
128
        $this->assertSame($object, Container::getInstance());
129
130
        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...
131
            return $this;
132
        });
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...
133
134
        $this->assertSame($this, Container::getInstance());
135
    }
136
137
    public function testInvokeFunctionWithoutMethodThrowsException()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testInvokeFunctionWithoutMethodThrowsException()
Loading history...
138
    {
139
        $this->expectException(Exception::class);
140
        $this->expectExceptionMessage('function not exists: ContainerTestCallStub()');
141
        $container = new Container;
142
        $container->invokeFunction('ContainerTestCallStub', []);
143
    }
144
}