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
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @expectedException \PHPUnit\Framework\Error\Notice |
121
|
|
|
* @expectedExceptionMessage Entry is a DateTime object, which does not implement Psr\Container\ContainerInterface |
122
|
|
|
*/ |
123
|
|
|
public function testGetInterfaceMismatch() |
124
|
|
|
{ |
125
|
|
|
$container = new Container([ |
126
|
|
|
ContainerInterface::class => function () { return new \DateTime(); } |
127
|
|
|
]); |
128
|
|
|
|
129
|
|
|
$container->get(ContainerInterface::class); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
public function testAutowire() |
134
|
|
|
{ |
135
|
|
|
$foo = new \stdClass(); |
136
|
|
|
|
137
|
|
|
$autowire = $this->createMock(AutowireInterface::class); |
138
|
|
|
$autowire->expects($this->once())->method('instantiate') |
139
|
|
|
->with('Foo')->willReturn($foo); |
140
|
|
|
|
141
|
|
|
$container = new Container([ |
142
|
|
|
AutowireInterface::class => function() use ($autowire) { |
143
|
|
|
return $autowire; |
144
|
|
|
} |
145
|
|
|
]); |
146
|
|
|
|
147
|
|
|
$result = $container->autowire('Foo'); |
148
|
|
|
|
149
|
|
|
$this->assertSame($foo, $result); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testAutowireParams() |
153
|
|
|
{ |
154
|
|
|
$foo = new \stdClass(); |
155
|
|
|
|
156
|
|
|
$autowire = $this->createMock(AutowireInterface::class); |
157
|
|
|
$autowire->expects($this->once())->method('instantiate') |
158
|
|
|
->with('Foo', 'one', 'two')->willReturn($foo); |
159
|
|
|
|
160
|
|
|
$container = new Container([ |
161
|
|
|
AutowireInterface::class => function() use ($autowire) { |
162
|
|
|
return $autowire; |
163
|
|
|
} |
164
|
|
|
]); |
165
|
|
|
|
166
|
|
|
$result = $container->autowire('Foo', 'one', 'two'); |
167
|
|
|
|
168
|
|
|
$this->assertSame($foo, $result); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @expectedException \Jasny\Container\Exception\NotFoundException |
173
|
|
|
*/ |
174
|
|
|
public function testAutowireNotFound() |
175
|
|
|
{ |
176
|
|
|
$container = new Container([]); |
177
|
|
|
$container->autowire('Foo'); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|