Skip to content
Snippets Groups Projects

Issue #3254166: Build tests should not modify dependencies' composer.json files

Files
3
<?php
namespace Drupal\Tests\automatic_updates\Build;
use Composer\Json\JsonFile;
use Drupal\BuildTests\QuickStart\QuickStartTestBase;
use Drupal\Composer\Composer;
/**
* Base class for tests which build a test site from a template project.
*/
abstract class TemplateProjectSiteTestBase extends QuickStartTestBase {
/**
* Returns path repository definitions for core's Composer packages.
*
* @param string $group
* The package group. Can be one of 'Template', 'Metapackage', or 'Plugin'.
*
* @return array[]
* A set of path repository definitions, keyed by package name.
*/
protected function getPathRepositories(string $group): array {
/** @var \SplFileInfo[] $files */
$files = Composer::composerSubprojectPaths($this->getDrupalRoot(), $group);
$repositories = [];
foreach ($files as $file) {
$json = new JsonFile($file->getPathname());
$data = $json->read();
$name = $data['name'];
$repositories[$name] = [
'type' => 'path',
'url' => $file->getPath(),
'options' => [
'symlink' => FALSE,
],
];
}
return $repositories;
}
/**
* Creates a Composer repository of all packages installed in vendor.
*
* @return \Composer\Json\JsonFile
* A wrapper around the repository file.
*/
protected function createVendorRepository(): JsonFile {
$file = new JsonFile($this->getWorkspaceDirectory() . '/vendor_packages.json');
// Build out package definitions for everything installed in the vendor
// directory.
$packages = [];
foreach ($this->getPackagesFromLockFile() as $package) {
$name = $package['name'];
$path = $this->getDrupalRoot() . "/vendor/$name";
// 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($path) && !is_link($path)) {
$version = $package['version'];
unset($package['source']);
$package['dist'] = [
'type' => 'path',
'url' => $path,
];
$packages[$name][$version] = $package;
}
}
$file->write(['packages' => $packages]);
return $file;
}
/**
* Reads all package information from the composer.lock file.
*
* @return mixed[][]
* All package information (including dev packages) from the lock file.
*/
private function getPackagesFromLockFile(): array {
$data = file_get_contents($this->getDrupalRoot() . '/composer.lock');
$data = JsonFile::parseJson($data);
return array_merge($data['packages'], $data['packages-dev']);
}
}
Loading