ResolveClassPassTest.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Tests\Compiler;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\ChildDefinition;
  13. use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  15. use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
  16. class ResolveClassPassTest extends TestCase
  17. {
  18. /**
  19. * @dataProvider provideValidClassId
  20. */
  21. public function testResolveClassFromId($serviceId)
  22. {
  23. $container = new ContainerBuilder();
  24. $def = $container->register($serviceId);
  25. (new ResolveClassPass())->process($container);
  26. $this->assertSame($serviceId, $def->getClass());
  27. }
  28. public function provideValidClassId()
  29. {
  30. yield array('Acme\UnknownClass');
  31. yield array(CaseSensitiveClass::class);
  32. }
  33. /**
  34. * @dataProvider provideInvalidClassId
  35. */
  36. public function testWontResolveClassFromId($serviceId)
  37. {
  38. $container = new ContainerBuilder();
  39. $def = $container->register($serviceId);
  40. (new ResolveClassPass())->process($container);
  41. $this->assertNull($def->getClass());
  42. }
  43. public function provideInvalidClassId()
  44. {
  45. yield array(\stdClass::class);
  46. yield array('bar');
  47. yield array('\DateTime');
  48. }
  49. public function testNonFqcnChildDefinition()
  50. {
  51. $container = new ContainerBuilder();
  52. $parent = $container->register('App\Foo', null);
  53. $child = $container->setDefinition('App\Foo.child', new ChildDefinition('App\Foo'));
  54. (new ResolveClassPass())->process($container);
  55. $this->assertSame('App\Foo', $parent->getClass());
  56. $this->assertNull($child->getClass());
  57. }
  58. public function testClassFoundChildDefinition()
  59. {
  60. $container = new ContainerBuilder();
  61. $parent = $container->register('App\Foo', null);
  62. $child = $container->setDefinition(self::class, new ChildDefinition('App\Foo'));
  63. (new ResolveClassPass())->process($container);
  64. $this->assertSame('App\Foo', $parent->getClass());
  65. $this->assertSame(self::class, $child->getClass());
  66. }
  67. /**
  68. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  69. * @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.
  70. */
  71. public function testAmbiguousChildDefinition()
  72. {
  73. $container = new ContainerBuilder();
  74. $parent = $container->register('App\Foo', null);
  75. $child = $container->setDefinition('App\Foo\Child', new ChildDefinition('App\Foo'));
  76. (new ResolveClassPass())->process($container);
  77. }
  78. }