From 87cded8723fc5c808db800b611a91a0b87694ba4 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Sat, 12 Dec 2015 01:02:55 +0000 Subject: [PATCH] Issue #2631478 by neclimdul: bootstrap.php loader can show as changed global state --- .../Drupal/KernelTests/KernelTestBase.php | 3 +- core/tests/bootstrap.php | 51 +++++++++++++------ 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index 6c35c2b6ac2f..a0ed6a67d2ed 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -235,8 +235,7 @@ protected function bootEnvironment() { $this->streamWrappers = array(); \Drupal::unsetContainer(); - // @see /core/tests/bootstrap.php - $this->classLoader = $GLOBALS['loader']; + $this->classLoader = require $this->root . '/autoload.php'; require_once $this->root . '/core/includes/bootstrap.inc'; diff --git a/core/tests/bootstrap.php b/core/tests/bootstrap.php index 626f4ba865ed..f1f786255cc0 100644 --- a/core/tests/bootstrap.php +++ b/core/tests/bootstrap.php @@ -87,22 +87,41 @@ function drupal_phpunit_get_extension_namespaces($dirs) { if (!defined('PHPUNIT_COMPOSER_INSTALL')) { define('PHPUNIT_COMPOSER_INSTALL', __DIR__ . '/../../autoload.php'); } -// Start with classes in known locations. -$loader = require __DIR__ . '/../../autoload.php'; -$loader->add('Drupal\\Tests', __DIR__); -$loader->add('Drupal\\KernelTests', __DIR__); - -if (!isset($GLOBALS['namespaces'])) { - // Scan for arbitrary extension namespaces from core and contrib. - $extension_roots = drupal_phpunit_contrib_extension_directory_roots(); - - $dirs = array_map('drupal_phpunit_find_extension_directories', $extension_roots); - $dirs = array_reduce($dirs, 'array_merge', array()); - $GLOBALS['namespaces'] = drupal_phpunit_get_extension_namespaces($dirs); -} -foreach ($GLOBALS['namespaces'] as $prefix => $paths) { - $loader->addPsr4($prefix, $paths); -} + +/** + * Populate class loader with additional namespaces for tests. + * + * We run this in a function to avoid setting the class loader to a global + * that can change. This change can cause unpredictable false positives for + * phpunit's global state change watcher. The class loader can be retrieved from + * composer at any time by requiring autoload.php. + */ +function drupal_phpunit_populate_class_loader() { + + /** @var \Composer\Autoload\ClassLoader $loader */ + $loader = require __DIR__ . '/../../autoload.php'; + + // Start with classes in known locations. + $loader->add('Drupal\\Tests', __DIR__); + $loader->add('Drupal\\KernelTests', __DIR__); + + if (!isset($GLOBALS['namespaces'])) { + // Scan for arbitrary extension namespaces from core and contrib. + $extension_roots = drupal_phpunit_contrib_extension_directory_roots(); + + $dirs = array_map('drupal_phpunit_find_extension_directories', $extension_roots); + $dirs = array_reduce($dirs, 'array_merge', array()); + $GLOBALS['namespaces'] = drupal_phpunit_get_extension_namespaces($dirs); + } + foreach ($GLOBALS['namespaces'] as $prefix => $paths) { + $loader->addPsr4($prefix, $paths); + } + + return $loader; +}; + +// Do class loader population. +drupal_phpunit_populate_class_loader(); // Set sane locale settings, to ensure consistent string, dates, times and // numbers handling. -- GitLab