Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Core / DependencyInjection / Compiler / StackedKernelPassTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\DependencyInjection\Compiler;
4
5 use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass;
6 use Drupal\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\DependencyInjection\Definition;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass
12  * @group DependencyInjection
13  */
14 class StackedKernelPassTest extends UnitTestCase {
15
16   /**
17    * The stacked kernel pass.
18    *
19    * @var \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass
20    */
21   protected $stackedKernelPass;
22
23   /**
24    * @var \Drupal\Core\DependencyInjection\Container
25    */
26   protected $containerBuilder;
27
28   /**
29    * {@inheritdoc}
30    */
31   protected function setUp() {
32     $this->stackedKernelPass = new StackedKernelPass();
33     $this->containerBuilder = new ContainerBuilder();
34   }
35
36   /**
37    * @covers ::process
38    */
39   public function testProcessWithStackedKernel() {
40     $stacked_kernel = new Definition('Stack\StackedHttpKernel');
41
42     $this->containerBuilder->setDefinition('http_kernel', $stacked_kernel);
43     $this->containerBuilder->setDefinition('http_kernel.basic', $this->createMiddlewareServiceDefinition(FALSE, 0));
44
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));
48
49     $this->stackedKernelPass->process($this->containerBuilder);
50
51     $stacked_kernel_args = $this->containerBuilder->getDefinition('http_kernel')->getArguments();
52
53     // Check the stacked kernel args.
54     $this->assertSame((string) $stacked_kernel_args[0], 'http_kernel.one');
55     $this->assertCount(4, $stacked_kernel_args[1]);
56     $this->assertSame((string) $stacked_kernel_args[1][0], 'http_kernel.one');
57     $this->assertSame((string) $stacked_kernel_args[1][1], 'http_kernel.two');
58     $this->assertSame((string) $stacked_kernel_args[1][2], 'http_kernel.three');
59     $this->assertSame((string) $stacked_kernel_args[1][3], 'http_kernel.basic');
60
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]);
66
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]);
71
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]);
76   }
77
78   /**
79    * @covers ::process
80    */
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);
85
86     $unprocessed_kernel = $this->containerBuilder->getDefinition('http_kernel');
87
88     $this->assertSame($kernel, $unprocessed_kernel);
89     $this->assertSame($kernel->getArguments(), $unprocessed_kernel->getArguments());
90   }
91
92   /**
93    * Creates a middleware definition.
94    *
95    * @param bool $tag
96    *   Whether or not to set the http_middleware tag.
97    * @param int $priority
98    *   The priority to be used for the tag.
99    *
100    * @return \Symfony\Component\DependencyInjection\Definition
101    */
102   protected function createMiddlewareServiceDefinition($tag = TRUE, $priority = 0) {
103     $definition = new Definition('Symfony\Component\HttpKernel\HttpKernelInterface', ['test']);
104
105     if ($tag) {
106       $definition->addTag('http_middleware', ['priority' => $priority]);
107     }
108
109     return $definition;
110   }
111
112 }