StackedKernelPassTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace Drupal\Tests\Core\DependencyInjection\Compiler;
  3. use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass;
  4. use Drupal\Core\DependencyInjection\ContainerBuilder;
  5. use Drupal\Tests\UnitTestCase;
  6. use Symfony\Component\DependencyInjection\Definition;
  7. /**
  8. * @coversDefaultClass \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass
  9. * @group DependencyInjection
  10. */
  11. class StackedKernelPassTest extends UnitTestCase {
  12. /**
  13. * The stacked kernel pass.
  14. *
  15. * @var \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass
  16. */
  17. protected $stackedKernelPass;
  18. /**
  19. * @var \Drupal\Core\DependencyInjection\Container
  20. */
  21. protected $containerBuilder;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function setUp() {
  26. $this->stackedKernelPass = new StackedKernelPass();
  27. $this->containerBuilder = new ContainerBuilder();
  28. }
  29. /**
  30. * @covers ::process
  31. */
  32. public function testProcessWithStackedKernel() {
  33. $stacked_kernel = new Definition('Stack\StackedHttpKernel');
  34. $this->containerBuilder->setDefinition('http_kernel', $stacked_kernel);
  35. $this->containerBuilder->setDefinition('http_kernel.basic', $this->createMiddlewareServiceDefinition(FALSE, 0));
  36. $this->containerBuilder->setDefinition('http_kernel.three', $this->createMiddlewareServiceDefinition());
  37. $this->containerBuilder->setDefinition('http_kernel.one', $this->createMiddlewareServiceDefinition(TRUE, 10));
  38. $this->containerBuilder->setDefinition('http_kernel.two', $this->createMiddlewareServiceDefinition(TRUE, 5));
  39. $this->stackedKernelPass->process($this->containerBuilder);
  40. $stacked_kernel_args = $this->containerBuilder->getDefinition('http_kernel')->getArguments();
  41. // Check the stacked kernel args.
  42. $this->assertSame('http_kernel.one', (string) $stacked_kernel_args[0]);
  43. $this->assertCount(4, $stacked_kernel_args[1]);
  44. $this->assertSame('http_kernel.one', (string) $stacked_kernel_args[1][0]);
  45. $this->assertSame('http_kernel.two', (string) $stacked_kernel_args[1][1]);
  46. $this->assertSame('http_kernel.three', (string) $stacked_kernel_args[1][2]);
  47. $this->assertSame('http_kernel.basic', (string) $stacked_kernel_args[1][3]);
  48. // Check the modified definitions.
  49. $definition = $this->containerBuilder->getDefinition('http_kernel.one');
  50. $args = $definition->getArguments();
  51. $this->assertSame('http_kernel.two', (string) $args[0]);
  52. $this->assertSame('test', $args[1]);
  53. $definition = $this->containerBuilder->getDefinition('http_kernel.two');
  54. $args = $definition->getArguments();
  55. $this->assertSame('http_kernel.three', (string) $args[0]);
  56. $this->assertSame('test', $args[1]);
  57. $definition = $this->containerBuilder->getDefinition('http_kernel.three');
  58. $args = $definition->getArguments();
  59. $this->assertSame('http_kernel.basic', (string) $args[0]);
  60. $this->assertSame('test', $args[1]);
  61. }
  62. /**
  63. * @covers ::process
  64. */
  65. public function testProcessWithHttpKernel() {
  66. $kernel = new Definition('Symfony\Component\HttpKernel\HttpKernelInterface');
  67. $this->containerBuilder->setDefinition('http_kernel', $kernel);
  68. $this->stackedKernelPass->process($this->containerBuilder);
  69. $unprocessed_kernel = $this->containerBuilder->getDefinition('http_kernel');
  70. $this->assertSame($kernel, $unprocessed_kernel);
  71. $this->assertSame($kernel->getArguments(), $unprocessed_kernel->getArguments());
  72. }
  73. /**
  74. * Creates a middleware definition.
  75. *
  76. * @param bool $tag
  77. * Whether or not to set the http_middleware tag.
  78. * @param int $priority
  79. * The priority to be used for the tag.
  80. *
  81. * @return \Symfony\Component\DependencyInjection\Definition
  82. */
  83. protected function createMiddlewareServiceDefinition($tag = TRUE, $priority = 0) {
  84. $definition = new Definition('Symfony\Component\HttpKernel\HttpKernelInterface', ['test']);
  85. if ($tag) {
  86. $definition->addTag('http_middleware', ['priority' => $priority]);
  87. }
  88. return $definition;
  89. }
  90. }