class LegacyScriptsTest

@group Composer @group legacy @requires externalCommand composer @coversDefaultClass \Drupal\Core\Composer\Composer

Hierarchy

Expanded class hierarchy of LegacyScriptsTest

File

core/tests/Drupal/BuildTests/Composer/LegacyScriptsTest.php, line 15

Namespace

Drupal\BuildTests\Composer
View source
class LegacyScriptsTest extends BuildTestBase {
    
    /**
     * @covers ::vendorTestCodeCleanup
     */
    public function testVendorTestCodeCleanup() {
        $package_dir = 'composer/Template/RecommendedProject';
        // Create a "Composer"-type repository containing one entry for every
        // package in the vendor directory.
        $vendor_packages_path = $this->getWorkspaceDirectory() . '/vendor_packages/packages.json';
        $this->makeVendorPackage($vendor_packages_path);
        // Make a copy of the code to alter in the workspace directory.
        $this->copyCodebase();
        // Remove the packages.drupal.org entry (and any other custom repository)
        // from the site under test's repositories section. There is no way to do
        // this via `composer config --unset`, so we read and rewrite composer.json.
        $composer_json_path = $this->getWorkspaceDirectory() . "/{$package_dir}/composer.json";
        $composer_json = json_decode(file_get_contents($composer_json_path), TRUE);
        unset($composer_json['repositories']);
        $json = json_encode($composer_json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
        file_put_contents($composer_json_path, $json);
        // Set up the template to use our path repos. Inclusion of metapackages is
        // reported differently, so we load up a separate set for them.
        $metapackage_path_repos = $this->getPathReposForType($this->getWorkspaceDirectory(), 'Metapackage');
        $this->assertArrayHasKey('drupal/core-recommended', $metapackage_path_repos);
        $path_repos = array_merge($metapackage_path_repos, $this->getPathReposForType($this->getWorkspaceDirectory(), 'Plugin'));
        // Always add drupal/core as a path repo.
        $path_repos['drupal/core'] = $this->getWorkspaceDirectory() . '/core';
        foreach ($path_repos as $name => $path) {
            $this->executeCommand("composer config --no-interaction repositories.{$name} path {$path}", $package_dir);
            $this->assertCommandSuccessful();
        }
        // Add our vendor package repository to our site under test's repositories
        // section. Call it "local" (although the name does not matter).
        $this->executeCommand("composer config --no-interaction repositories.local composer file://" . $vendor_packages_path, $package_dir);
        $this->assertCommandSuccessful();
        // Add the vendorTestCodeCleanup script as a post-install command.
        $this->executeCommand('composer config scripts.post-package-install "Drupal\\Core\\Composer\\Composer::vendorTestCodeCleanup"');
        $this->assertCommandSuccessful();
        // Attempt to install packages which will trigger the script.
        $this->executeCommand('composer install');
        $this->assertCommandSuccessful();
    }
    
    /**
     * Get Composer items that we want to be path repos, from within a directory.
     *
     * @param string $workspace_directory
     *   The full path to the workspace directory.
     * @param string $subdir
     *   The subdirectory to search under composer/.
     *
     * @return string[]
     *   Array of paths, indexed by package name.
     */
    public function getPathReposForType($workspace_directory, $subdir) {
        // Find the Composer items that we want to be path repos.
        
        /** @var \SplFileInfo[] $path_repos */
        $path_repos = Composer::composerSubprojectPaths($workspace_directory, $subdir);
        $data = [];
        foreach ($path_repos as $path_repo) {
            $json_file = new JsonFile($path_repo->getPathname());
            $json = $json_file->read();
            $data[$json['name']] = $path_repo->getPath();
        }
        return $data;
    }
    
    /**
     * Creates a test package that points to all the projects in vendor.
     *
     * @param string $repository_path
     *   The path where to create the test package.
     */
    protected function makeVendorPackage($repository_path) {
        $root = $this->getDrupalRoot();
        $process = $this->executeCommand("composer --working-dir={$root} info --format=json");
        $this->assertCommandSuccessful();
        $installed = json_decode($process->getOutput(), TRUE);
        // Build out package definitions for everything installed in
        // the vendor directory.
        $packages = [];
        foreach ($installed['installed'] as $project) {
            $name = $project['name'];
            $version = $project['version'];
            $path = "vendor/{$name}";
            $full_path = "{$root}/{$path}";
            // We are building a set of path repositories to projects in the vendor
            // directory, so we will skip any project that does not exist in vendor.
            // Also skip the projects that are symlinked in vendor. These are in our
            // metapackage. They will be represented as path repositories in the test
            // project's composer.json.
            if (is_dir($full_path) && !is_link($full_path)) {
                $packages['packages'][$name] = [
                    $version => [
                        "name" => $name,
                        "dist" => [
                            "type" => "path",
                            "url" => $full_path,
                        ],
                        "version" => $version,
                    ],
                ];
            }
        }
        $json = json_encode($packages, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
        mkdir(dirname($repository_path));
        file_put_contents($repository_path, $json);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
BuildTestBase::$commandProcess private property The most recent command process.
BuildTestBase::$destroyBuild protected property Default to destroying build artifacts after a test finishes.
BuildTestBase::$hostName private static property Our native host name, used by PHP when it starts up the server.
BuildTestBase::$hostPort private property Port that will be tested.
BuildTestBase::$mink private property The Mink session manager.
BuildTestBase::$phpFinder private property The PHP executable finder.
BuildTestBase::$portLocks private property A list of ports used by the test.
BuildTestBase::$serverDocroot private property The docroot for the server process.
BuildTestBase::$serverProcess private property The process that's running the HTTP server.
BuildTestBase::$workspaceDir private property The working directory where this test will manipulate files.
BuildTestBase::assertCommandExitCode public function Asserts that the last command returned the specified exit code.
BuildTestBase::assertCommandOutputContains public function Assert that text is present in the output of the most recent command.
BuildTestBase::assertCommandSuccessful public function Asserts that the last command ran without error.
BuildTestBase::assertDrupalVisit public function Helper function to assert that the last visit was a Drupal site.
BuildTestBase::assertErrorOutputContains public function Assert that text is present in the error output of the most recent command.
BuildTestBase::assertErrorOutputNotContains public function Assert text is not present in the error output of the most recent command.
BuildTestBase::checkPortIsAvailable protected function Checks whether a port is available.
BuildTestBase::copyCodebase public function Copy the current working codebase into a workspace.
BuildTestBase::executeCommand public function Run a command.
BuildTestBase::findAvailablePort protected function Discover an available port number.
BuildTestBase::getCodebaseFinder public function Get a default Finder object for a Drupal codebase.
BuildTestBase::getDrupalRoot protected function Get the root path of this Drupal codebase.
BuildTestBase::getMink public function Get the Mink instance.
BuildTestBase::getPortNumber protected function Get the port number for requests.
BuildTestBase::getWorkingPath protected function Get the working directory within the workspace, creating if necessary.
BuildTestBase::getWorkspaceDirectory public function Full path to the workspace where this test can build.
BuildTestBase::initMink protected function Set up the Mink session manager.
BuildTestBase::instantiateServer protected function Do the work of making a server process.
BuildTestBase::setUp protected function 2
BuildTestBase::setUpBeforeClass public static function
BuildTestBase::standUpServer protected function Makes a local test server using PHP's internal HTTP server.
BuildTestBase::stopServer protected function Stop the HTTP server, zero out all necessary variables.
BuildTestBase::tearDown protected function 1
BuildTestBase::visit public function Visit a URI on the HTTP server.
ExternalCommandRequirementsTrait::$existingCommands private static property A list of existing external commands we've already discovered.
ExternalCommandRequirementsTrait::checkClassCommandRequirements private static function Checks whether required external commands are available per test class.
ExternalCommandRequirementsTrait::checkExternalCommandRequirements private static function Checks missing external command requirements.
ExternalCommandRequirementsTrait::checkMethodCommandRequirements private static function Checks whether required external commands are available per method.
ExternalCommandRequirementsTrait::externalCommandIsAvailable private static function Determine if an external command is available. 3
LegacyScriptsTest::getPathReposForType public function Get Composer items that we want to be path repos, from within a directory.
LegacyScriptsTest::makeVendorPackage protected function Creates a test package that points to all the projects in vendor.
LegacyScriptsTest::testVendorTestCodeCleanup public function @covers ::vendorTestCodeCleanup
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.