-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathContainerTest.php
228 lines (200 loc) · 6.33 KB
/
ContainerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/*
* This file is part of the PHPBench package
*
* (c) Daniel Leech <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PhpBench\Tests\Unit\DependencyInjection;
use PhpBench\DependencyInjection\Container;
use PhpBench\DependencyInjection\ExtensionInterface;
class ContainerTest extends \PHPUnit_Framework_TestCase
{
private $container;
public function setUp()
{
$this->container = new Container();
}
/**
* It should register and get services
* It should return the same instance on consecutive calls.
*/
public function testRegisterSet()
{
$this->container->register('stdclass', function () {
return new \stdClass();
});
$instance = $this->container->get('stdclass');
$this->assertInstanceOf('stdClass', $instance);
$this->assertSame($instance, $this->container->get('stdclass'));
}
/**
* It should say if it contains a service or not.
*/
public function testHas()
{
$this->container->register('stdclass', function () {
return new \stdClass();
});
$this->assertFalse($this->container->has('foo'));
$this->assertTrue($this->container->has('stdclass'));
}
/**
* It should register and retrieve tagged services IDs with attributes.
*/
public function testServiceIdTags()
{
$this->container->register('stdclass1', function () {
return new \stdClass();
}, ['tag1' => ['name' => 'hello']]);
$this->container->register('stdclass2', function () {
return new \stdClass();
}, ['tag1' => ['name' => 'hello']]);
$this->container->register('stdclass3', function () {
return new \stdClass();
}, ['tag2' => ['name' => 'goodbye']]);
$serviceIds = $this->container->getServiceIdsForTag('tag1');
$this->assertNotNull($serviceIds);
$this->assertCount(2, $serviceIds);
foreach ($serviceIds as $attributes) {
$this->assertEquals('hello', $attributes['name']);
}
}
/**
* Its should throw an exception if a service is already registered.
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessge Service with ID "stdclass"
*/
public function testServiceAlreadyRegistered()
{
$this->container->register('stdclass', function () {
return new \stdClass();
});
$this->container->register('stdclass', function () {
return new \stdClass();
});
}
/**
* It should register extensions.
* It should register extension configuration.
* It should build the extensions.
*/
public function testRegisterExtension()
{
$container = new Container([
__NAMESPACE__ . '\\TestExtension',
]);
$container->init();
$object = $container->get('foobar');
$this->assertInstanceOf('stdClass', $object);
$this->assertEquals('bar', $object->foobar);
}
/**
* User configuration should take priority over extension configuration.
*/
public function testRegisterExtensionWithUserConfig()
{
$container = new Container(
[
__NAMESPACE__ . '\\TestExtension',
],
[
'foo' => 'bazz',
]
);
$container->init();
$object = $container->get('foobar');
$this->assertInstanceOf('stdClass', $object);
$this->assertEquals('bazz', $object->foobar);
}
/**
* It should merge parameters.
*/
public function testMergeParameters()
{
$this->container->setParameter('foo', [ 'foo' => 'bar' ]);
$this->container->mergeParameter('foo', [ 'bar' => 'boo' ]);
$this->assertEquals([
'foo' => 'bar',
'bar' => 'boo'
], $this->container->getParameter('foo'));
}
/**
* It should throw an exception when trying to merge a value into a non-array parameter.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage scalar
*/
public function testMergeParameterNonArray()
{
$this->container->setParameter('foo', 'bar');
$this->container->mergeParameter('foo', [ 'bar' => 'boo' ]);
}
/**
* It should throw an exception if an extension class does not exist.
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage "NotExistingExtension" does not exist
*/
public function testRegisterNotExistingExtension()
{
$container = new Container(['NotExistingExtension']);
$container->init();
}
/**
* It should throw an exception if an extension class does not implement
* the ExtensionInterface.
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Extension "stdClass" must implement the
*/
public function testRegisterNotImplementingExtension()
{
$container = new Container(['stdClass']);
$container->init();
}
/**
* It should throw an exception if an unknown user configuration key is used.
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Unknown configuration keys: "not". Permitted keys:
*/
public function testUnknownUserConfig()
{
$container = new Container([], [
'not' => 'existing',
]);
$container->init();
}
/**
* It should throw an exception if a requested parameter does not exist.
*
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Parameter "foo" has not been registered
*/
public function testUnknownParameter()
{
$container = new Container();
$container->getParameter('foo');
}
}
class TestExtension implements ExtensionInterface
{
public function getDefaultConfig()
{
return [
'foo' => 'bar',
];
}
public function load(Container $container)
{
$container->register('foobar', function ($container) {
$stdClass = new \stdClass();
$stdClass->foobar = $container->getParameter('foo');
return $stdClass;
});
}
}