Yaffs site version 1.1
[yaffs-website] / vendor / symfony / dependency-injection / Tests / DefinitionTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Definition;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17
18 class DefinitionTest extends TestCase
19 {
20     public function testConstructor()
21     {
22         $def = new Definition('stdClass');
23         $this->assertEquals('stdClass', $def->getClass(), '__construct() takes the class name as its first argument');
24
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');
27     }
28
29     public function testSetGetFactory()
30     {
31         $def = new Definition('stdClass');
32
33         $this->assertSame($def, $def->setFactory('foo'), '->setFactory() implements a fluent interface');
34         $this->assertEquals('foo', $def->getFactory(), '->getFactory() returns the factory');
35
36         $def->setFactory('Foo::bar');
37         $this->assertEquals(array('Foo', 'bar'), $def->getFactory(), '->setFactory() converts string static method call to the array');
38     }
39
40     public function testSetGetClass()
41     {
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');
45     }
46
47     public function testSetGetDecoratedService()
48     {
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());
55
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());
62
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());
68
69         $def = new Definition('stdClass');
70
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.');
74         } else {
75             $this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');
76         }
77
78         $def->setDecoratedService('foo', 'foo');
79     }
80
81     public function testArguments()
82     {
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');
88     }
89
90     public function testMethodCalls()
91     {
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');
101     }
102
103     /**
104      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
105      * @expectedExceptionMessage Method name cannot be empty.
106      */
107     public function testExceptionOnEmptyMethodCall()
108     {
109         $def = new Definition('stdClass');
110         $def->addMethodCall('');
111     }
112
113     public function testSetGetFile()
114     {
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');
118     }
119
120     public function testSetIsShared()
121     {
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');
126     }
127
128     /**
129      * @group legacy
130      */
131     public function testPrototypeScopedDefinitionAreNotShared()
132     {
133         $def = new Definition('stdClass');
134         $def->setScope(ContainerInterface::SCOPE_PROTOTYPE);
135
136         $this->assertFalse($def->isShared());
137         $this->assertEquals(ContainerInterface::SCOPE_PROTOTYPE, $def->getScope());
138     }
139
140     /**
141      * @group legacy
142      */
143     public function testSetGetScope()
144     {
145         $def = new Definition('stdClass');
146         $this->assertEquals('container', $def->getScope());
147         $this->assertSame($def, $def->setScope('foo'));
148         $this->assertEquals('foo', $def->getScope());
149     }
150
151     public function testSetIsPublic()
152     {
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.');
157     }
158
159     public function testSetIsSynthetic()
160     {
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.');
165     }
166
167     /**
168      * @group legacy
169      */
170     public function testLegacySetIsSynchronized()
171     {
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.');
176     }
177
178     public function testSetIsLazy()
179     {
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.');
184     }
185
186     public function testSetIsAbstract()
187     {
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.');
192     }
193
194     public function testSetIsDeprecated()
195     {
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');
201     }
202
203     /**
204      * @dataProvider invalidDeprecationMessageProvider
205      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
206      */
207     public function testSetDeprecatedWithInvalidDeprecationTemplate($message)
208     {
209         $def = new Definition('stdClass');
210         $def->setDeprecated(false, $message);
211     }
212
213     public function invalidDeprecationMessageProvider()
214     {
215         return array(
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'),
220         );
221     }
222
223     public function testSetGetConfigurator()
224     {
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');
228     }
229
230     public function testClearTags()
231     {
232         $def = new Definition('stdClass');
233         $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
234         $def->addTag('foo', array('foo' => 'bar'));
235         $def->clearTags();
236         $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
237     }
238
239     public function testClearTag()
240     {
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'));
253     }
254
255     public function testTags()
256     {
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');
270     }
271
272     public function testSetArgument()
273     {
274         $def = new Definition('stdClass');
275
276         $def->addArgument('foo');
277         $this->assertSame(array('foo'), $def->getArguments());
278
279         $this->assertSame($def, $def->replaceArgument(0, 'moo'));
280         $this->assertSame(array('moo'), $def->getArguments());
281
282         $def->addArgument('moo');
283         $def
284             ->replaceArgument(0, 'foo')
285             ->replaceArgument(1, 'bar')
286         ;
287         $this->assertSame(array('foo', 'bar'), $def->getArguments());
288     }
289
290     /**
291      * @expectedException \OutOfBoundsException
292      */
293     public function testGetArgumentShouldCheckBounds()
294     {
295         $def = new Definition('stdClass');
296
297         $def->addArgument('foo');
298         $def->getArgument(1);
299     }
300
301     /**
302      * @expectedException \OutOfBoundsException
303      * @expectedExceptionMessage The index "1" is not in the range [0, 0].
304      */
305     public function testReplaceArgumentShouldCheckBounds()
306     {
307         $def = new Definition('stdClass');
308
309         $def->addArgument('foo');
310         $def->replaceArgument(1, 'bar');
311     }
312
313     /**
314      * @expectedException \OutOfBoundsException
315      * @expectedExceptionMessage Cannot replace arguments if none have been configured yet.
316      */
317     public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
318     {
319         $def = new Definition('stdClass');
320         $def->replaceArgument(0, 'bar');
321     }
322
323     public function testSetGetProperties()
324     {
325         $def = new Definition('stdClass');
326
327         $this->assertEquals(array(), $def->getProperties());
328         $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
329         $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
330     }
331
332     public function testSetProperty()
333     {
334         $def = new Definition('stdClass');
335
336         $this->assertEquals(array(), $def->getProperties());
337         $this->assertSame($def, $def->setProperty('foo', 'bar'));
338         $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
339     }
340
341     public function testAutowired()
342     {
343         $def = new Definition('stdClass');
344         $this->assertFalse($def->isAutowired());
345         $def->setAutowired(true);
346         $this->assertTrue($def->isAutowired());
347     }
348
349     public function testTypes()
350     {
351         $def = new Definition('stdClass');
352
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());
360     }
361 }