1e385da5477ab9a8a30601127a00fc5828fc9035
[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
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');
26     }
27
28     public function testSetGetFactory()
29     {
30         $def = new Definition('stdClass');
31
32         $this->assertSame($def, $def->setFactory('foo'), '->setFactory() implements a fluent interface');
33         $this->assertEquals('foo', $def->getFactory(), '->getFactory() returns the factory');
34
35         $def->setFactory('Foo::bar');
36         $this->assertEquals(array('Foo', 'bar'), $def->getFactory(), '->setFactory() converts string static method call to the array');
37     }
38
39     public function testSetGetClass()
40     {
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');
44     }
45
46     public function testSetGetDecoratedService()
47     {
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());
54
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());
61
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());
67
68         $def = new Definition('stdClass');
69
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.');
73         } else {
74             $this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');
75         }
76
77         $def->setDecoratedService('foo', 'foo');
78     }
79
80     public function testArguments()
81     {
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');
87     }
88
89     public function testMethodCalls()
90     {
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');
100     }
101
102     /**
103      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
104      * @expectedExceptionMessage Method name cannot be empty.
105      */
106     public function testExceptionOnEmptyMethodCall()
107     {
108         $def = new Definition('stdClass');
109         $def->addMethodCall('');
110     }
111
112     public function testSetGetFile()
113     {
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');
117     }
118
119     public function testSetIsShared()
120     {
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');
125     }
126
127     public function testSetIsPublic()
128     {
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.');
133     }
134
135     public function testSetIsSynthetic()
136     {
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.');
141     }
142
143     public function testSetIsLazy()
144     {
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.');
149     }
150
151     public function testSetIsAbstract()
152     {
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.');
157     }
158
159     public function testSetIsDeprecated()
160     {
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');
166     }
167
168     /**
169      * @dataProvider invalidDeprecationMessageProvider
170      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
171      */
172     public function testSetDeprecatedWithInvalidDeprecationTemplate($message)
173     {
174         $def = new Definition('stdClass');
175         $def->setDeprecated(false, $message);
176     }
177
178     public function invalidDeprecationMessageProvider()
179     {
180         return array(
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'),
185         );
186     }
187
188     public function testSetGetConfigurator()
189     {
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');
193     }
194
195     public function testClearTags()
196     {
197         $def = new Definition('stdClass');
198         $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
199         $def->addTag('foo', array('foo' => 'bar'));
200         $def->clearTags();
201         $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
202     }
203
204     public function testClearTag()
205     {
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'));
218     }
219
220     public function testTags()
221     {
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');
235     }
236
237     public function testSetArgument()
238     {
239         $def = new Definition('stdClass');
240
241         $def->addArgument('foo');
242         $this->assertSame(array('foo'), $def->getArguments());
243
244         $this->assertSame($def, $def->replaceArgument(0, 'moo'));
245         $this->assertSame(array('moo'), $def->getArguments());
246
247         $def->addArgument('moo');
248         $def
249             ->replaceArgument(0, 'foo')
250             ->replaceArgument(1, 'bar')
251         ;
252         $this->assertSame(array('foo', 'bar'), $def->getArguments());
253     }
254
255     /**
256      * @expectedException \OutOfBoundsException
257      */
258     public function testGetArgumentShouldCheckBounds()
259     {
260         $def = new Definition('stdClass');
261
262         $def->addArgument('foo');
263         $def->getArgument(1);
264     }
265
266     /**
267      * @expectedException \OutOfBoundsException
268      * @expectedExceptionMessage The index "1" is not in the range [0, 0].
269      */
270     public function testReplaceArgumentShouldCheckBounds()
271     {
272         $def = new Definition('stdClass');
273
274         $def->addArgument('foo');
275         $def->replaceArgument(1, 'bar');
276     }
277
278     /**
279      * @expectedException \OutOfBoundsException
280      * @expectedExceptionMessage Cannot replace arguments if none have been configured yet.
281      */
282     public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
283     {
284         $def = new Definition('stdClass');
285         $def->replaceArgument(0, 'bar');
286     }
287
288     public function testSetGetProperties()
289     {
290         $def = new Definition('stdClass');
291
292         $this->assertEquals(array(), $def->getProperties());
293         $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
294         $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
295     }
296
297     public function testSetProperty()
298     {
299         $def = new Definition('stdClass');
300
301         $this->assertEquals(array(), $def->getProperties());
302         $this->assertSame($def, $def->setProperty('foo', 'bar'));
303         $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
304     }
305
306     public function testAutowired()
307     {
308         $def = new Definition('stdClass');
309         $this->assertFalse($def->isAutowired());
310         $def->setAutowired(true);
311         $this->assertTrue($def->isAutowired());
312     }
313
314     public function testTypes()
315     {
316         $def = new Definition('stdClass');
317
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());
325     }
326 }