acbcf1dda2d8ce5c0253a4e21e84ec0d96ffc4fa
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Compiler / ResolveClassPassTest.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\Compiler;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ChildDefinition;
16 use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
19
20 class ResolveClassPassTest extends TestCase
21 {
22     /**
23      * @dataProvider provideValidClassId
24      */
25     public function testResolveClassFromId($serviceId)
26     {
27         $container = new ContainerBuilder();
28         $def = $container->register($serviceId);
29
30         (new ResolveClassPass())->process($container);
31
32         $this->assertSame($serviceId, $def->getClass());
33     }
34
35     public function provideValidClassId()
36     {
37         yield array('Acme\UnknownClass');
38         yield array(CaseSensitiveClass::class);
39     }
40
41     /**
42      * @dataProvider provideInvalidClassId
43      */
44     public function testWontResolveClassFromId($serviceId)
45     {
46         $container = new ContainerBuilder();
47         $def = $container->register($serviceId);
48
49         (new ResolveClassPass())->process($container);
50
51         $this->assertNull($def->getClass());
52     }
53
54     public function provideInvalidClassId()
55     {
56         yield array(\stdClass::class);
57         yield array('bar');
58         yield array('\DateTime');
59     }
60
61     public function testNonFqcnChildDefinition()
62     {
63         $container = new ContainerBuilder();
64         $parent = $container->register('App\Foo', null);
65         $child = $container->setDefinition('App\Foo.child', new ChildDefinition('App\Foo'));
66
67         (new ResolveClassPass())->process($container);
68
69         $this->assertSame('App\Foo', $parent->getClass());
70         $this->assertNull($child->getClass());
71     }
72
73     public function testClassFoundChildDefinition()
74     {
75         $container = new ContainerBuilder();
76         $parent = $container->register('App\Foo', null);
77         $child = $container->setDefinition(self::class, new ChildDefinition('App\Foo'));
78
79         (new ResolveClassPass())->process($container);
80
81         $this->assertSame('App\Foo', $parent->getClass());
82         $this->assertSame(self::class, $child->getClass());
83     }
84
85     /**
86      * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
87      * @expectedExceptionMessage Service definition "App\Foo\Child" has a parent but no class, and its name looks like a FQCN. Either the class is missing or you want to inherit it from the parent service. To resolve this ambiguity, please rename this service to a non-FQCN (e.g. using dots), or create the missing class.
88      */
89     public function testAmbiguousChildDefinition()
90     {
91         $container = new ContainerBuilder();
92         $parent = $container->register('App\Foo', null);
93         $child = $container->setDefinition('App\Foo\Child', new ChildDefinition('App\Foo'));
94
95         (new ResolveClassPass())->process($container);
96     }
97 }