Updated to Drupal 8.5. Core Media not yet in use.
[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
17 class DefinitionTest extends TestCase
18 {
19     public function testConstructor()
20     {
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());
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();
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         $this->assertSame(array('factory' => true), $def->getChanges());
39     }
40
41     public function testSetGetClass()
42     {
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');
46     }
47
48     public function testSetGetDecoratedService()
49     {
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());
56
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());
63
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());
69
70         $def = new Definition('stdClass');
71
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.');
75         } else {
76             $this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');
77         }
78
79         $def->setDecoratedService('foo', 'foo');
80     }
81
82     public function testArguments()
83     {
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');
89     }
90
91     public function testMethodCalls()
92     {
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');
102     }
103
104     /**
105      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
106      * @expectedExceptionMessage Method name cannot be empty.
107      */
108     public function testExceptionOnEmptyMethodCall()
109     {
110         $def = new Definition('stdClass');
111         $def->addMethodCall('');
112     }
113
114     public function testSetGetFile()
115     {
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');
119     }
120
121     public function testSetIsShared()
122     {
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');
127     }
128
129     public function testSetIsPublic()
130     {
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.');
135     }
136
137     public function testSetIsSynthetic()
138     {
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.');
143     }
144
145     public function testSetIsLazy()
146     {
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.');
151     }
152
153     public function testSetIsAbstract()
154     {
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.');
159     }
160
161     public function testSetIsDeprecated()
162     {
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');
168     }
169
170     /**
171      * @dataProvider invalidDeprecationMessageProvider
172      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
173      */
174     public function testSetDeprecatedWithInvalidDeprecationTemplate($message)
175     {
176         $def = new Definition('stdClass');
177         $def->setDeprecated(false, $message);
178     }
179
180     public function invalidDeprecationMessageProvider()
181     {
182         return array(
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'),
187         );
188     }
189
190     public function testSetGetConfigurator()
191     {
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');
195     }
196
197     public function testClearTags()
198     {
199         $def = new Definition('stdClass');
200         $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
201         $def->addTag('foo', array('foo' => 'bar'));
202         $def->clearTags();
203         $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
204     }
205
206     public function testClearTag()
207     {
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'));
220     }
221
222     public function testTags()
223     {
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');
237     }
238
239     public function testSetArgument()
240     {
241         $def = new Definition('stdClass');
242
243         $def->addArgument('foo');
244         $this->assertSame(array('foo'), $def->getArguments());
245
246         $this->assertSame($def, $def->replaceArgument(0, 'moo'));
247         $this->assertSame(array('moo'), $def->getArguments());
248
249         $def->addArgument('moo');
250         $def
251             ->replaceArgument(0, 'foo')
252             ->replaceArgument(1, 'bar')
253         ;
254         $this->assertSame(array('foo', 'bar'), $def->getArguments());
255     }
256
257     /**
258      * @expectedException \OutOfBoundsException
259      */
260     public function testGetArgumentShouldCheckBounds()
261     {
262         $def = new Definition('stdClass');
263
264         $def->addArgument('foo');
265         $def->getArgument(1);
266     }
267
268     /**
269      * @expectedException \OutOfBoundsException
270      * @expectedExceptionMessage The index "1" is not in the range [0, 0].
271      */
272     public function testReplaceArgumentShouldCheckBounds()
273     {
274         $def = new Definition('stdClass');
275
276         $def->addArgument('foo');
277         $def->replaceArgument(1, 'bar');
278     }
279
280     /**
281      * @expectedException \OutOfBoundsException
282      * @expectedExceptionMessage Cannot replace arguments if none have been configured yet.
283      */
284     public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
285     {
286         $def = new Definition('stdClass');
287         $def->replaceArgument(0, 'bar');
288     }
289
290     public function testSetGetProperties()
291     {
292         $def = new Definition('stdClass');
293
294         $this->assertEquals(array(), $def->getProperties());
295         $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
296         $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
297     }
298
299     public function testSetProperty()
300     {
301         $def = new Definition('stdClass');
302
303         $this->assertEquals(array(), $def->getProperties());
304         $this->assertSame($def, $def->setProperty('foo', 'bar'));
305         $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
306     }
307
308     public function testAutowired()
309     {
310         $def = new Definition('stdClass');
311         $this->assertFalse($def->isAutowired());
312
313         $def->setAutowired(true);
314         $this->assertTrue($def->isAutowired());
315
316         $def->setAutowired(false);
317         $this->assertFalse($def->isAutowired());
318     }
319
320     public function testChangesNoChanges()
321     {
322         $def = new Definition();
323
324         $this->assertSame(array(), $def->getChanges());
325     }
326
327     public function testGetChangesWithChanges()
328     {
329         $def = new Definition('stdClass', array('fooarg'));
330
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');
338         $def->setLazy(true);
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);
348
349         $this->assertSame(array(
350             'class' => true,
351             'autowired' => true,
352             'configurator' => true,
353             'decorated_service' => true,
354             'deprecated' => true,
355             'factory' => true,
356             'file' => true,
357             'lazy' => true,
358             'public' => true,
359             'shared' => true,
360             'autoconfigured' => true,
361         ), $def->getChanges());
362
363         $def->setChanges(array());
364         $this->assertSame(array(), $def->getChanges());
365     }
366
367     /**
368      * @group legacy
369      */
370     public function testTypes()
371     {
372         $def = new Definition('stdClass');
373
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());
381     }
382
383     public function testShouldAutoconfigure()
384     {
385         $def = new Definition('stdClass');
386         $this->assertFalse($def->isAutoconfigured());
387         $def->setAutoconfigured(true);
388         $this->assertTrue($def->isAutoconfigured());
389     }
390
391     public function testAddError()
392     {
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());
398     }
399 }