4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Definition;
17 class DefinitionTest extends TestCase
19 public function testConstructor()
21 $def = new Definition('stdClass');
22 $this->assertEquals('stdClass', $def->getClass(), '__construct() takes the class name as its first argument');
23 $this->assertSame(array('class' => true), $def->getChanges());
25 $def = new Definition('stdClass', array('foo'));
26 $this->assertEquals(array('foo'), $def->getArguments(), '__construct() takes an optional array of arguments as its second argument');
29 public function testSetGetFactory()
31 $def = new Definition();
33 $this->assertSame($def, $def->setFactory('foo'), '->setFactory() implements a fluent interface');
34 $this->assertEquals('foo', $def->getFactory(), '->getFactory() returns the factory');
36 $def->setFactory('Foo::bar');
37 $this->assertEquals(array('Foo', 'bar'), $def->getFactory(), '->setFactory() converts string static method call to the array');
38 $this->assertSame(array('factory' => true), $def->getChanges());
41 public function testSetGetClass()
43 $def = new Definition('stdClass');
44 $this->assertSame($def, $def->setClass('foo'), '->setClass() implements a fluent interface');
45 $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
48 public function testSetGetDecoratedService()
50 $def = new Definition('stdClass');
51 $this->assertNull($def->getDecoratedService());
52 $def->setDecoratedService('foo', 'foo.renamed', 5);
53 $this->assertEquals(array('foo', 'foo.renamed', 5), $def->getDecoratedService());
54 $def->setDecoratedService(null);
55 $this->assertNull($def->getDecoratedService());
57 $def = new Definition('stdClass');
58 $this->assertNull($def->getDecoratedService());
59 $def->setDecoratedService('foo', 'foo.renamed');
60 $this->assertEquals(array('foo', 'foo.renamed', 0), $def->getDecoratedService());
61 $def->setDecoratedService(null);
62 $this->assertNull($def->getDecoratedService());
64 $def = new Definition('stdClass');
65 $def->setDecoratedService('foo');
66 $this->assertEquals(array('foo', null, 0), $def->getDecoratedService());
67 $def->setDecoratedService(null);
68 $this->assertNull($def->getDecoratedService());
70 $def = new Definition('stdClass');
72 if (method_exists($this, 'expectException')) {
73 $this->expectException('InvalidArgumentException');
74 $this->expectExceptionMessage('The decorated service inner name for "foo" must be different than the service name itself.');
76 $this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');
79 $def->setDecoratedService('foo', 'foo');
82 public function testArguments()
84 $def = new Definition('stdClass');
85 $this->assertSame($def, $def->setArguments(array('foo')), '->setArguments() implements a fluent interface');
86 $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
87 $this->assertSame($def, $def->addArgument('bar'), '->addArgument() implements a fluent interface');
88 $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
91 public function testMethodCalls()
93 $def = new Definition('stdClass');
94 $this->assertSame($def, $def->setMethodCalls(array(array('foo', array('foo')))), '->setMethodCalls() implements a fluent interface');
95 $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
96 $this->assertSame($def, $def->addMethodCall('bar', array('bar')), '->addMethodCall() implements a fluent interface');
97 $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
98 $this->assertTrue($def->hasMethodCall('bar'), '->hasMethodCall() returns true if first argument is a method to call registered');
99 $this->assertFalse($def->hasMethodCall('no_registered'), '->hasMethodCall() returns false if first argument is not a method to call registered');
100 $this->assertSame($def, $def->removeMethodCall('bar'), '->removeMethodCall() implements a fluent interface');
101 $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->removeMethodCall() removes a method to call');
105 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
106 * @expectedExceptionMessage Method name cannot be empty.
108 public function testExceptionOnEmptyMethodCall()
110 $def = new Definition('stdClass');
111 $def->addMethodCall('');
114 public function testSetGetFile()
116 $def = new Definition('stdClass');
117 $this->assertSame($def, $def->setFile('foo'), '->setFile() implements a fluent interface');
118 $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
121 public function testSetIsShared()
123 $def = new Definition('stdClass');
124 $this->assertTrue($def->isShared(), '->isShared() returns true by default');
125 $this->assertSame($def, $def->setShared(false), '->setShared() implements a fluent interface');
126 $this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
129 public function testSetIsPublic()
131 $def = new Definition('stdClass');
132 $this->assertTrue($def->isPublic(), '->isPublic() returns true by default');
133 $this->assertSame($def, $def->setPublic(false), '->setPublic() implements a fluent interface');
134 $this->assertFalse($def->isPublic(), '->isPublic() returns false if the instance must not be public.');
137 public function testSetIsSynthetic()
139 $def = new Definition('stdClass');
140 $this->assertFalse($def->isSynthetic(), '->isSynthetic() returns false by default');
141 $this->assertSame($def, $def->setSynthetic(true), '->setSynthetic() implements a fluent interface');
142 $this->assertTrue($def->isSynthetic(), '->isSynthetic() returns true if the service is synthetic.');
145 public function testSetIsLazy()
147 $def = new Definition('stdClass');
148 $this->assertFalse($def->isLazy(), '->isLazy() returns false by default');
149 $this->assertSame($def, $def->setLazy(true), '->setLazy() implements a fluent interface');
150 $this->assertTrue($def->isLazy(), '->isLazy() returns true if the service is lazy.');
153 public function testSetIsAbstract()
155 $def = new Definition('stdClass');
156 $this->assertFalse($def->isAbstract(), '->isAbstract() returns false by default');
157 $this->assertSame($def, $def->setAbstract(true), '->setAbstract() implements a fluent interface');
158 $this->assertTrue($def->isAbstract(), '->isAbstract() returns true if the instance must not be public.');
161 public function testSetIsDeprecated()
163 $def = new Definition('stdClass');
164 $this->assertFalse($def->isDeprecated(), '->isDeprecated() returns false by default');
165 $this->assertSame($def, $def->setDeprecated(true), '->setDeprecated() implements a fluent interface');
166 $this->assertTrue($def->isDeprecated(), '->isDeprecated() returns true if the instance should not be used anymore.');
167 $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');
171 * @dataProvider invalidDeprecationMessageProvider
172 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
174 public function testSetDeprecatedWithInvalidDeprecationTemplate($message)
176 $def = new Definition('stdClass');
177 $def->setDeprecated(false, $message);
180 public function invalidDeprecationMessageProvider()
183 "With \rs" => array("invalid \r message %service_id%"),
184 "With \ns" => array("invalid \n message %service_id%"),
185 'With */s' => array('invalid */ message %service_id%'),
186 'message not containing require %service_id% variable' => array('this is deprecated'),
190 public function testSetGetConfigurator()
192 $def = new Definition('stdClass');
193 $this->assertSame($def, $def->setConfigurator('foo'), '->setConfigurator() implements a fluent interface');
194 $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
197 public function testClearTags()
199 $def = new Definition('stdClass');
200 $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
201 $def->addTag('foo', array('foo' => 'bar'));
203 $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
206 public function testClearTag()
208 $def = new Definition('stdClass');
209 $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
210 $def->addTag('1foo1', array('foo1' => 'bar1'));
211 $def->addTag('2foo2', array('foo2' => 'bar2'));
212 $def->addTag('3foo3', array('foo3' => 'bar3'));
213 $def->clearTag('2foo2');
214 $this->assertTrue($def->hasTag('1foo1'));
215 $this->assertFalse($def->hasTag('2foo2'));
216 $this->assertTrue($def->hasTag('3foo3'));
217 $def->clearTag('1foo1');
218 $this->assertFalse($def->hasTag('1foo1'));
219 $this->assertTrue($def->hasTag('3foo3'));
222 public function testTags()
224 $def = new Definition('stdClass');
225 $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
226 $this->assertFalse($def->hasTag('foo'));
227 $this->assertSame($def, $def->addTag('foo'), '->addTag() implements a fluent interface');
228 $this->assertTrue($def->hasTag('foo'));
229 $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
230 $def->addTag('foo', array('foo' => 'bar'));
231 $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
232 $def->addTag('bar', array('bar' => 'bar'));
233 $this->assertEquals($def->getTags(), array(
234 'foo' => array(array(), array('foo' => 'bar')),
235 'bar' => array(array('bar' => 'bar')),
236 ), '->getTags() returns all tags');
239 public function testSetArgument()
241 $def = new Definition('stdClass');
243 $def->addArgument('foo');
244 $this->assertSame(array('foo'), $def->getArguments());
246 $this->assertSame($def, $def->replaceArgument(0, 'moo'));
247 $this->assertSame(array('moo'), $def->getArguments());
249 $def->addArgument('moo');
251 ->replaceArgument(0, 'foo')
252 ->replaceArgument(1, 'bar')
254 $this->assertSame(array('foo', 'bar'), $def->getArguments());
258 * @expectedException \OutOfBoundsException
260 public function testGetArgumentShouldCheckBounds()
262 $def = new Definition('stdClass');
264 $def->addArgument('foo');
265 $def->getArgument(1);
269 * @expectedException \OutOfBoundsException
270 * @expectedExceptionMessage The index "1" is not in the range [0, 0].
272 public function testReplaceArgumentShouldCheckBounds()
274 $def = new Definition('stdClass');
276 $def->addArgument('foo');
277 $def->replaceArgument(1, 'bar');
281 * @expectedException \OutOfBoundsException
282 * @expectedExceptionMessage Cannot replace arguments if none have been configured yet.
284 public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
286 $def = new Definition('stdClass');
287 $def->replaceArgument(0, 'bar');
290 public function testSetGetProperties()
292 $def = new Definition('stdClass');
294 $this->assertEquals(array(), $def->getProperties());
295 $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
296 $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
299 public function testSetProperty()
301 $def = new Definition('stdClass');
303 $this->assertEquals(array(), $def->getProperties());
304 $this->assertSame($def, $def->setProperty('foo', 'bar'));
305 $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
308 public function testAutowired()
310 $def = new Definition('stdClass');
311 $this->assertFalse($def->isAutowired());
313 $def->setAutowired(true);
314 $this->assertTrue($def->isAutowired());
316 $def->setAutowired(false);
317 $this->assertFalse($def->isAutowired());
320 public function testChangesNoChanges()
322 $def = new Definition();
324 $this->assertSame(array(), $def->getChanges());
327 public function testGetChangesWithChanges()
329 $def = new Definition('stdClass', array('fooarg'));
331 $def->setAbstract(true);
332 $def->setAutowired(true);
333 $def->setConfigurator('configuration_func');
334 $def->setDecoratedService(null);
335 $def->setDeprecated(true);
336 $def->setFactory('factory_func');
337 $def->setFile('foo.php');
339 $def->setPublic(true);
340 $def->setShared(true);
341 $def->setSynthetic(true);
342 // changes aren't tracked for these, class or arguments
343 $def->setInstanceofConditionals(array());
344 $def->addTag('foo_tag');
345 $def->addMethodCall('methodCall');
346 $def->setProperty('fooprop', true);
347 $def->setAutoconfigured(true);
349 $this->assertSame(array(
352 'configurator' => true,
353 'decorated_service' => true,
354 'deprecated' => true,
360 'autoconfigured' => true,
361 ), $def->getChanges());
363 $def->setChanges(array());
364 $this->assertSame(array(), $def->getChanges());
370 public function testTypes()
372 $def = new Definition('stdClass');
374 $this->assertEquals(array(), $def->getAutowiringTypes());
375 $this->assertSame($def, $def->setAutowiringTypes(array('Foo')));
376 $this->assertEquals(array('Foo'), $def->getAutowiringTypes());
377 $this->assertSame($def, $def->addAutowiringType('Bar'));
378 $this->assertTrue($def->hasAutowiringType('Bar'));
379 $this->assertSame($def, $def->removeAutowiringType('Foo'));
380 $this->assertEquals(array('Bar'), $def->getAutowiringTypes());
383 public function testShouldAutoconfigure()
385 $def = new Definition('stdClass');
386 $this->assertFalse($def->isAutoconfigured());
387 $def->setAutoconfigured(true);
388 $this->assertTrue($def->isAutoconfigured());
391 public function testAddError()
393 $def = new Definition('stdClass');
394 $this->assertEmpty($def->getErrors());
395 $def->addError('First error');
396 $def->addError('Second error');
397 $this->assertSame(array('First error', 'Second error'), $def->getErrors());