ServiceSubscriberTraitTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\Contracts\Tests\Service;
  11. use PHPUnit\Framework\TestCase;
  12. use Psr\Container\ContainerInterface;
  13. use Symfony\Contracts\Service\ServiceLocatorTrait;
  14. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  15. use Symfony\Contracts\Service\ServiceSubscriberTrait;
  16. class ServiceSubscriberTraitTest extends TestCase
  17. {
  18. public function testMethodsOnParentsAndChildrenAreIgnoredInGetSubscribedServices()
  19. {
  20. $expected = [TestService::class.'::aService' => '?Symfony\Contracts\Tests\Service\Service2'];
  21. $this->assertEquals($expected, ChildTestService::getSubscribedServices());
  22. }
  23. public function testSetContainerIsCalledOnParent()
  24. {
  25. $container = new class([]) implements ContainerInterface {
  26. use ServiceLocatorTrait;
  27. };
  28. $this->assertSame($container, (new TestService())->setContainer($container));
  29. }
  30. }
  31. class ParentTestService
  32. {
  33. public function aParentService(): Service1
  34. {
  35. }
  36. public function setContainer(ContainerInterface $container)
  37. {
  38. return $container;
  39. }
  40. }
  41. class TestService extends ParentTestService implements ServiceSubscriberInterface
  42. {
  43. use ServiceSubscriberTrait;
  44. public function aService(): Service2
  45. {
  46. }
  47. }
  48. class ChildTestService extends TestService
  49. {
  50. public function aChildService(): Service3
  51. {
  52. }
  53. }