1
0
Fork 0
composer/tests/Composer/Test/DependencyResolver/PoolBuilderTest.php

242 lines
8.8 KiB
PHP
Raw Normal View History

<?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;
use Composer\Repository\FilterRepository;
use Composer\Repository\LockArrayRepository;
use Composer\DependencyResolver\Request;
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\RepositorySet;
use Composer\Test\TestCase;
class PoolBuilderTest extends TestCase
{
/**
* @dataProvider getIntegrationTests
* @param string $file
* @param string $message
* @param mixed[] $expect
* @param mixed[] $root
* @param mixed[] $requestData
* @param mixed[] $packageRepos
* @param mixed[] $fixed
*/
public function testPoolBuilder($file, $message, $expect, $root, $requestData, $packageRepos, $fixed)
{
$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();
$rootReferences = !empty($root['references']) ? $root['references'] : array();
$stabilityFlags = array_map(function ($stability) {
return BasePackage::$stabilities[$stability];
}, $stabilityFlags);
$parser = new VersionParser();
foreach ($rootAliases as $index => $alias) {
$rootAliases[$index]['version'] = $parser->normalize($alias['version']);
$rootAliases[$index]['alias_normalized'] = $parser->normalize($alias['alias']);
}
$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;
};
$repositorySet = new RepositorySet($minimumStability, $stabilityFlags, $rootAliases, $rootReferences);
foreach ($packageRepos as $packages) {
$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) {
$repo->addPackage($loadPackage($package));
}
}
$repositorySet->addRepository($lockedRepo = new LockArrayRepository());
if (isset($requestData['locked'])) {
foreach ($requestData['locked'] as $package) {
$lockedRepo->addPackage($loadPackage($package));
}
}
$request = new Request($lockedRepo);
foreach ($requestData['require'] as $package => $constraint) {
$request->requireName($package, $parser->parseConstraints($constraint));
}
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);
}
foreach ($fixed as $fixedPackage) {
$request->fixPackage($loadPackage($fixedPackage));
}
$pool = $repositorySet->createPool($request, new NullIO());
2020-04-07 12:40:51 +00:00
$result = array();
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 = '';
if ($package->getSourceReference()) {
$suffix = '#'.$package->getSourceReference();
}
2020-06-06 13:19:55 +00:00
if ($package->getRepository() instanceof LockArrayRepository) {
$suffix .= ' (locked)';
2020-06-06 13:19:55 +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().')';
}
return (string) $package->getName().'-'.$package->getVersion() . $suffix;
}, $result);
$this->assertSame($expect, $result);
}
/**
* @return array<string, array<string>>
*/
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();
$packageRepos = JsonFile::parseJson($testData['PACKAGE-REPOS']);
$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)));
}
$tests[basename($file)] = array(str_replace($fixturesDir.'/', '', $file), $message, $expect, $root, $request, $packageRepos, $fixed);
}
return $tests;
}
/**
* @param \SplFileInfo $file
* @param string $fixturesDir
* @return array<string, string>
*/
protected function readTestFile(\SplFileInfo $file, $fixturesDir)
{
PHP 8.1: fix deprecation warnings about incorrect default values (#10036) * PHP 8.1/Tests: fix some deprecation warnings The default value for the `preg_split()` `$limit` parameter is `-1`, not `null`. Fixes numerous `preg_split(): Passing null to parameter #3 ($limit) of type int is deprecated` notices when running the test suite. Ref: https://www.php.net/manual/en/function.preg-split.php * PHP 8.1/NoProxyPattern: fix deprecation warning The default value for the `preg_split()` `$limit` parameter is `-1`, not `null`. Fixes some `preg_split(): Passing null to parameter #3 ($limit) of type int is deprecated` notices when running the test suite. ``` Deprecation triggered by Composer\Test\Util\Http\ProxyManagerTest::testGetProxyForRequest: preg_split(): Passing null to parameter #3 ($limit) of type int is deprecated Stack trace: 0 [internal function]: Symfony\Bridge\PhpUnit\DeprecationErrorHandler->handleError(8192, '...', '...', 42) 1 src/Composer/Util/NoProxyPattern.php(42): preg_split('...', '...', NULL, 1) 2 src/Composer/Util/Http/ProxyManager.php(148): Composer\Util\NoProxyPattern->__construct('...') 3 src/Composer/Util/Http/ProxyManager.php(50): Composer\Util\Http\ProxyManager->initProxyData() 4 src/Composer/Util/Http/ProxyManager.php(59): Composer\Util\Http\ProxyManager->__construct() 5 tests/Composer/Test/Util/Http/ProxyManagerTest.php(75): Composer\Util\Http\ProxyManager::getInstance() ... ``` Ref: https://www.php.net/manual/en/function.preg-split.php * PHP 8.1: fix deprecation warnings / http_build_query() This fixes all relevant calls to the PHP native `http_build_query()` function. The second parameter of which is the _optional_ `$numeric_prefix` parameter which expects a `string`. A parameter being optional, however, does not automatically make it nullable. As of PHP 8.1, passing `null` to a non-nullable PHP native function will generate a deprecation notice. In this case, these function calls yielded a `http_build_query(): Passing null to parameter #2 ($numeric_prefix) of type string is deprecated` notice. Changing the `null` to an empty string fixes this without BC-break. Fixes a few deprecation warnings found when running the tests. Refs: * https://www.php.net/manual/en/function.http-build-query.php * https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg * PHP 8.1: fix deprecation notices / PharData::__construct() This fixes all relevant calls to the PHP native `PharData::__construct()` method. The second parameter of this method is the _optional_ `$flags` parameter which expects an `int` of flags to be passed to the `Phar` parent class `RecursiveDirectoryIterator`. Fixed by passing the default value for the `$flags` parameter as per the `RecursiveDirectoryIterator::__construct()` method. The third parameter of the method is the _optional_ `$alias` parameter which expects an `string`. Fixed by passing an empty string. Fixes various notices along the lines of: ``` Deprecation triggered by Composer\Test\Package\Archiver\ArchiveManagerTest::testArchiveTar: PharData::__construct(): Passing null to parameter #2 ($flags) of type int is deprecated Stack trace: 0 [internal function]: Symfony\Bridge\PhpUnit\DeprecationErrorHandler->handleError(8192, '...', '...', 55) 1 src/Composer/Package/Archiver/PharArchiver.php(55): PharData->__construct('...', NULL, NULL, 2) 2 src/Composer/Package/Archiver/ArchiveManager.php(193): Composer\Package\Archiver\PharArchiver->archive('...', '...', '...', Array, false) 3 tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php(65): Composer\Package\Archiver\ArchiveManager->archive(Object(Composer\Package\CompletePackage), '...', '...') ... ``` Refs: * https://www.php.net/manual/en/phardata.construct.php * https://www.php.net/manual/en/recursivedirectoryiterator.construct.php Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-08-11 11:05:45 +00:00
$tokens = preg_split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file->getRealPath()), -1, PREG_SPLIT_DELIM_CAPTURE);
$sectionInfo = array(
'TEST' => true,
'ROOT' => false,
'REQUEST' => true,
'FIXED' => false,
'PACKAGE-REPOS' => true,
'EXPECT' => true,
);
$section = null;
2020-04-07 12:40:51 +00:00
$data = array();
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;
}
}