3 namespace Drupal\Tests\Core\DependencyInjection\Compiler;
5 use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass;
6 use Drupal\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\DependencyInjection\Definition;
11 * @coversDefaultClass \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass
12 * @group DependencyInjection
14 class StackedKernelPassTest extends UnitTestCase {
17 * The stacked kernel pass.
19 * @var \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass
21 protected $stackedKernelPass;
24 * @var \Drupal\Core\DependencyInjection\Container
26 protected $containerBuilder;
31 protected function setUp() {
32 $this->stackedKernelPass = new StackedKernelPass();
33 $this->containerBuilder = new ContainerBuilder();
39 public function testProcessWithStackedKernel() {
40 $stacked_kernel = new Definition('Stack\StackedHttpKernel');
42 $this->containerBuilder->setDefinition('http_kernel', $stacked_kernel);
43 $this->containerBuilder->setDefinition('http_kernel.basic', $this->createMiddlewareServiceDefinition(FALSE, 0));
45 $this->containerBuilder->setDefinition('http_kernel.three', $this->createMiddlewareServiceDefinition());
46 $this->containerBuilder->setDefinition('http_kernel.one', $this->createMiddlewareServiceDefinition(TRUE, 10));
47 $this->containerBuilder->setDefinition('http_kernel.two', $this->createMiddlewareServiceDefinition(TRUE, 5));
49 $this->stackedKernelPass->process($this->containerBuilder);
51 $stacked_kernel_args = $this->containerBuilder->getDefinition('http_kernel')->getArguments();
53 // Check the stacked kernel args.
54 $this->assertSame('http_kernel.one', (string) $stacked_kernel_args[0]);
55 $this->assertCount(4, $stacked_kernel_args[1]);
56 $this->assertSame('http_kernel.one', (string) $stacked_kernel_args[1][0]);
57 $this->assertSame('http_kernel.two', (string) $stacked_kernel_args[1][1]);
58 $this->assertSame('http_kernel.three', (string) $stacked_kernel_args[1][2]);
59 $this->assertSame('http_kernel.basic', (string) $stacked_kernel_args[1][3]);
61 // Check the modified definitions.
62 $definition = $this->containerBuilder->getDefinition('http_kernel.one');
63 $args = $definition->getArguments();
64 $this->assertSame('http_kernel.two', (string) $args[0]);
65 $this->assertSame('test', $args[1]);
67 $definition = $this->containerBuilder->getDefinition('http_kernel.two');
68 $args = $definition->getArguments();
69 $this->assertSame('http_kernel.three', (string) $args[0]);
70 $this->assertSame('test', $args[1]);
72 $definition = $this->containerBuilder->getDefinition('http_kernel.three');
73 $args = $definition->getArguments();
74 $this->assertSame('http_kernel.basic', (string) $args[0]);
75 $this->assertSame('test', $args[1]);
81 public function testProcessWithHttpKernel() {
82 $kernel = new Definition('Symfony\Component\HttpKernel\HttpKernelInterface');
83 $this->containerBuilder->setDefinition('http_kernel', $kernel);
84 $this->stackedKernelPass->process($this->containerBuilder);
86 $unprocessed_kernel = $this->containerBuilder->getDefinition('http_kernel');
88 $this->assertSame($kernel, $unprocessed_kernel);
89 $this->assertSame($kernel->getArguments(), $unprocessed_kernel->getArguments());
93 * Creates a middleware definition.
96 * Whether or not to set the http_middleware tag.
97 * @param int $priority
98 * The priority to be used for the tag.
100 * @return \Symfony\Component\DependencyInjection\Definition
102 protected function createMiddlewareServiceDefinition($tag = TRUE, $priority = 0) {
103 $definition = new Definition('Symfony\Component\HttpKernel\HttpKernelInterface', ['test']);
106 $definition->addTag('http_middleware', ['priority' => $priority]);