/
DefinitionTest.php
383 lines (328 loc) · 15.4 KB
/
DefinitionTest.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Definition;
class DefinitionTest extends TestCase
{
public function testConstructor()
{
$def = new Definition('stdClass');
$this->assertEquals('stdClass', $def->getClass(), '__construct() takes the class name as its first argument');
$this->assertSame(array('class' => true), $def->getChanges());
$def = new Definition('stdClass', array('foo'));
$this->assertEquals(array('foo'), $def->getArguments(), '__construct() takes an optional array of arguments as its second argument');
}
public function testSetGetFactory()
{
$def = new Definition();
$this->assertSame($def, $def->setFactory('foo'), '->setFactory() implements a fluent interface');
$this->assertEquals('foo', $def->getFactory(), '->getFactory() returns the factory');
$def->setFactory('Foo::bar');
$this->assertEquals(array('Foo', 'bar'), $def->getFactory(), '->setFactory() converts string static method call to the array');
$this->assertSame(array('factory' => true), $def->getChanges());
}
public function testSetGetClass()
{
$def = new Definition('stdClass');
$this->assertSame($def, $def->setClass('foo'), '->setClass() implements a fluent interface');
$this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
}
public function testSetGetDecoratedService()
{
$def = new Definition('stdClass');
$this->assertNull($def->getDecoratedService());
$def->setDecoratedService('foo', 'foo.renamed', 5);
$this->assertEquals(array('foo', 'foo.renamed', 5), $def->getDecoratedService());
$def->setDecoratedService(null);
$this->assertNull($def->getDecoratedService());
$def = new Definition('stdClass');
$this->assertNull($def->getDecoratedService());
$def->setDecoratedService('foo', 'foo.renamed');
$this->assertEquals(array('foo', 'foo.renamed', 0), $def->getDecoratedService());
$def->setDecoratedService(null);
$this->assertNull($def->getDecoratedService());
$def = new Definition('stdClass');
$def->setDecoratedService('foo');
$this->assertEquals(array('foo', null, 0), $def->getDecoratedService());
$def->setDecoratedService(null);
$this->assertNull($def->getDecoratedService());
$def = new Definition('stdClass');
if (method_exists($this, 'expectException')) {
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('The decorated service inner name for "foo" must be different than the service name itself.');
} else {
$this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');
}
$def->setDecoratedService('foo', 'foo');
}
public function testArguments()
{
$def = new Definition('stdClass');
$this->assertSame($def, $def->setArguments(array('foo')), '->setArguments() implements a fluent interface');
$this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
$this->assertSame($def, $def->addArgument('bar'), '->addArgument() implements a fluent interface');
$this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
}
public function testMethodCalls()
{
$def = new Definition('stdClass');
$this->assertSame($def, $def->setMethodCalls(array(array('foo', array('foo')))), '->setMethodCalls() implements a fluent interface');
$this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
$this->assertSame($def, $def->addMethodCall('bar', array('bar')), '->addMethodCall() implements a fluent interface');
$this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
$this->assertTrue($def->hasMethodCall('bar'), '->hasMethodCall() returns true if first argument is a method to call registered');
$this->assertFalse($def->hasMethodCall('no_registered'), '->hasMethodCall() returns false if first argument is not a method to call registered');
$this->assertSame($def, $def->removeMethodCall('bar'), '->removeMethodCall() implements a fluent interface');
$this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->removeMethodCall() removes a method to call');
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Method name cannot be empty.
*/
public function testExceptionOnEmptyMethodCall()
{
$def = new Definition('stdClass');
$def->addMethodCall('');
}
public function testSetGetFile()
{
$def = new Definition('stdClass');
$this->assertSame($def, $def->setFile('foo'), '->setFile() implements a fluent interface');
$this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
}
public function testSetIsShared()
{
$def = new Definition('stdClass');
$this->assertTrue($def->isShared(), '->isShared() returns true by default');
$this->assertSame($def, $def->setShared(false), '->setShared() implements a fluent interface');
$this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
}
public function testSetIsPublic()
{
$def = new Definition('stdClass');
$this->assertTrue($def->isPublic(), '->isPublic() returns true by default');
$this->assertSame($def, $def->setPublic(false), '->setPublic() implements a fluent interface');
$this->assertFalse($def->isPublic(), '->isPublic() returns false if the instance must not be public.');
}
public function testSetIsSynthetic()
{
$def = new Definition('stdClass');
$this->assertFalse($def->isSynthetic(), '->isSynthetic() returns false by default');
$this->assertSame($def, $def->setSynthetic(true), '->setSynthetic() implements a fluent interface');
$this->assertTrue($def->isSynthetic(), '->isSynthetic() returns true if the service is synthetic.');
}
public function testSetIsLazy()
{
$def = new Definition('stdClass');
$this->assertFalse($def->isLazy(), '->isLazy() returns false by default');
$this->assertSame($def, $def->setLazy(true), '->setLazy() implements a fluent interface');
$this->assertTrue($def->isLazy(), '->isLazy() returns true if the service is lazy.');
}
public function testSetIsAbstract()
{
$def = new Definition('stdClass');
$this->assertFalse($def->isAbstract(), '->isAbstract() returns false by default');
$this->assertSame($def, $def->setAbstract(true), '->setAbstract() implements a fluent interface');
$this->assertTrue($def->isAbstract(), '->isAbstract() returns true if the instance must not be public.');
}
public function testSetIsDeprecated()
{
$def = new Definition('stdClass');
$this->assertFalse($def->isDeprecated(), '->isDeprecated() returns false by default');
$this->assertSame($def, $def->setDeprecated(true), '->setDeprecated() implements a fluent interface');
$this->assertTrue($def->isDeprecated(), '->isDeprecated() returns true if the instance should not be used anymore.');
$this->assertSame('The "deprecated_service" service is deprecated. You should stop using it, as it will soon be removed.', $def->getDeprecationMessage('deprecated_service'), '->getDeprecationMessage() should return a formatted message template');
}
/**
* @dataProvider invalidDeprecationMessageProvider
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
*/
public function testSetDeprecatedWithInvalidDeprecationTemplate($message)
{
$def = new Definition('stdClass');
$def->setDeprecated(false, $message);
}
public function invalidDeprecationMessageProvider()
{
return array(
"With \rs" => array("invalid \r message %service_id%"),
"With \ns" => array("invalid \n message %service_id%"),
'With */s' => array('invalid */ message %service_id%'),
'message not containing require %service_id% variable' => array('this is deprecated'),
);
}
public function testSetGetConfigurator()
{
$def = new Definition('stdClass');
$this->assertSame($def, $def->setConfigurator('foo'), '->setConfigurator() implements a fluent interface');
$this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
}
public function testClearTags()
{
$def = new Definition('stdClass');
$this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
$def->addTag('foo', array('foo' => 'bar'));
$def->clearTags();
$this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
}
public function testClearTag()
{
$def = new Definition('stdClass');
$this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
$def->addTag('1foo1', array('foo1' => 'bar1'));
$def->addTag('2foo2', array('foo2' => 'bar2'));
$def->addTag('3foo3', array('foo3' => 'bar3'));
$def->clearTag('2foo2');
$this->assertTrue($def->hasTag('1foo1'));
$this->assertFalse($def->hasTag('2foo2'));
$this->assertTrue($def->hasTag('3foo3'));
$def->clearTag('1foo1');
$this->assertFalse($def->hasTag('1foo1'));
$this->assertTrue($def->hasTag('3foo3'));
}
public function testTags()
{
$def = new Definition('stdClass');
$this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
$this->assertFalse($def->hasTag('foo'));
$this->assertSame($def, $def->addTag('foo'), '->addTag() implements a fluent interface');
$this->assertTrue($def->hasTag('foo'));
$this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
$def->addTag('foo', array('foo' => 'bar'));
$this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
$def->addTag('bar', array('bar' => 'bar'));
$this->assertEquals($def->getTags(), array(
'foo' => array(array(), array('foo' => 'bar')),
'bar' => array(array('bar' => 'bar')),
), '->getTags() returns all tags');
}
public function testSetArgument()
{
$def = new Definition('stdClass');
$def->addArgument('foo');
$this->assertSame(array('foo'), $def->getArguments());
$this->assertSame($def, $def->replaceArgument(0, 'moo'));
$this->assertSame(array('moo'), $def->getArguments());
$def->addArgument('moo');
$def
->replaceArgument(0, 'foo')
->replaceArgument(1, 'bar')
;
$this->assertSame(array('foo', 'bar'), $def->getArguments());
}
/**
* @expectedException \OutOfBoundsException
*/
public function testGetArgumentShouldCheckBounds()
{
$def = new Definition('stdClass');
$def->addArgument('foo');
$def->getArgument(1);
}
/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage The index "1" is not in the range [0, 0].
*/
public function testReplaceArgumentShouldCheckBounds()
{
$def = new Definition('stdClass');
$def->addArgument('foo');
$def->replaceArgument(1, 'bar');
}
/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage Cannot replace arguments if none have been configured yet.
*/
public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
{
$def = new Definition('stdClass');
$def->replaceArgument(0, 'bar');
}
public function testSetGetProperties()
{
$def = new Definition('stdClass');
$this->assertEquals(array(), $def->getProperties());
$this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
$this->assertEquals(array('foo' => 'bar'), $def->getProperties());
}
public function testSetProperty()
{
$def = new Definition('stdClass');
$this->assertEquals(array(), $def->getProperties());
$this->assertSame($def, $def->setProperty('foo', 'bar'));
$this->assertEquals(array('foo' => 'bar'), $def->getProperties());
}
public function testAutowired()
{
$def = new Definition('stdClass');
$this->assertFalse($def->isAutowired());
$def->setAutowired(true);
$this->assertTrue($def->isAutowired());
$def->setAutowired(false);
$this->assertFalse($def->isAutowired());
}
public function testChangesNoChanges()
{
$def = new Definition();
$this->assertSame(array(), $def->getChanges());
}
public function testGetChangesWithChanges()
{
$def = new Definition('stdClass', array('fooarg'));
$def->setAbstract(true);
$def->setAutowired(true);
$def->setConfigurator('configuration_func');
$def->setDecoratedService(null);
$def->setDeprecated(true);
$def->setFactory('factory_func');
$def->setFile('foo.php');
$def->setLazy(true);
$def->setPublic(true);
$def->setShared(true);
$def->setSynthetic(true);
// changes aren't tracked for these, class or arguments
$def->setInstanceofConditionals(array());
$def->addTag('foo_tag');
$def->addMethodCall('methodCall');
$def->setProperty('fooprop', true);
$def->setAutoconfigured(true);
$this->assertSame(array(
'class' => true,
'autowired' => true,
'configurator' => true,
'decorated_service' => true,
'deprecated' => true,
'factory' => true,
'file' => true,
'lazy' => true,
'public' => true,
'shared' => true,
'autoconfigured' => true,
), $def->getChanges());
$def->setChanges(array());
$this->assertSame(array(), $def->getChanges());
}
public function testShouldAutoconfigure()
{
$def = new Definition('stdClass');
$this->assertFalse($def->isAutoconfigured());
$def->setAutoconfigured(true);
$this->assertTrue($def->isAutoconfigured());
}
public function testAddError()
{
$def = new Definition('stdClass');
$this->assertFalse($def->hasErrors());
$def->addError('First error');
$def->addError('Second error');
$this->assertSame(array('First error', 'Second error'), $def->getErrors());
}
}