You are here

class StackedKernelPassTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/StackedKernelPassTest.php \Drupal\Tests\Core\DependencyInjection\Compiler\StackedKernelPassTest

@coversDefaultClass \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass @group DependencyInjection

Hierarchy

Expanded class hierarchy of StackedKernelPassTest

File

core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/StackedKernelPassTest.php, line 19
Contains \Drupal\Tests\Core\DependencyInjection\Compiler\StackedKernelPassTest.

Namespace

Drupal\Tests\Core\DependencyInjection\Compiler
View source
class StackedKernelPassTest extends UnitTestCase {

  /**
   * The stacked kernel pass.
   *
   * @var \Drupal\Core\DependencyInjection\Compiler\StackedKernelPass
   */
  protected $stackedKernelPass;

  /**
   * @var \Drupal\Core\DependencyInjection\Container
   */
  protected $containerBuilder;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    $this->stackedKernelPass = new StackedKernelPass();
    $this->containerBuilder = new ContainerBuilder();
  }

  /**
   * @covers ::process
   */
  public function testProcessWithStackedKernel() {
    $stacked_kernel = new Definition('Stack\\StackedHttpKernel');
    $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();

    // Check the stacked kernel args.
    $this
      ->assertSame((string) $stacked_kernel_args[0], 'http_kernel.one');
    $this
      ->assertCount(4, $stacked_kernel_args[1]);
    $this
      ->assertSame((string) $stacked_kernel_args[1][0], 'http_kernel.one');
    $this
      ->assertSame((string) $stacked_kernel_args[1][1], 'http_kernel.two');
    $this
      ->assertSame((string) $stacked_kernel_args[1][2], 'http_kernel.three');
    $this
      ->assertSame((string) $stacked_kernel_args[1][3], 'http_kernel.basic');

    // Check the modified definitions.
    $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]);
  }

  /**
   * @covers ::process
   */
  public function testProcessWithHttpKernel() {
    $kernel = new Definition('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
    $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());
  }

  /**
   * Creates a middleware definition.
   *
   * @param bool $tag
   *   Whether or not to set the http_middleware tag.
   * @param int $priority
   *   The priority to be used for the tag.
   *
   * @return \Symfony\Component\DependencyInjection\Definition
   */
  protected function createMiddlewareServiceDefinition($tag = TRUE, $priority = 0) {
    $definition = new Definition('Symfony\\Component\\HttpKernel\\HttpKernelInterface', [
      'test',
    ]);
    if ($tag) {
      $definition
        ->addTag('http_middleware', [
        'priority' => $priority,
      ]);
    }
    return $definition;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StackedKernelPassTest::$containerBuilder protected property
StackedKernelPassTest::$stackedKernelPass protected property The stacked kernel pass.
StackedKernelPassTest::createMiddlewareServiceDefinition protected function Creates a middleware definition.
StackedKernelPassTest::setUp public function Overrides UnitTestCase::setUp
StackedKernelPassTest::testProcessWithHttpKernel public function @covers ::process
StackedKernelPassTest::testProcessWithStackedKernel public function @covers ::process
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.