ResettableServicePassTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
  3. use PHPUnit\Framework\TestCase;
  4. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  5. use Symfony\Component\DependencyInjection\ContainerBuilder;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\DependencyInjection\Reference;
  8. use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
  9. use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
  10. use Symfony\Component\HttpKernel\Tests\Fixtures\ClearableService;
  11. use Symfony\Component\HttpKernel\Tests\Fixtures\ResettableService;
  12. class ResettableServicePassTest extends TestCase
  13. {
  14. public function testCompilerPass()
  15. {
  16. $container = new ContainerBuilder();
  17. $container->register('one', ResettableService::class)
  18. ->setPublic(true)
  19. ->addTag('kernel.reset', ['method' => 'reset']);
  20. $container->register('two', ClearableService::class)
  21. ->setPublic(true)
  22. ->addTag('kernel.reset', ['method' => 'clear']);
  23. $container->register('services_resetter', ServicesResetter::class)
  24. ->setPublic(true)
  25. ->setArguments([null, []]);
  26. $container->addCompilerPass(new ResettableServicePass());
  27. $container->compile();
  28. $definition = $container->getDefinition('services_resetter');
  29. $this->assertEquals(
  30. [
  31. new IteratorArgument([
  32. 'one' => new Reference('one', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
  33. 'two' => new Reference('two', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
  34. ]),
  35. [
  36. 'one' => 'reset',
  37. 'two' => 'clear',
  38. ],
  39. ],
  40. $definition->getArguments()
  41. );
  42. }
  43. /**
  44. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  45. * @expectedExceptionMessage Tag kernel.reset requires the "method" attribute to be set.
  46. */
  47. public function testMissingMethod()
  48. {
  49. $container = new ContainerBuilder();
  50. $container->register(ResettableService::class)
  51. ->addTag('kernel.reset');
  52. $container->register('services_resetter', ServicesResetter::class)
  53. ->setArguments([null, []]);
  54. $container->addCompilerPass(new ResettableServicePass());
  55. $container->compile();
  56. }
  57. public function testCompilerPassWithoutResetters()
  58. {
  59. $container = new ContainerBuilder();
  60. $container->register('services_resetter', ServicesResetter::class)
  61. ->setArguments([null, []]);
  62. $container->addCompilerPass(new ResettableServicePass());
  63. $container->compile();
  64. $this->assertFalse($container->has('services_resetter'));
  65. }
  66. }