2020-01-19 18:46:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\DependencyResolver;
|
|
|
|
|
|
|
|
use Composer\IO\NullIO;
|
|
|
|
use Composer\Repository\ArrayRepository;
|
2020-08-28 11:07:11 +00:00
|
|
|
use Composer\Repository\FilterRepository;
|
2020-01-19 18:46:16 +00:00
|
|
|
use Composer\Repository\LockArrayRepository;
|
|
|
|
use Composer\DependencyResolver\DefaultPolicy;
|
|
|
|
use Composer\DependencyResolver\Pool;
|
|
|
|
use Composer\DependencyResolver\PoolBuilder;
|
|
|
|
use Composer\DependencyResolver\Request;
|
|
|
|
use Composer\DependencyResolver\Solver;
|
|
|
|
use Composer\DependencyResolver\SolverProblemsException;
|
|
|
|
use Composer\Package\BasePackage;
|
|
|
|
use Composer\Package\AliasPackage;
|
|
|
|
use Composer\Json\JsonFile;
|
|
|
|
use Composer\Package\Loader\ArrayLoader;
|
|
|
|
use Composer\Package\Version\VersionParser;
|
|
|
|
use Composer\Repository\InstalledArrayRepository;
|
|
|
|
use Composer\Repository\RepositorySet;
|
|
|
|
use Composer\Test\TestCase;
|
|
|
|
use Composer\Semver\Constraint\MultiConstraint;
|
|
|
|
|
|
|
|
class PoolBuilderTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider getIntegrationTests
|
|
|
|
*/
|
2020-08-28 10:15:19 +00:00
|
|
|
public function testPoolBuilder($file, $message, $expect, $root, $requestData, $packageRepos, $fixed)
|
2020-01-19 18:46:16 +00:00
|
|
|
{
|
|
|
|
$rootAliases = !empty($root['aliases']) ? $root['aliases'] : array();
|
|
|
|
$minimumStability = !empty($root['minimum-stability']) ? $root['minimum-stability'] : 'stable';
|
|
|
|
$stabilityFlags = !empty($root['stability-flags']) ? $root['stability-flags'] : array();
|
2020-06-06 15:16:54 +00:00
|
|
|
$rootReferences = !empty($root['references']) ? $root['references'] : array();
|
2020-01-19 18:46:16 +00:00
|
|
|
$stabilityFlags = array_map(function ($stability) {
|
|
|
|
return BasePackage::$stabilities[$stability];
|
|
|
|
}, $stabilityFlags);
|
|
|
|
|
|
|
|
$parser = new VersionParser();
|
2020-04-08 10:20:18 +00:00
|
|
|
foreach ($rootAliases as $index => $alias) {
|
|
|
|
$rootAliases[$index]['version'] = $parser->normalize($alias['version']);
|
|
|
|
$rootAliases[$index]['alias_normalized'] = $parser->normalize($alias['alias']);
|
2020-01-19 18:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$loader = new ArrayLoader();
|
|
|
|
$packageIds = array();
|
|
|
|
$loadPackage = function ($data) use ($loader, &$packageIds) {
|
|
|
|
if (!empty($data['id'])) {
|
|
|
|
$id = $data['id'];
|
|
|
|
unset($data['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pkg = $loader->load($data);
|
|
|
|
|
|
|
|
if (!empty($id)) {
|
|
|
|
if (!empty($packageIds[$id])) {
|
|
|
|
throw new \LogicException('Duplicate package id '.$id.' defined');
|
|
|
|
}
|
|
|
|
$packageIds[$id] = $pkg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pkg;
|
|
|
|
};
|
|
|
|
|
2020-06-06 15:16:54 +00:00
|
|
|
$repositorySet = new RepositorySet($minimumStability, $stabilityFlags, $rootAliases, $rootReferences);
|
2020-08-28 10:15:19 +00:00
|
|
|
foreach ($packageRepos as $packages) {
|
2020-08-28 11:07:11 +00:00
|
|
|
$repo = new ArrayRepository();
|
|
|
|
if (isset($packages['canonical']) || isset($packages['only']) || isset($packages['exclude'])) {
|
|
|
|
$options = $packages;
|
|
|
|
$packages = $options['packages'];
|
|
|
|
unset($options['packages']);
|
|
|
|
$repositorySet->addRepository(new FilterRepository($repo, $options));
|
|
|
|
} else {
|
|
|
|
$repositorySet->addRepository($repo);
|
|
|
|
}
|
|
|
|
foreach ($packages as $package) {
|
2020-08-28 10:15:19 +00:00
|
|
|
$repo->addPackage($loadPackage($package));
|
|
|
|
}
|
2020-01-19 18:46:16 +00:00
|
|
|
}
|
2020-08-28 10:15:19 +00:00
|
|
|
$repositorySet->addRepository($lockedRepo = new LockArrayRepository());
|
2020-01-19 18:46:16 +00:00
|
|
|
|
2020-04-29 22:04:55 +00:00
|
|
|
if (isset($requestData['locked'])) {
|
|
|
|
foreach ($requestData['locked'] as $package) {
|
|
|
|
$lockedRepo->addPackage($loadPackage($package));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$request = new Request($lockedRepo);
|
|
|
|
foreach ($requestData['require'] as $package => $constraint) {
|
2020-01-19 22:28:00 +00:00
|
|
|
$request->requireName($package, $parser->parseConstraints($constraint));
|
2020-01-19 18:46:16 +00:00
|
|
|
}
|
2020-04-29 22:04:55 +00:00
|
|
|
if (isset($requestData['allowList'])) {
|
|
|
|
$transitiveDeps = Request::UPDATE_ONLY_LISTED;
|
|
|
|
if (isset($requestData['allowTransitiveDepsNoRootRequire']) && $requestData['allowTransitiveDepsNoRootRequire']) {
|
|
|
|
$transitiveDeps = Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS_NO_ROOT_REQUIRE;
|
|
|
|
}
|
|
|
|
if (isset($requestData['allowTransitiveDeps']) && $requestData['allowTransitiveDeps']) {
|
|
|
|
$transitiveDeps = Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS;
|
|
|
|
}
|
|
|
|
$request->setUpdateAllowList(array_flip($requestData['allowList']), $transitiveDeps);
|
|
|
|
}
|
2020-01-19 18:46:16 +00:00
|
|
|
|
|
|
|
foreach ($fixed as $fixedPackage) {
|
|
|
|
$request->fixPackage($loadPackage($fixedPackage));
|
|
|
|
}
|
|
|
|
|
2020-04-01 13:27:51 +00:00
|
|
|
$pool = $repositorySet->createPool($request, new NullIO());
|
2020-04-07 12:40:51 +00:00
|
|
|
$result = array();
|
2020-01-19 18:46:16 +00:00
|
|
|
for ($i = 1, $count = count($pool); $i <= $count; $i++) {
|
|
|
|
$result[] = $pool->packageById($i);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = array_map(function ($package) use ($packageIds) {
|
|
|
|
if ($id = array_search($package, $packageIds, true)) {
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
2020-06-06 13:19:55 +00:00
|
|
|
$suffix = '';
|
2020-06-06 15:16:54 +00:00
|
|
|
if ($package->getSourceReference()) {
|
|
|
|
$suffix = '#'.$package->getSourceReference();
|
|
|
|
}
|
2020-06-06 13:19:55 +00:00
|
|
|
if ($package->getRepository() instanceof LockArrayRepository) {
|
2020-06-06 15:16:54 +00:00
|
|
|
$suffix .= ' (locked)';
|
2020-06-06 13:19:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 15:16:54 +00:00
|
|
|
if ($package instanceof AliasPackage) {
|
|
|
|
if ($id = array_search($package->getAliasOf(), $packageIds, true)) {
|
|
|
|
return (string) $package->getName().'-'.$package->getVersion() . $suffix . ' (alias of '.$id . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
return (string) $package->getName().'-'.$package->getVersion() . $suffix . ' (alias of '.$package->getAliasOf()->getVersion().')';
|
2020-01-19 18:46:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 15:16:54 +00:00
|
|
|
return (string) $package->getName().'-'.$package->getVersion() . $suffix;
|
2020-01-19 18:46:16 +00:00
|
|
|
}, $result);
|
|
|
|
|
|
|
|
$this->assertSame($expect, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIntegrationTests()
|
|
|
|
{
|
|
|
|
$fixturesDir = realpath(__DIR__.'/Fixtures/poolbuilder/');
|
|
|
|
$tests = array();
|
|
|
|
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
|
|
|
|
if (!preg_match('/\.test$/', $file)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$testData = $this->readTestFile($file, $fixturesDir);
|
|
|
|
|
|
|
|
$message = $testData['TEST'];
|
|
|
|
|
|
|
|
$request = JsonFile::parseJson($testData['REQUEST']);
|
|
|
|
$root = !empty($testData['ROOT']) ? JsonFile::parseJson($testData['ROOT']) : array();
|
|
|
|
|
2020-08-28 10:15:19 +00:00
|
|
|
$packageRepos = JsonFile::parseJson($testData['PACKAGE-REPOS']);
|
2020-01-19 18:46:16 +00:00
|
|
|
$fixed = array();
|
|
|
|
if (!empty($testData['FIXED'])) {
|
|
|
|
$fixed = JsonFile::parseJson($testData['FIXED']);
|
|
|
|
}
|
|
|
|
$expect = JsonFile::parseJson($testData['EXPECT']);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
die(sprintf('Test "%s" is not valid: '.$e->getMessage(), str_replace($fixturesDir.'/', '', $file)));
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:15:19 +00:00
|
|
|
$tests[basename($file)] = array(str_replace($fixturesDir.'/', '', $file), $message, $expect, $root, $request, $packageRepos, $fixed);
|
2020-01-19 18:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function readTestFile(\SplFileInfo $file, $fixturesDir)
|
|
|
|
{
|
|
|
|
$tokens = preg_split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), null, PREG_SPLIT_DELIM_CAPTURE);
|
|
|
|
|
|
|
|
$sectionInfo = array(
|
|
|
|
'TEST' => true,
|
|
|
|
'ROOT' => false,
|
|
|
|
'REQUEST' => true,
|
|
|
|
'FIXED' => false,
|
2020-08-28 10:15:19 +00:00
|
|
|
'PACKAGE-REPOS' => true,
|
2020-01-19 18:46:16 +00:00
|
|
|
'EXPECT' => true,
|
|
|
|
);
|
|
|
|
|
|
|
|
$section = null;
|
2020-04-07 12:40:51 +00:00
|
|
|
$data = array();
|
2020-01-19 18:46:16 +00:00
|
|
|
foreach ($tokens as $i => $token) {
|
|
|
|
if (null === $section && empty($token)) {
|
|
|
|
continue; // skip leading blank
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $section) {
|
|
|
|
if (!isset($sectionInfo[$token])) {
|
|
|
|
throw new \RuntimeException(sprintf(
|
|
|
|
'The test file "%s" must not contain a section named "%s".',
|
|
|
|
str_replace($fixturesDir.'/', '', $file),
|
|
|
|
$token
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$section = $token;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sectionData = $token;
|
|
|
|
|
|
|
|
$data[$section] = $sectionData;
|
|
|
|
$section = $sectionData = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($sectionInfo as $section => $required) {
|
|
|
|
if ($required && !isset($data[$section])) {
|
|
|
|
throw new \RuntimeException(sprintf(
|
|
|
|
'The test file "%s" must have a section named "%s".',
|
|
|
|
str_replace($fixturesDir.'/', '', $file),
|
|
|
|
$section
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|