trait ComposerInstallersTrait

A utility for kernel tests that need to use 'composer/installers'.

@internal

Hierarchy

3 files declare their use of ComposerInstallersTrait
EnabledExtensionsValidatorTest.php in core/modules/package_manager/tests/src/Kernel/EnabledExtensionsValidatorTest.php
GitExcluderTest.php in core/modules/package_manager/tests/src/Kernel/PathExcluder/GitExcluderTest.php
OverwriteExistingPackagesValidatorTest.php in core/modules/package_manager/tests/src/Kernel/OverwriteExistingPackagesValidatorTest.php

File

core/modules/package_manager/tests/src/Traits/ComposerInstallersTrait.php, line 17

Namespace

Drupal\Tests\package_manager\Traits
View source
trait ComposerInstallersTrait {
    
    /**
     * Installs the composer/installers package.
     *
     * @param string $dir
     *   The fixture directory to install into.
     */
    private function installComposerInstallers(string $dir) : void {
        $package_name = 'composer/installers';
        $this->assertTrue(InstalledVersions::isInstalled($package_name));
        $repository = json_encode([
            'type' => 'path',
            'url' => InstalledVersions::getInstallPath($package_name),
            'options' => [
                'symlink' => FALSE,
                'versions' => [
                    // Explicitly state the version contained by this path repository,
                    // otherwise Composer will infer the version based on the git clone or
                    // fall back to `dev-master`.
                    // @see https://getcomposer.org/doc/05-repositories.md#path
'composer/installers' => InstalledVersions::getVersion($package_name),
                ],
            ],
        ], JSON_UNESCAPED_SLASHES);
        $working_dir_option = "--working-dir={$dir}";
        (new Process([
            'composer',
            'config',
            'repo.composer-installers-real',
            $repository,
            $working_dir_option,
        ]))->mustRun();
        (new FixtureManipulator())->addConfig([
            'allow-plugins.composer/installers' => TRUE,
        ])
            ->commitChanges($dir);
        (new Process([
            'composer',
            'require',
            'composer/installers:@dev',
            $working_dir_option,
        ]))->mustRun();
        // Use the default installer paths for Drupal core and extensions.
        $this->setInstallerPaths([], $dir);
    }
    
    /**
     * Sets the installer paths config.
     *
     * @param array $installer_paths
     *   The installed paths.
     * @param string $directory
     *   The fixture directory.
     */
    private function setInstallerPaths(array $installer_paths, string $directory) : void {
        // Respect any existing installer paths.
        $extra = $this->container
            ->get(ComposerInspector::class)
            ->getConfig('extra', $directory . '/composer.json');
        $existing_installer_paths = json_decode($extra, TRUE, flags: JSON_THROW_ON_ERROR)['installer-paths'] ?? [];
        (new FixtureManipulator())->addConfig([
            'extra.installer-paths' => $installer_paths + $existing_installer_paths,
        ])
            ->commitChanges($directory);
    }

}

Members

Title Sort descending Modifiers Object type Summary
ComposerInstallersTrait::installComposerInstallers private function Installs the composer/installers package.
ComposerInstallersTrait::setInstallerPaths private function Sets the installer paths config.

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