ProxyServicesPassTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Drupal\Tests\Core\DependencyInjection\Compiler;
  3. use Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass;
  4. use Drupal\Core\DependencyInjection\ContainerBuilder;
  5. use Drupal\Core\Path\CurrentPathStack;
  6. use Drupal\Tests\UnitTestCase;
  7. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  8. /**
  9. * @coversDefaultClass \Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass
  10. * @group DependencyInjection
  11. */
  12. class ProxyServicesPassTest extends UnitTestCase {
  13. /**
  14. * The tested proxy services pass.
  15. *
  16. * @var \Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass
  17. */
  18. protected $proxyServicesPass;
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUp() {
  23. parent::setUp();
  24. $this->proxyServicesPass = new ProxyServicesPass();
  25. }
  26. /**
  27. * @covers ::process
  28. */
  29. public function testContainerWithoutLazyServices() {
  30. $container = new ContainerBuilder();
  31. $container->register('plugin_cache_clearer', 'Drupal\Core\Plugin\CachedDiscoveryClearer');
  32. $this->proxyServicesPass->process($container);
  33. $this->assertCount(2, $container->getDefinitions());
  34. $this->assertEquals('Drupal\Core\Plugin\CachedDiscoveryClearer', $container->getDefinition('plugin_cache_clearer')->getClass());
  35. }
  36. /**
  37. * @covers ::process
  38. */
  39. public function testContainerWithLazyServices() {
  40. $container = new ContainerBuilder();
  41. $container->register('plugin_cache_clearer', 'Drupal\Core\Plugin\CachedDiscoveryClearer')
  42. ->setLazy(TRUE);
  43. $this->proxyServicesPass->process($container);
  44. $this->assertCount(3, $container->getDefinitions());
  45. $non_proxy_definition = $container->getDefinition('drupal.proxy_original_service.plugin_cache_clearer');
  46. $this->assertEquals('Drupal\Core\Plugin\CachedDiscoveryClearer', $non_proxy_definition->getClass());
  47. $this->assertFalse($non_proxy_definition->isLazy());
  48. $this->assertTrue($non_proxy_definition->isPublic());
  49. $this->assertEquals('Drupal\Core\ProxyClass\Plugin\CachedDiscoveryClearer', $container->getDefinition('plugin_cache_clearer')->getClass());
  50. }
  51. /**
  52. * @covers ::process
  53. */
  54. public function testContainerWithLazyServicesWithoutProxyClass() {
  55. $container = new ContainerBuilder();
  56. $container->register('path.current', CurrentPathStack::class)
  57. ->setLazy(TRUE);
  58. $this->expectException(InvalidArgumentException::class);
  59. $this->proxyServicesPass->process($container);
  60. }
  61. }