diff --git a/core/tests/Drupal/KernelTests/Setup/Commands/SetupDrupalTestScriptTest.php b/core/tests/Drupal/KernelTests/Setup/Commands/SetupDrupalTestScriptTest.php index 197e3477ea..0dcef01615 100644 --- a/core/tests/Drupal/KernelTests/Setup/Commands/SetupDrupalTestScriptTest.php +++ b/core/tests/Drupal/KernelTests/Setup/Commands/SetupDrupalTestScriptTest.php @@ -4,6 +4,7 @@ use Drupal\KernelTests\KernelTestBase; use Drupal\Setup\Commands\TestInstallationSetupApplication; +use Drupal\Setup\SetupDrupalTestScript; use GuzzleHttp\Client; use GuzzleHttp\Psr7\Request; use Symfony\Component\Console\Tester\ApplicationTester; @@ -39,7 +40,7 @@ public function testInstallScript() { $app_tester->run( [ 'command' => 'setup-drupal-test', - '--setup_file' => __DIR__ . '/../../../Setup/SetupDrupalTestScript.php', + '--setup_class' => SetupDrupalTestScript::class, ], [ 'interactive' => FALSE, @@ -54,7 +55,7 @@ public function testInstallScript() { $http_client = new Client(); $request = (new Request('GET', getenv('SIMPLETEST_BASE_URL') . '/test-page')) ->withHeader('User-Agent', trim($app_tester->getDisplay())); - + $response = $http_client->send($request); // Ensure the test_page_test module got installed. $this->assertContains('Test page | Drupal', (string) $response->getBody()); diff --git a/core/tests/Drupal/Setup/Commands/TestInstallationSetupCommand.php b/core/tests/Drupal/Setup/Commands/TestInstallationSetupCommand.php index 49fdafa1e2..8e81737a69 100644 --- a/core/tests/Drupal/Setup/Commands/TestInstallationSetupCommand.php +++ b/core/tests/Drupal/Setup/Commands/TestInstallationSetupCommand.php @@ -23,7 +23,7 @@ class TestInstallationSetupCommand extends Command { */ protected function configure() { $this->setName('setup-drupal-test') - ->addOption('setup_file', NULL, InputOption::VALUE_OPTIONAL) + ->addOption('setup_class', NULL, InputOption::VALUE_OPTIONAL) ->addOption('db_url', NULL, InputOption::VALUE_OPTIONAL, 'URL for database or SIMPLETEST_DB', getenv('SIMPLETEST_DB')) ->addOption('base_url', NULL, InputOption::VALUE_OPTIONAL, 'Base URL for site under test or SIMPLETEST_BASE_URL', getenv('SIMPLETEST_BASE_URL')); } @@ -41,7 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { // Manage site fixture. $test = new TestInstallationSetup(); - $test->setup('testing', $input->getOption('setup_file')); + $test->setup('testing', $input->getOption('setup_class')); $output->writeln(drupal_generate_test_ua($test->getDatabasePrefix())); } diff --git a/core/tests/Drupal/Setup/TestInstallationSetup.php b/core/tests/Drupal/Setup/TestInstallationSetup.php index f2fd50f0b2..c57ad52bc4 100644 --- a/core/tests/Drupal/Setup/TestInstallationSetup.php +++ b/core/tests/Drupal/Setup/TestInstallationSetup.php @@ -46,18 +46,18 @@ class TestInstallationSetup { * * @param string $profile * (optional) The installation profile to use. - * @param string $setup_file - * (optional) Setup file. A PHP file to setup configuration used by the + * @param string $setup_class + * (optional) Setup class. A PHP class to setup configuration used by the * test. */ - public function setup($profile = 'testing', $setup_file = NULL) { + public function setup($profile = 'testing', $setup_class = NULL) { $this->profile = $profile; $this->setupBaseUrl(); $this->prepareEnvironment(); $this->installDrupal(); - if ($setup_file) { - $this->executeSetupFile($setup_file); + if ($setup_class) { + $this->executeSetupClass($setup_class); } } @@ -87,18 +87,14 @@ protected function installDrupal() { /** * Uses the setup file to configure Drupal. * - * @param string $setup_file - * The setup file. + * @param string $class + * The setup class. */ - protected function executeSetupFile($setup_file) { - $class = static::fileGetPhpClass($setup_file); - - if (empty($class)) { - throw new \InvalidArgumentException(sprintf('You need to define a class implementing \Drupal\Setup\TestSetupInterface')); + protected function executeSetupClass($class) { + if (!class_exists($class)) { + throw new \InvalidArgumentException("There was a problem loading {$class}"); } - require_once $setup_file; - if (!is_subclass_of($class, TestSetupInterface::class)) { throw new \InvalidArgumentException(sprintf('You need to define a class implementing \Drupal\Setup\TestSetupInterface')); } @@ -108,60 +104,6 @@ protected function executeSetupFile($setup_file) { $instance->setup(); } - /** - * Gets the PHP classes contained in a php file. - * - * @param string $filepath - * The file path. - * - * @return string|null - * Returns a PHP class. - */ - protected static function fileGetPhpClass($filepath) { - $php_code = file_get_contents($filepath); - return static::extractClassesFromPhp($php_code); - } - - /** - * @param string $php_code - * PHP code to parse. - * - * @return string - * A PHP class. - */ - protected static function extractClassesFromPhp($php_code) { - $tokens = token_get_all($php_code); - - $class = $namespace = ''; - - for ($i = 0; $i < count($tokens); $i++) { - if ($tokens[$i][0] === T_NAMESPACE) { - for ($j = $i + 1; $j < count($tokens); $j++) { - if ($tokens[$j][0] === T_STRING) { - $namespace .= '\\' . $tokens[$j][1]; - } - else { - if ($tokens[$j] === '{' || $tokens[$j] === ';') { - break; - } - } - } - } - - if ($tokens[$i][0] === T_CLASS) { - for ($j = $i + 1; $j < count($tokens); $j++) { - if ($tokens[$j] === '{') { - $class = $tokens[$i + 2][1]; - } - } - } - } - - if ($namespace && $class) { - return $namespace . '\\' . $class; - } - } - /** * {@inheritdoc} */ diff --git a/core/tests/Drupal/Setup/TestSetupInterface.php b/core/tests/Drupal/Setup/TestSetupInterface.php index 89bc2bda11..fe992161dc 100644 --- a/core/tests/Drupal/Setup/TestSetupInterface.php +++ b/core/tests/Drupal/Setup/TestSetupInterface.php @@ -9,7 +9,7 @@ /** * Run code to setup the test. - * + * * You have access to any API provided by any installed module. To install * modules use * @code diff --git a/core/tests/bootstrap.php b/core/tests/bootstrap.php index afce1d1e2e..17dc860dc1 100644 --- a/core/tests/bootstrap.php +++ b/core/tests/bootstrap.php @@ -149,7 +149,7 @@ function drupal_phpunit_populate_class_loader() { }; // Do class loader population. -$this_is_the_drupal_test_bootstrap_autoloader_so_do_not_be_confused = drupal_phpunit_populate_class_loader(); +drupal_phpunit_populate_class_loader(); // Set sane locale settings, to ensure consistent string, dates, times and // numbers handling. @@ -184,5 +184,3 @@ class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase'); class_alias('\PHPUnit\Util\Test', '\PHPUnit_Util_Test'); class_alias('\PHPUnit\Util\XML', '\PHPUnit_Util_XML'); } - -return $this_is_the_drupal_test_bootstrap_autoloader_so_do_not_be_confused;