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;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
18 class DefinitionTest extends TestCase
20 public function testConstructor()
22 $def = new Definition('stdClass');
23 $this->assertEquals('stdClass', $def->getClass(), '__construct() takes the class name as its first argument');
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('stdClass');
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');
40 public function testSetGetClass()
42 $def = new Definition('stdClass');
43 $this->assertSame($def, $def->setClass('foo'), '->setClass() implements a fluent interface');
44 $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
47 public function testSetGetDecoratedService()
49 $def = new Definition('stdClass');
50 $this->assertNull($def->getDecoratedService());
51 $def->setDecoratedService('foo', 'foo.renamed', 5);
52 $this->assertEquals(array('foo', 'foo.renamed', 5), $def->getDecoratedService());
53 $def->setDecoratedService(null);
54 $this->assertNull($def->getDecoratedService());
56 $def = new Definition('stdClass');
57 $this->assertNull($def->getDecoratedService());
58 $def->setDecoratedService('foo', 'foo.renamed');
59 $this->assertEquals(array('foo', 'foo.renamed', 0), $def->getDecoratedService());
60 $def->setDecoratedService(null);
61 $this->assertNull($def->getDecoratedService());
63 $def = new Definition('stdClass');
64 $def->setDecoratedService('foo');
65 $this->assertEquals(array('foo', null, 0), $def->getDecoratedService());
66 $def->setDecoratedService(null);
67 $this->assertNull($def->getDecoratedService());
69 $def = new Definition('stdClass');
71 if (method_exists($this, 'expectException')) {
72 $this->expectException('InvalidArgumentException');
73 $this->expectExceptionMessage('The decorated service inner name for "foo" must be different than the service name itself.');
75 $this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');
78 $def->setDecoratedService('foo', 'foo');
81 public function testArguments()
83 $def = new Definition('stdClass');
84 $this->assertSame($def, $def->setArguments(array('foo')), '->setArguments() implements a fluent interface');
85 $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
86 $this->assertSame($def, $def->addArgument('bar'), '->addArgument() implements a fluent interface');
87 $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
90 public function testMethodCalls()
92 $def = new Definition('stdClass');
93 $this->assertSame($def, $def->setMethodCalls(array(array('foo', array('foo')))), '->setMethodCalls() implements a fluent interface');
94 $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
95 $this->assertSame($def, $def->addMethodCall('bar', array('bar')), '->addMethodCall() implements a fluent interface');
96 $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
97 $this->assertTrue($def->hasMethodCall('bar'), '->hasMethodCall() returns true if first argument is a method to call registered');
98 $this->assertFalse($def->hasMethodCall('no_registered'), '->hasMethodCall() returns false if first argument is not a method to call registered');
99 $this->assertSame($def, $def->removeMethodCall('bar'), '->removeMethodCall() implements a fluent interface');
100 $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->removeMethodCall() removes a method to call');
104 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
105 * @expectedExceptionMessage Method name cannot be empty.
107 public function testExceptionOnEmptyMethodCall()
109 $def = new Definition('stdClass');
110 $def->addMethodCall('');
113 public function testSetGetFile()
115 $def = new Definition('stdClass');
116 $this->assertSame($def, $def->setFile('foo'), '->setFile() implements a fluent interface');
117 $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
120 public function testSetIsShared()
122 $def = new Definition('stdClass');
123 $this->assertTrue($def->isShared(), '->isShared() returns true by default');
124 $this->assertSame($def, $def->setShared(false), '->setShared() implements a fluent interface');
125 $this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
131 public function testPrototypeScopedDefinitionAreNotShared()
133 $def = new Definition('stdClass');
134 $def->setScope(ContainerInterface::SCOPE_PROTOTYPE);
136 $this->assertFalse($def->isShared());
137 $this->assertEquals(ContainerInterface::SCOPE_PROTOTYPE, $def->getScope());
143 public function testSetGetScope()
145 $def = new Definition('stdClass');
146 $this->assertEquals('container', $def->getScope());
147 $this->assertSame($def, $def->setScope('foo'));
148 $this->assertEquals('foo', $def->getScope());
151 public function testSetIsPublic()
153 $def = new Definition('stdClass');
154 $this->assertTrue($def->isPublic(), '->isPublic() returns true by default');
155 $this->assertSame($def, $def->setPublic(false), '->setPublic() implements a fluent interface');
156 $this->assertFalse($def->isPublic(), '->isPublic() returns false if the instance must not be public.');
159 public function testSetIsSynthetic()
161 $def = new Definition('stdClass');
162 $this->assertFalse($def->isSynthetic(), '->isSynthetic() returns false by default');
163 $this->assertSame($def, $def->setSynthetic(true), '->setSynthetic() implements a fluent interface');
164 $this->assertTrue($def->isSynthetic(), '->isSynthetic() returns true if the service is synthetic.');
170 public function testLegacySetIsSynchronized()
172 $def = new Definition('stdClass');
173 $this->assertFalse($def->isSynchronized(), '->isSynchronized() returns false by default');
174 $this->assertSame($def, $def->setSynchronized(true), '->setSynchronized() implements a fluent interface');
175 $this->assertTrue($def->isSynchronized(), '->isSynchronized() returns true if the service is synchronized.');
178 public function testSetIsLazy()
180 $def = new Definition('stdClass');
181 $this->assertFalse($def->isLazy(), '->isLazy() returns false by default');
182 $this->assertSame($def, $def->setLazy(true), '->setLazy() implements a fluent interface');
183 $this->assertTrue($def->isLazy(), '->isLazy() returns true if the service is lazy.');
186 public function testSetIsAbstract()
188 $def = new Definition('stdClass');
189 $this->assertFalse($def->isAbstract(), '->isAbstract() returns false by default');
190 $this->assertSame($def, $def->setAbstract(true), '->setAbstract() implements a fluent interface');
191 $this->assertTrue($def->isAbstract(), '->isAbstract() returns true if the instance must not be public.');
194 public function testSetIsDeprecated()
196 $def = new Definition('stdClass');
197 $this->assertFalse($def->isDeprecated(), '->isDeprecated() returns false by default');
198 $this->assertSame($def, $def->setDeprecated(true), '->setDeprecated() implements a fluent interface');
199 $this->assertTrue($def->isDeprecated(), '->isDeprecated() returns true if the instance should not be used anymore.');
200 $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');
204 * @dataProvider invalidDeprecationMessageProvider
205 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
207 public function testSetDeprecatedWithInvalidDeprecationTemplate($message)
209 $def = new Definition('stdClass');
210 $def->setDeprecated(false, $message);
213 public function invalidDeprecationMessageProvider()
216 "With \rs" => array("invalid \r message %service_id%"),
217 "With \ns" => array("invalid \n message %service_id%"),
218 'With */s' => array('invalid */ message %service_id%'),
219 'message not containing require %service_id% variable' => array('this is deprecated'),
223 public function testSetGetConfigurator()
225 $def = new Definition('stdClass');
226 $this->assertSame($def, $def->setConfigurator('foo'), '->setConfigurator() implements a fluent interface');
227 $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
230 public function testClearTags()
232 $def = new Definition('stdClass');
233 $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
234 $def->addTag('foo', array('foo' => 'bar'));
236 $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
239 public function testClearTag()
241 $def = new Definition('stdClass');
242 $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
243 $def->addTag('1foo1', array('foo1' => 'bar1'));
244 $def->addTag('2foo2', array('foo2' => 'bar2'));
245 $def->addTag('3foo3', array('foo3' => 'bar3'));
246 $def->clearTag('2foo2');
247 $this->assertTrue($def->hasTag('1foo1'));
248 $this->assertFalse($def->hasTag('2foo2'));
249 $this->assertTrue($def->hasTag('3foo3'));
250 $def->clearTag('1foo1');
251 $this->assertFalse($def->hasTag('1foo1'));
252 $this->assertTrue($def->hasTag('3foo3'));
255 public function testTags()
257 $def = new Definition('stdClass');
258 $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
259 $this->assertFalse($def->hasTag('foo'));
260 $this->assertSame($def, $def->addTag('foo'), '->addTag() implements a fluent interface');
261 $this->assertTrue($def->hasTag('foo'));
262 $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
263 $def->addTag('foo', array('foo' => 'bar'));
264 $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
265 $def->addTag('bar', array('bar' => 'bar'));
266 $this->assertEquals($def->getTags(), array(
267 'foo' => array(array(), array('foo' => 'bar')),
268 'bar' => array(array('bar' => 'bar')),
269 ), '->getTags() returns all tags');
272 public function testSetArgument()
274 $def = new Definition('stdClass');
276 $def->addArgument('foo');
277 $this->assertSame(array('foo'), $def->getArguments());
279 $this->assertSame($def, $def->replaceArgument(0, 'moo'));
280 $this->assertSame(array('moo'), $def->getArguments());
282 $def->addArgument('moo');
284 ->replaceArgument(0, 'foo')
285 ->replaceArgument(1, 'bar')
287 $this->assertSame(array('foo', 'bar'), $def->getArguments());
291 * @expectedException \OutOfBoundsException
293 public function testGetArgumentShouldCheckBounds()
295 $def = new Definition('stdClass');
297 $def->addArgument('foo');
298 $def->getArgument(1);
302 * @expectedException \OutOfBoundsException
303 * @expectedExceptionMessage The index "1" is not in the range [0, 0].
305 public function testReplaceArgumentShouldCheckBounds()
307 $def = new Definition('stdClass');
309 $def->addArgument('foo');
310 $def->replaceArgument(1, 'bar');
314 * @expectedException \OutOfBoundsException
315 * @expectedExceptionMessage Cannot replace arguments if none have been configured yet.
317 public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
319 $def = new Definition('stdClass');
320 $def->replaceArgument(0, 'bar');
323 public function testSetGetProperties()
325 $def = new Definition('stdClass');
327 $this->assertEquals(array(), $def->getProperties());
328 $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
329 $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
332 public function testSetProperty()
334 $def = new Definition('stdClass');
336 $this->assertEquals(array(), $def->getProperties());
337 $this->assertSame($def, $def->setProperty('foo', 'bar'));
338 $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
341 public function testAutowired()
343 $def = new Definition('stdClass');
344 $this->assertFalse($def->isAutowired());
345 $def->setAutowired(true);
346 $this->assertTrue($def->isAutowired());
349 public function testTypes()
351 $def = new Definition('stdClass');
353 $this->assertEquals(array(), $def->getAutowiringTypes());
354 $this->assertSame($def, $def->setAutowiringTypes(array('Foo')));
355 $this->assertEquals(array('Foo'), $def->getAutowiringTypes());
356 $this->assertSame($def, $def->addAutowiringType('Bar'));
357 $this->assertTrue($def->hasAutowiringType('Bar'));
358 $this->assertSame($def, $def->removeAutowiringType('Foo'));
359 $this->assertEquals(array('Bar'), $def->getAutowiringTypes());