ResettableServicePassTest.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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', array('method' => 'reset'));
  20. $container->register('two', ClearableService::class)
  21. ->setPublic(true)
  22. ->addTag('kernel.reset', array('method' => 'clear'));
  23. $container->register('services_resetter', ServicesResetter::class)
  24. ->setPublic(true)
  25. ->setArguments(array(null, array()));
  26. $container->addCompilerPass(new ResettableServicePass());
  27. $container->compile();
  28. $definition = $container->getDefinition('services_resetter');
  29. $this->assertEquals(
  30. array(
  31. new IteratorArgument(array(
  32. 'one' => new Reference('one', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
  33. 'two' => new Reference('two', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE),
  34. )),
  35. array(
  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(array(null, array()));
  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(array(null, array()));
  62. $container->addCompilerPass(new ResettableServicePass());
  63. $container->compile();
  64. $this->assertFalse($container->has('services_resetter'));
  65. }
  66. }