Commit 06d6ed53 authored by catch's avatar catch
Browse files

Issue #3293446 by alexpott, daffie: Create less static cache in...

Issue #3293446 by alexpott, daffie: Create less static cache in ExtensionDiscovery during KernelTests

(cherry picked from commit 605ec651)
parent 74467cb4
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -522,6 +522,10 @@ public static function ignoreTarget($key, $target) {
   *   The URL.
   * @param string $root
   *   The root directory of the Drupal installation.
   * @param bool|null $include_test_drivers
   *   (optional) Whether to include test extensions. If FALSE, all 'tests'
   *   directories are excluded in the search. When NULL will be determined by
   *   the extension_discovery_scan_tests setting.
   *
   * @return array
   *   The database connection info.
@@ -532,7 +536,7 @@ public static function ignoreTarget($key, $target) {
   * @throws \RuntimeException
   *   Exception thrown when a module provided database driver does not exist.
   */
  public static function convertDbUrlToConnectionInfo($url, $root) {
  public static function convertDbUrlToConnectionInfo($url, $root, ?bool $include_test_drivers = NULL) {
    // Check that the URL is well formed, starting with 'scheme://', where
    // 'scheme' is a database driver name.
    if (preg_match('/^(.*):\/\//', $url, $matches) !== 1) {
@@ -561,7 +565,7 @@ public static function convertDbUrlToConnectionInfo($url, $root) {
      // this method can be called before Drupal is installed and is never
      // called during regular runtime.
      $namespace = "Drupal\\$module\\Driver\\Database\\$driver";
      $psr4_base_directory = Database::findDriverAutoloadDirectory($namespace, $root, TRUE);
      $psr4_base_directory = Database::findDriverAutoloadDirectory($namespace, $root, $include_test_drivers);
      $additional_class_loader = new ClassLoader();
      $additional_class_loader->addPsr4($namespace . '\\', $psr4_base_directory);
      $additional_class_loader->register(TRUE);
@@ -633,6 +637,10 @@ public static function convertDbUrlToConnectionInfo($url, $root) {
   *   The database driver's namespace.
   * @param string $root
   *   The root directory of the Drupal installation.
   * @param bool|null $include_test_drivers
   *   (optional) Whether to include test extensions. If FALSE, all 'tests'
   *   directories are excluded in the search. When NULL will be determined by
   *   the extension_discovery_scan_tests setting.
   *
   * @return string|false
   *   The PSR-4 directory to add to the autoloader for the namespace if the
@@ -642,7 +650,7 @@ public static function convertDbUrlToConnectionInfo($url, $root) {
   * @throws \RuntimeException
   *   Exception thrown when a module provided database driver does not exist.
   */
  public static function findDriverAutoloadDirectory($namespace, $root) {
  public static function findDriverAutoloadDirectory($namespace, $root, ?bool $include_test_drivers = NULL) {
    // As explained by this method's documentation, return FALSE if the
    // namespace is not a sub-namespace of a Drupal module.
    if (!static::isWithinModuleNamespace($namespace)) {
@@ -655,7 +663,7 @@ public static function findDriverAutoloadDirectory($namespace, $root) {
    // The namespace is within a Drupal module. Find the directory where the
    // module is located.
    $extension_discovery = new ExtensionDiscovery($root, FALSE, []);
    $modules = $extension_discovery->scan('module');
    $modules = $extension_discovery->scan('module', $include_test_drivers);
    if (!isset($modules[$module])) {
      throw new \RuntimeException(sprintf("Cannot find the module '%s' for the database driver namespace '%s'", $module, $namespace));
    }
+1 −1
Original line number Diff line number Diff line
@@ -167,7 +167,7 @@ protected function changeDatabasePrefix() {
      // Ensure no existing database gets in the way. If a default database
      // exists already it must be removed.
      Database::removeConnection('default');
      $database = Database::convertDbUrlToConnectionInfo($db_url, $this->root ?? DRUPAL_ROOT);
      $database = Database::convertDbUrlToConnectionInfo($db_url, $this->root ?? DRUPAL_ROOT, TRUE);
      Database::addConnectionInfo('default', 'default', $database);
    }

+1 −1
Original line number Diff line number Diff line
@@ -607,7 +607,7 @@ function simpletest_script_setup_database($new = FALSE) {
    // Remove a possibly existing default connection (from settings.php).
    Database::removeConnection('default');
    try {
      $databases['default']['default'] = Database::convertDbUrlToConnectionInfo($args['dburl'], DRUPAL_ROOT);
      $databases['default']['default'] = Database::convertDbUrlToConnectionInfo($args['dburl'], DRUPAL_ROOT, TRUE);
    }
    catch (\InvalidArgumentException $e) {
      simpletest_script_print_error('Invalid --dburl. Reason: ' . $e->getMessage());
+1 −1
Original line number Diff line number Diff line
@@ -454,7 +454,7 @@ protected function getDatabaseConnectionInfo() {
      throw new \Exception('There is no database connection so no tests can be run. You must provide a SIMPLETEST_DB environment variable to run PHPUnit based functional tests outside of run-tests.sh. See https://www.drupal.org/node/2116263#skipped-tests for more information.');
    }
    else {
      $database = Database::convertDbUrlToConnectionInfo($db_url, $this->root);
      $database = Database::convertDbUrlToConnectionInfo($db_url, $this->root, TRUE);
      Database::addConnectionInfo('default', 'default', $database);
    }

+6 −10
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@

use Composer\Autoload\ClassLoader;
use Drupal\Core\Database\Database;
use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

@@ -57,10 +56,8 @@ protected function setUp(): void {
   * @covers ::findDriverAutoloadDirectory
   * @dataProvider providerFindDriverAutoloadDirectory
   */
  public function testFindDriverAutoloadDirectory($expected, $namespace) {
    new Settings(['extension_discovery_scan_tests' => TRUE]);
    // The only module that provides a driver in core is a test module.
    $this->assertSame($expected, Database::findDriverAutoloadDirectory($namespace, $this->root));
  public function testFindDriverAutoloadDirectory($expected, $namespace, $include_test_drivers) {
    $this->assertSame($expected, Database::findDriverAutoloadDirectory($namespace, $this->root, $include_test_drivers));
  }

  /**
@@ -70,9 +67,9 @@ public function testFindDriverAutoloadDirectory($expected, $namespace) {
   */
  public function providerFindDriverAutoloadDirectory() {
    return [
      'core mysql' => ['core/modules/mysql/src/Driver/Database/mysql/', 'Drupal\mysql\Driver\Database\mysql'],
      'D8 custom fake' => [FALSE, 'Drupal\Driver\Database\corefake'],
      'module mysql' => ['core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/', 'Drupal\driver_test\Driver\Database\DrivertestMysql'],
      'core mysql' => ['core/modules/mysql/src/Driver/Database/mysql/', 'Drupal\mysql\Driver\Database\mysql', FALSE],
      'D8 custom fake' => [FALSE, 'Drupal\Driver\Database\corefake', TRUE],
      'module mysql' => ['core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/', 'Drupal\driver_test\Driver\Database\DrivertestMysql', TRUE],
    ];
  }

@@ -81,10 +78,9 @@ public function providerFindDriverAutoloadDirectory() {
   * @dataProvider providerFindDriverAutoloadDirectoryException
   */
  public function testFindDriverAutoloadDirectoryException($expected_message, $namespace, $include_tests) {
    new Settings(['extension_discovery_scan_tests' => $include_tests]);
    $this->expectException(\RuntimeException::class);
    $this->expectExceptionMessage($expected_message);
    Database::findDriverAutoloadDirectory($namespace, $this->root);
    Database::findDriverAutoloadDirectory($namespace, $this->root, $include_tests);
  }

  /**
Loading