|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny\Container\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Jasny\Autowire\AutowireInterface; |
|
6
|
|
|
use Jasny\Container\Container; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Test class for Container |
|
12
|
|
|
* |
|
13
|
|
|
* @covers \Jasny\Container\Container |
|
14
|
|
|
*/ |
|
15
|
|
|
class ContainerTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
public function testGet() |
|
19
|
|
|
{ |
|
20
|
|
|
$container = new Container([ |
|
21
|
|
|
"instance" => function () { return "value"; } |
|
22
|
|
|
]); |
|
23
|
|
|
|
|
24
|
|
|
$this->assertSame('value', $container->get('instance')); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @expectedException \Jasny\Container\Exception\NotFoundException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function testGetNotFound() |
|
31
|
|
|
{ |
|
32
|
|
|
$container = new Container([]); |
|
33
|
|
|
|
|
34
|
|
|
$container->get('nonexistant'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testDelegateContainer() |
|
38
|
|
|
{ |
|
39
|
|
|
$container = new Container([ |
|
40
|
|
|
"instance" => function () { return "value"; } |
|
41
|
|
|
]); |
|
42
|
|
|
|
|
43
|
|
|
$container2 = new Container([ |
|
44
|
|
|
"instance2" => function (ContainerInterface $container) { return $container->get('instance'); } |
|
45
|
|
|
], $container); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertSame('value', $container2->get('instance2')); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testOneInstanceOnly() |
|
51
|
|
|
{ |
|
52
|
|
|
$container = new Container([ |
|
53
|
|
|
"instance" => function () { return new \stdClass(); } |
|
54
|
|
|
]); |
|
55
|
|
|
|
|
56
|
|
|
$instance1 = $container->get('instance'); |
|
57
|
|
|
$instance2 = $container->get('instance'); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertSame($instance1, $instance2); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testHas() |
|
63
|
|
|
{ |
|
64
|
|
|
$container = new Container([ |
|
65
|
|
|
"instance" => function () { return "value"; } |
|
66
|
|
|
]); |
|
67
|
|
|
|
|
68
|
|
|
$this->assertTrue($container->has('instance')); |
|
69
|
|
|
$this->assertFalse($container->has('non_existing')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
public function testGetSub() |
|
74
|
|
|
{ |
|
75
|
|
|
$subContainer = new Container([ |
|
76
|
|
|
"instance" => function() { return "value"; } |
|
77
|
|
|
]); |
|
78
|
|
|
|
|
79
|
|
|
$container = new Container([ |
|
80
|
|
|
"sub" => function () use ($subContainer) { return $subContainer; }, |
|
81
|
|
|
"instance" => function () { return "nop"; } |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
|
|
$result = $container->get('sub.instance'); |
|
85
|
|
|
$this->assertEquals("value", $result); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @expectedException \Jasny\Container\Exception\NoSubContainerException |
|
90
|
|
|
*/ |
|
91
|
|
|
public function testGetSubInvalid() |
|
92
|
|
|
{ |
|
93
|
|
|
$container = new Container([ |
|
94
|
|
|
"sub" => function () { return "value"; } |
|
95
|
|
|
]); |
|
96
|
|
|
|
|
97
|
|
|
$container->get('sub.instance'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function testHasSub() |
|
101
|
|
|
{ |
|
102
|
|
|
$subContainer = new Container([ |
|
103
|
|
|
"instance" => function() { return "value"; } |
|
104
|
|
|
]); |
|
105
|
|
|
|
|
106
|
|
|
$container = new Container([ |
|
107
|
|
|
"sub" => function () use ($subContainer) { return $subContainer; }, |
|
108
|
|
|
"instance" => function () { return "nop"; } |
|
109
|
|
|
]); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertTrue($container->has('sub.instance')); |
|
112
|
|
|
|
|
113
|
|
|
$this->assertFalse($container->has('sub.non_existing')); |
|
114
|
|
|
$this->assertFalse($container->has('instance.foo')); |
|
115
|
|
|
$this->assertFalse($container->has('foo.instance')); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function testHasDeepSub() |
|
119
|
|
|
{ |
|
120
|
|
|
$subContainer = new Container([ |
|
121
|
|
|
"d1.d2.instance" => function() { return "value"; } |
|
122
|
|
|
]); |
|
123
|
|
|
|
|
124
|
|
|
$container = new Container([ |
|
125
|
|
|
"sub.u1.u2" => function () use ($subContainer) { return $subContainer; }, |
|
126
|
|
|
]); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertTrue($container->has('sub.u1.u2.d1.d2.instance')); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertFalse($container->has('sub.u1.u2.d1')); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @expectedException \TypeError |
|
136
|
|
|
* @expectedExceptionMessage Entry is a DateTime object, which does not implement Jasny\Container\Container |
|
137
|
|
|
*/ |
|
138
|
|
|
public function testGetClassMismatch() |
|
139
|
|
|
{ |
|
140
|
|
|
$container = new Container([ |
|
141
|
|
|
Container::class => function () { return new \DateTime(); } |
|
142
|
|
|
]); |
|
143
|
|
|
|
|
144
|
|
|
$container->get(Container::class); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @expectedException \TypeError |
|
149
|
|
|
* @expectedExceptionMessage Entry is a DateTime object, which does not implement Psr\Container\ContainerInterface |
|
150
|
|
|
*/ |
|
151
|
|
|
public function testGetInterfaceMismatch() |
|
152
|
|
|
{ |
|
153
|
|
|
$container = new Container([ |
|
154
|
|
|
ContainerInterface::class => function () { return new \DateTime(); } |
|
155
|
|
|
]); |
|
156
|
|
|
|
|
157
|
|
|
$container->get(ContainerInterface::class); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
public function testAutowire() |
|
162
|
|
|
{ |
|
163
|
|
|
$foo = new \stdClass(); |
|
164
|
|
|
|
|
165
|
|
|
$autowire = $this->createMock(AutowireInterface::class); |
|
166
|
|
|
$autowire->expects($this->once())->method('instantiate') |
|
167
|
|
|
->with('Foo')->willReturn($foo); |
|
168
|
|
|
|
|
169
|
|
|
$container = new Container([ |
|
170
|
|
|
AutowireInterface::class => function() use ($autowire) { |
|
171
|
|
|
return $autowire; |
|
172
|
|
|
} |
|
173
|
|
|
]); |
|
174
|
|
|
|
|
175
|
|
|
$result = $container->autowire('Foo'); |
|
176
|
|
|
|
|
177
|
|
|
$this->assertSame($foo, $result); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function testAutowireParams() |
|
181
|
|
|
{ |
|
182
|
|
|
$foo = new \stdClass(); |
|
183
|
|
|
|
|
184
|
|
|
$autowire = $this->createMock(AutowireInterface::class); |
|
185
|
|
|
$autowire->expects($this->once())->method('instantiate') |
|
186
|
|
|
->with('Foo', 'one', 'two')->willReturn($foo); |
|
187
|
|
|
|
|
188
|
|
|
$container = new Container([ |
|
189
|
|
|
AutowireInterface::class => function() use ($autowire) { |
|
190
|
|
|
return $autowire; |
|
191
|
|
|
} |
|
192
|
|
|
]); |
|
193
|
|
|
|
|
194
|
|
|
$result = $container->autowire('Foo', 'one', 'two'); |
|
195
|
|
|
|
|
196
|
|
|
$this->assertSame($foo, $result); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @expectedException \Jasny\Container\Exception\NotFoundException |
|
201
|
|
|
*/ |
|
202
|
|
|
public function testAutowireNotFound() |
|
203
|
|
|
{ |
|
204
|
|
|
$container = new Container([]); |
|
205
|
|
|
$container->autowire('Foo'); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|