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');
24 $def = new Definition('stdClass', array('foo'));
25 $this->assertEquals(array('foo'), $def->getArguments(), '__construct() takes an optional array of arguments as its second argument');
28 public function testSetGetFactory()
30 $def = new Definition('stdClass');
32 $this->assertSame($def, $def->setFactory('foo'), '->setFactory() implements a fluent interface');
33 $this->assertEquals('foo', $def->getFactory(), '->getFactory() returns the factory');
35 $def->setFactory('Foo::bar');
36 $this->assertEquals(array('Foo', 'bar'), $def->getFactory(), '->setFactory() converts string static method call to the array');
39 public function testSetGetClass()
41 $def = new Definition('stdClass');
42 $this->assertSame($def, $def->setClass('foo'), '->setClass() implements a fluent interface');
43 $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
46 public function testSetGetDecoratedService()
48 $def = new Definition('stdClass');
49 $this->assertNull($def->getDecoratedService());
50 $def->setDecoratedService('foo', 'foo.renamed', 5);
51 $this->assertEquals(array('foo', 'foo.renamed', 5), $def->getDecoratedService());
52 $def->setDecoratedService(null);
53 $this->assertNull($def->getDecoratedService());
55 $def = new Definition('stdClass');
56 $this->assertNull($def->getDecoratedService());
57 $def->setDecoratedService('foo', 'foo.renamed');
58 $this->assertEquals(array('foo', 'foo.renamed', 0), $def->getDecoratedService());
59 $def->setDecoratedService(null);
60 $this->assertNull($def->getDecoratedService());
62 $def = new Definition('stdClass');
63 $def->setDecoratedService('foo');
64 $this->assertEquals(array('foo', null, 0), $def->getDecoratedService());
65 $def->setDecoratedService(null);
66 $this->assertNull($def->getDecoratedService());
68 $def = new Definition('stdClass');
70 if (method_exists($this, 'expectException')) {
71 $this->expectException('InvalidArgumentException');
72 $this->expectExceptionMessage('The decorated service inner name for "foo" must be different than the service name itself.');
74 $this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');
77 $def->setDecoratedService('foo', 'foo');
80 public function testArguments()
82 $def = new Definition('stdClass');
83 $this->assertSame($def, $def->setArguments(array('foo')), '->setArguments() implements a fluent interface');
84 $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
85 $this->assertSame($def, $def->addArgument('bar'), '->addArgument() implements a fluent interface');
86 $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
89 public function testMethodCalls()
91 $def = new Definition('stdClass');
92 $this->assertSame($def, $def->setMethodCalls(array(array('foo', array('foo')))), '->setMethodCalls() implements a fluent interface');
93 $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
94 $this->assertSame($def, $def->addMethodCall('bar', array('bar')), '->addMethodCall() implements a fluent interface');
95 $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
96 $this->assertTrue($def->hasMethodCall('bar'), '->hasMethodCall() returns true if first argument is a method to call registered');
97 $this->assertFalse($def->hasMethodCall('no_registered'), '->hasMethodCall() returns false if first argument is not a method to call registered');
98 $this->assertSame($def, $def->removeMethodCall('bar'), '->removeMethodCall() implements a fluent interface');
99 $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->removeMethodCall() removes a method to call');
103 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
104 * @expectedExceptionMessage Method name cannot be empty.
106 public function testExceptionOnEmptyMethodCall()
108 $def = new Definition('stdClass');
109 $def->addMethodCall('');
112 public function testSetGetFile()
114 $def = new Definition('stdClass');
115 $this->assertSame($def, $def->setFile('foo'), '->setFile() implements a fluent interface');
116 $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
119 public function testSetIsShared()
121 $def = new Definition('stdClass');
122 $this->assertTrue($def->isShared(), '->isShared() returns true by default');
123 $this->assertSame($def, $def->setShared(false), '->setShared() implements a fluent interface');
124 $this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
127 public function testSetIsPublic()
129 $def = new Definition('stdClass');
130 $this->assertTrue($def->isPublic(), '->isPublic() returns true by default');
131 $this->assertSame($def, $def->setPublic(false), '->setPublic() implements a fluent interface');
132 $this->assertFalse($def->isPublic(), '->isPublic() returns false if the instance must not be public.');
135 public function testSetIsSynthetic()
137 $def = new Definition('stdClass');
138 $this->assertFalse($def->isSynthetic(), '->isSynthetic() returns false by default');
139 $this->assertSame($def, $def->setSynthetic(true), '->setSynthetic() implements a fluent interface');
140 $this->assertTrue($def->isSynthetic(), '->isSynthetic() returns true if the service is synthetic.');
143 public function testSetIsLazy()
145 $def = new Definition('stdClass');
146 $this->assertFalse($def->isLazy(), '->isLazy() returns false by default');
147 $this->assertSame($def, $def->setLazy(true), '->setLazy() implements a fluent interface');
148 $this->assertTrue($def->isLazy(), '->isLazy() returns true if the service is lazy.');
151 public function testSetIsAbstract()
153 $def = new Definition('stdClass');
154 $this->assertFalse($def->isAbstract(), '->isAbstract() returns false by default');
155 $this->assertSame($def, $def->setAbstract(true), '->setAbstract() implements a fluent interface');
156 $this->assertTrue($def->isAbstract(), '->isAbstract() returns true if the instance must not be public.');
159 public function testSetIsDeprecated()
161 $def = new Definition('stdClass');
162 $this->assertFalse($def->isDeprecated(), '->isDeprecated() returns false by default');
163 $this->assertSame($def, $def->setDeprecated(true), '->setDeprecated() implements a fluent interface');
164 $this->assertTrue($def->isDeprecated(), '->isDeprecated() returns true if the instance should not be used anymore.');
165 $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');
169 * @dataProvider invalidDeprecationMessageProvider
170 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
172 public function testSetDeprecatedWithInvalidDeprecationTemplate($message)
174 $def = new Definition('stdClass');
175 $def->setDeprecated(false, $message);
178 public function invalidDeprecationMessageProvider()
181 "With \rs" => array("invalid \r message %service_id%"),
182 "With \ns" => array("invalid \n message %service_id%"),
183 'With */s' => array('invalid */ message %service_id%'),
184 'message not containing require %service_id% variable' => array('this is deprecated'),
188 public function testSetGetConfigurator()
190 $def = new Definition('stdClass');
191 $this->assertSame($def, $def->setConfigurator('foo'), '->setConfigurator() implements a fluent interface');
192 $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
195 public function testClearTags()
197 $def = new Definition('stdClass');
198 $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
199 $def->addTag('foo', array('foo' => 'bar'));
201 $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
204 public function testClearTag()
206 $def = new Definition('stdClass');
207 $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
208 $def->addTag('1foo1', array('foo1' => 'bar1'));
209 $def->addTag('2foo2', array('foo2' => 'bar2'));
210 $def->addTag('3foo3', array('foo3' => 'bar3'));
211 $def->clearTag('2foo2');
212 $this->assertTrue($def->hasTag('1foo1'));
213 $this->assertFalse($def->hasTag('2foo2'));
214 $this->assertTrue($def->hasTag('3foo3'));
215 $def->clearTag('1foo1');
216 $this->assertFalse($def->hasTag('1foo1'));
217 $this->assertTrue($def->hasTag('3foo3'));
220 public function testTags()
222 $def = new Definition('stdClass');
223 $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
224 $this->assertFalse($def->hasTag('foo'));
225 $this->assertSame($def, $def->addTag('foo'), '->addTag() implements a fluent interface');
226 $this->assertTrue($def->hasTag('foo'));
227 $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
228 $def->addTag('foo', array('foo' => 'bar'));
229 $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
230 $def->addTag('bar', array('bar' => 'bar'));
231 $this->assertEquals($def->getTags(), array(
232 'foo' => array(array(), array('foo' => 'bar')),
233 'bar' => array(array('bar' => 'bar')),
234 ), '->getTags() returns all tags');
237 public function testSetArgument()
239 $def = new Definition('stdClass');
241 $def->addArgument('foo');
242 $this->assertSame(array('foo'), $def->getArguments());
244 $this->assertSame($def, $def->replaceArgument(0, 'moo'));
245 $this->assertSame(array('moo'), $def->getArguments());
247 $def->addArgument('moo');
249 ->replaceArgument(0, 'foo')
250 ->replaceArgument(1, 'bar')
252 $this->assertSame(array('foo', 'bar'), $def->getArguments());
256 * @expectedException \OutOfBoundsException
258 public function testGetArgumentShouldCheckBounds()
260 $def = new Definition('stdClass');
262 $def->addArgument('foo');
263 $def->getArgument(1);
267 * @expectedException \OutOfBoundsException
268 * @expectedExceptionMessage The index "1" is not in the range [0, 0].
270 public function testReplaceArgumentShouldCheckBounds()
272 $def = new Definition('stdClass');
274 $def->addArgument('foo');
275 $def->replaceArgument(1, 'bar');
279 * @expectedException \OutOfBoundsException
280 * @expectedExceptionMessage Cannot replace arguments if none have been configured yet.
282 public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
284 $def = new Definition('stdClass');
285 $def->replaceArgument(0, 'bar');
288 public function testSetGetProperties()
290 $def = new Definition('stdClass');
292 $this->assertEquals(array(), $def->getProperties());
293 $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
294 $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
297 public function testSetProperty()
299 $def = new Definition('stdClass');
301 $this->assertEquals(array(), $def->getProperties());
302 $this->assertSame($def, $def->setProperty('foo', 'bar'));
303 $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
306 public function testAutowired()
308 $def = new Definition('stdClass');
309 $this->assertFalse($def->isAutowired());
310 $def->setAutowired(true);
311 $this->assertTrue($def->isAutowired());
314 public function testTypes()
316 $def = new Definition('stdClass');
318 $this->assertEquals(array(), $def->getAutowiringTypes());
319 $this->assertSame($def, $def->setAutowiringTypes(array('Foo')));
320 $this->assertEquals(array('Foo'), $def->getAutowiringTypes());
321 $this->assertSame($def, $def->addAutowiringType('Bar'));
322 $this->assertTrue($def->hasAutowiringType('Bar'));
323 $this->assertSame($def, $def->removeAutowiringType('Foo'));
324 $this->assertEquals(array('Bar'), $def->getAutowiringTypes());