1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
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 |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
public $name; |
14
|
|
|
|
15
|
|
|
public function __construct($name) |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
$this->name = $name; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public static function __make() |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
return new self('Taylor'); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
class ContainerTest extends TestCase |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
protected function tearDown(): void |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
Container::setInstance(null); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testClosureResolution() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$container = new Container; |
36
|
|
|
|
37
|
|
|
Container::setInstance($container); |
38
|
|
|
|
39
|
|
|
$container->bind('name', function () { |
|
|
|
|
40
|
|
|
return 'Taylor'; |
41
|
|
|
}); |
|
|
|
|
42
|
|
|
|
43
|
|
|
$this->assertEquals('Taylor', $container->make('name')); |
44
|
|
|
|
45
|
|
|
$this->assertEquals('Taylor', Container::pull('name')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testGet() |
|
|
|
|
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 () { |
|
|
|
|
57
|
|
|
return 'Taylor'; |
58
|
|
|
}); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->assertSame('Taylor', $container->get('name')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testExist() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$container = new Container; |
66
|
|
|
|
67
|
|
|
$container->bind('name', function () { |
|
|
|
|
68
|
|
|
return 'Taylor'; |
69
|
|
|
}); |
|
|
|
|
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() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$container = new Container; |
81
|
|
|
|
82
|
|
|
$container->bind('name', function () { |
|
|
|
|
83
|
|
|
return 'Taylor'; |
84
|
|
|
}); |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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 () { |
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
}); |
|
|
|
|
133
|
|
|
|
134
|
|
|
$this->assertSame($this, Container::getInstance()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testInvokeFunctionWithoutMethodThrowsException() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$this->expectException(Exception::class); |
140
|
|
|
$this->expectExceptionMessage('function not exists: ContainerTestCallStub()'); |
141
|
|
|
$container = new Container; |
142
|
|
|
$container->invokeFunction('ContainerTestCallStub', []); |
143
|
|
|
} |
144
|
|
|
} |