View source
<?php
namespace Drupal\Tests\Core\DependencyInjection\Compiler;
use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Definition;
class StackedKernelPassTest extends UnitTestCase {
protected $stackedKernelPass;
protected $containerBuilder;
protected function setUp() : void {
$this->stackedKernelPass = new StackedKernelPass();
$this->containerBuilder = new ContainerBuilder();
}
public function testProcessWithStackedKernel() {
$stacked_kernel = new Definition('Stack\\StackedHttpKernel');
$stacked_kernel
->setPublic(TRUE);
$this->containerBuilder
->setDefinition('http_kernel', $stacked_kernel);
$this->containerBuilder
->setDefinition('http_kernel.basic', $this
->createMiddlewareServiceDefinition(FALSE, 0));
$this->containerBuilder
->setDefinition('http_kernel.three', $this
->createMiddlewareServiceDefinition());
$this->containerBuilder
->setDefinition('http_kernel.one', $this
->createMiddlewareServiceDefinition(TRUE, 10));
$this->containerBuilder
->setDefinition('http_kernel.two', $this
->createMiddlewareServiceDefinition(TRUE, 5));
$this->stackedKernelPass
->process($this->containerBuilder);
$stacked_kernel_args = $this->containerBuilder
->getDefinition('http_kernel')
->getArguments();
$this
->assertSame('http_kernel.one', (string) $stacked_kernel_args[0]);
$this
->assertCount(4, $stacked_kernel_args[1]);
$this
->assertSame('http_kernel.one', (string) $stacked_kernel_args[1][0]);
$this
->assertSame('http_kernel.two', (string) $stacked_kernel_args[1][1]);
$this
->assertSame('http_kernel.three', (string) $stacked_kernel_args[1][2]);
$this
->assertSame('http_kernel.basic', (string) $stacked_kernel_args[1][3]);
$definition = $this->containerBuilder
->getDefinition('http_kernel.one');
$args = $definition
->getArguments();
$this
->assertSame('http_kernel.two', (string) $args[0]);
$this
->assertSame('test', $args[1]);
$definition = $this->containerBuilder
->getDefinition('http_kernel.two');
$args = $definition
->getArguments();
$this
->assertSame('http_kernel.three', (string) $args[0]);
$this
->assertSame('test', $args[1]);
$definition = $this->containerBuilder
->getDefinition('http_kernel.three');
$args = $definition
->getArguments();
$this
->assertSame('http_kernel.basic', (string) $args[0]);
$this
->assertSame('test', $args[1]);
}
public function testProcessWithHttpKernel() {
$kernel = new Definition('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
$kernel
->setPublic(TRUE);
$this->containerBuilder
->setDefinition('http_kernel', $kernel);
$this->stackedKernelPass
->process($this->containerBuilder);
$unprocessed_kernel = $this->containerBuilder
->getDefinition('http_kernel');
$this
->assertSame($kernel, $unprocessed_kernel);
$this
->assertSame($kernel
->getArguments(), $unprocessed_kernel
->getArguments());
}
protected function createMiddlewareServiceDefinition($tag = TRUE, $priority = 0) {
$definition = new Definition('Symfony\\Component\\HttpKernel\\HttpKernelInterface', [
'test',
]);
$definition
->setPublic(TRUE);
if ($tag) {
$definition
->addTag('http_middleware', [
'priority' => $priority,
]);
}
return $definition;
}
}