function StackedKernelPassTest::testProcessWithStackedKernelAndServiceClosureMiddleware

Tests that class taking a service closure can be added as http_middleware.

Attributes

#[DataProvider('providerTestClosureMiddleware')]

File

core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/StackedKernelPassTest.php, line 164

Class

StackedKernelPassTest
Tests Drupal\Core\DependencyInjection\Compiler\StackedKernelPass.

Namespace

Drupal\Tests\Core\DependencyInjection\Compiler

Code

public function testProcessWithStackedKernelAndServiceClosureMiddleware(string $closureClass) : void {
  $stacked_kernel = new Definition(StackedHttpKernel::class);
  $stacked_kernel->setPublic(TRUE);
  $this->containerBuilder
    ->setDefinition('http_kernel', $stacked_kernel);
  $basic_kernel = $this->createMock(HttpKernelInterface::class);
  $basic_definition = (new Definition($basic_kernel::class))->setPublic(TRUE);
  $this->containerBuilder
    ->setDefinition('http_kernel.basic', $basic_definition);
  $this->containerBuilder
    ->setDefinition('http_kernel.one', (new Definition(TestHttpMiddlewareClass::class))->setPublic(TRUE)
    ->addTag('http_middleware', [
    'priority' => 200,
  ]));
  // Middleware class taking a service closure as its inner kernel argument.
  // Inner services will only be constructed when required.
  $this->containerBuilder
    ->setDefinition('http_kernel.two', (new Definition(TestClosureHttpMiddlewareClass::class))->setPublic(TRUE)
    ->addTag('http_middleware', [
    'priority' => 100,
  ]));
  // Middleware class declared final, this time without implementing
  // TerminableInterface.
  $this->containerBuilder
    ->setDefinition('http_kernel.three', (new Definition(FinalTestNonTerminableHttpMiddlewareClass::class))->setPublic(TRUE)
    ->addTag('http_middleware', [
    'priority' => 50,
  ]));
  $this->stackedKernelPass
    ->process($this->containerBuilder);
  $this->assertIsObject($this->containerBuilder
    ->get('http_kernel'));
  $this->assertTrue($this->containerBuilder
    ->initialized('http_kernel'));
  $this->assertTrue($this->containerBuilder
    ->initialized('http_kernel.one'));
  $this->assertTrue($this->containerBuilder
    ->initialized('http_kernel.two'));
  $this->assertFalse($this->containerBuilder
    ->initialized('http_kernel.three'));
  $this->assertFalse($this->containerBuilder
    ->initialized('http_kernel.basic'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.