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\Compiler;
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;
20 class ResolveClassPassTest extends TestCase
23 * @dataProvider provideValidClassId
25 public function testResolveClassFromId($serviceId)
27 $container = new ContainerBuilder();
28 $def = $container->register($serviceId);
30 (new ResolveClassPass())->process($container);
32 $this->assertSame($serviceId, $def->getClass());
35 public function provideValidClassId()
37 yield array('Acme\UnknownClass');
38 yield array(CaseSensitiveClass::class);
42 * @dataProvider provideInvalidClassId
44 public function testWontResolveClassFromId($serviceId)
46 $container = new ContainerBuilder();
47 $def = $container->register($serviceId);
49 (new ResolveClassPass())->process($container);
51 $this->assertNull($def->getClass());
54 public function provideInvalidClassId()
56 yield array(\stdClass::class);
58 yield array('\DateTime');
61 public function testNonFqcnChildDefinition()
63 $container = new ContainerBuilder();
64 $parent = $container->register('App\Foo', null);
65 $child = $container->setDefinition('App\Foo.child', new ChildDefinition('App\Foo'));
67 (new ResolveClassPass())->process($container);
69 $this->assertSame('App\Foo', $parent->getClass());
70 $this->assertNull($child->getClass());
73 public function testClassFoundChildDefinition()
75 $container = new ContainerBuilder();
76 $parent = $container->register('App\Foo', null);
77 $child = $container->setDefinition(self::class, new ChildDefinition('App\Foo'));
79 (new ResolveClassPass())->process($container);
81 $this->assertSame('App\Foo', $parent->getClass());
82 $this->assertSame(self::class, $child->getClass());
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.
89 public function testAmbiguousChildDefinition()
91 $container = new ContainerBuilder();
92 $parent = $container->register('App\Foo', null);
93 $child = $container->setDefinition('App\Foo\Child', new ChildDefinition('App\Foo'));
95 (new ResolveClassPass())->process($container);