Commit af82c328 authored by catch's avatar catch
Browse files

fix: #3611084 DrupalApplication should use the compiled container

By: alexpott
By: moshe weitzman
(cherry picked from commit 854ccc02)
parent 89b938b4
Loading
Loading
Loading
Loading
Loading
+1 −6
Original line number Diff line number Diff line
@@ -95,12 +95,7 @@ protected function bootstrap(): bool {
      // Discovery can get out of whack if cleared caches and try to run this
      // command without a web request priming discovery.
      chdir(\DRUPAL_ROOT);
      // We need to not load a cached copy of the container from disk. For
      // example, inside Kernel tests, we need to fully build the container so
      // we discover and register commands, instead of reusing the container
      // from the Kernel test itself. Therefore, we pass `FALSE` for the
      // `$allow_dumping` parameter here.
      $kernel = new DrupalKernel('prod', $this->classloader, FALSE);
      $kernel = new DrupalKernel($this->context['kernel.environment'] ?? 'prod', $this->classloader, $this->context['kernel.allow_dumping'] ?? TRUE);
      // We tried calling `DrupalKernel::bootEnvironment()` right here to setup
      // some common environment and PHP initialization steps. However, that
      // method also calls `set_error_handler('_drupal_error_handler')` which
+2 −11
Original line number Diff line number Diff line
@@ -4,13 +4,13 @@

namespace Drupal\Tests\system\Kernel\Command;

use Drupal\KernelTests\DrupalApplicationTesterTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\system\Command\StatusCommand;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\ApplicationTester;

/**
 * Tests the 'system:status' command.
@@ -19,6 +19,7 @@
#[RunTestsInSeparateProcesses]
#[CoversClass(StatusCommand::class)]
class StatusCommandTest extends KernelTestBase {
  use DrupalApplicationTesterTrait;

  /**
   * {@inheritdoc}
@@ -101,14 +102,4 @@ protected function assertStringContainsStringNoWhitespace(string $needle, string
    $this->assertStringContainsString($normalized_needle, $normalized_haystack, $message ?? '');
  }

  /**
   * Build our ApplicationTester.
   */
  private function applicationTester(array $context = []): ApplicationTester {
    $application = include __DIR__ . '/../../../../../../../vendor/bin/dr';
    $application = $application($context);
    $application->setAutoExit(FALSE);
    return new ApplicationTester($application);
  }

}
+2 −11
Original line number Diff line number Diff line
@@ -9,13 +9,13 @@
use Drupal\console_test\Command\ConsoleExampleConfigureCommand;
use Drupal\console_test\Command\ConsoleExamplePrivateCommand;
use Drupal\Core\DependencyInjection\Compiler\ConsoleCompilerPass;
use Drupal\KernelTests\DrupalApplicationTesterTrait;
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LazyCommand;
use Symfony\Component\Console\Tester\ApplicationTester;

/**
 * Tests integration with Symfony Console.
@@ -25,6 +25,7 @@
#[CoversClass(ConsoleCompilerPass::class)]
#[CoversClass(ConsoleExampleCommand::class)]
class ConsoleTest extends KernelTestBase {
  use DrupalApplicationTesterTrait;

  /**
   * {@inheritdoc}
@@ -116,14 +117,4 @@ public function testSubdirectoryCommand(): void {
    $this->assertStringContainsString('[OK] Done', $tester->getDisplay());
  }

  /**
   * Builds an ApplicationTester to invoke `vendor/bin/dr`.
   */
  private function applicationTester(array $context = []): ApplicationTester {
    $application = include __DIR__ . '/../../../../../../vendor/bin/dr';
    $application = $application($context);
    $application->setAutoExit(FALSE);
    return new ApplicationTester($application);
  }

}
+26 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\KernelTests;

use Symfony\Component\Console\Tester\ApplicationTester;

/**
 * Trait to help with testing the drupal console.
 */
trait DrupalApplicationTesterTrait {

  /**
   * Build our ApplicationTester.
   */
  private function applicationTester(array $context = []): ApplicationTester {
    $application = include __DIR__ . '/../../../../vendor/bin/dr';
    $context['kernel.environment'] = 'testing';
    $context['kernel.allow_dumping'] = FALSE;
    $application = $application($context);
    $application->setAutoExit(FALSE);
    return new ApplicationTester($application);
  }

}