2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2020-03-31 11:29:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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\Util;
|
|
|
|
|
|
|
|
use Composer\Package\Link;
|
|
|
|
use Composer\Package\Package;
|
|
|
|
use Composer\Test\TestCase;
|
|
|
|
use Composer\Util\PackageSorter;
|
2020-06-05 14:41:37 +00:00
|
|
|
use Composer\Semver\Constraint\MatchAllConstraint;
|
2020-03-31 11:29:22 +00:00
|
|
|
|
|
|
|
class PackageSorterTest extends TestCase
|
|
|
|
{
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testSortingDoesNothingWithNoDependencies(): void
|
2020-03-31 11:29:22 +00:00
|
|
|
{
|
2022-11-24 13:39:08 +00:00
|
|
|
$packages[] = self::createPackage('foo/bar1', []);
|
|
|
|
$packages[] = self::createPackage('foo/bar2', []);
|
|
|
|
$packages[] = self::createPackage('foo/bar3', []);
|
|
|
|
$packages[] = self::createPackage('foo/bar4', []);
|
2020-03-31 11:29:22 +00:00
|
|
|
|
|
|
|
$sortedPackages = PackageSorter::sortPackages($packages);
|
|
|
|
|
|
|
|
self::assertSame($packages, $sortedPackages);
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function sortingOrdersDependenciesHigherThanPackageDataProvider(): array
|
2020-03-31 11:29:22 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
'one package is dep' => [
|
|
|
|
[
|
2022-11-24 13:39:08 +00:00
|
|
|
self::createPackage('foo/bar1', ['foo/bar4']),
|
|
|
|
self::createPackage('foo/bar2', ['foo/bar4']),
|
|
|
|
self::createPackage('foo/bar3', ['foo/bar4']),
|
|
|
|
self::createPackage('foo/bar4', []),
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2020-03-31 11:29:22 +00:00
|
|
|
'foo/bar4',
|
|
|
|
'foo/bar1',
|
|
|
|
'foo/bar2',
|
|
|
|
'foo/bar3',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'one package has more deps' => [
|
|
|
|
[
|
2022-11-24 13:39:08 +00:00
|
|
|
self::createPackage('foo/bar1', ['foo/bar2']),
|
|
|
|
self::createPackage('foo/bar2', ['foo/bar4']),
|
|
|
|
self::createPackage('foo/bar3', ['foo/bar4']),
|
|
|
|
self::createPackage('foo/bar4', []),
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2020-03-31 11:29:22 +00:00
|
|
|
'foo/bar4',
|
|
|
|
'foo/bar2',
|
|
|
|
'foo/bar1',
|
|
|
|
'foo/bar3',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'package is required by many, but requires one other' => [
|
|
|
|
[
|
2022-11-24 13:39:08 +00:00
|
|
|
self::createPackage('foo/bar1', ['foo/bar3']),
|
|
|
|
self::createPackage('foo/bar2', ['foo/bar3']),
|
|
|
|
self::createPackage('foo/bar3', ['foo/bar4']),
|
|
|
|
self::createPackage('foo/bar4', []),
|
|
|
|
self::createPackage('foo/bar5', ['foo/bar3']),
|
|
|
|
self::createPackage('foo/bar6', ['foo/bar3']),
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2020-03-31 11:29:22 +00:00
|
|
|
'foo/bar4',
|
|
|
|
'foo/bar3',
|
|
|
|
'foo/bar1',
|
|
|
|
'foo/bar2',
|
|
|
|
'foo/bar5',
|
|
|
|
'foo/bar6',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'one package has many requires' => [
|
|
|
|
[
|
2022-11-24 13:39:08 +00:00
|
|
|
self::createPackage('foo/bar1', ['foo/bar2']),
|
|
|
|
self::createPackage('foo/bar2', []),
|
|
|
|
self::createPackage('foo/bar3', ['foo/bar4']),
|
|
|
|
self::createPackage('foo/bar4', []),
|
|
|
|
self::createPackage('foo/bar5', ['foo/bar2']),
|
|
|
|
self::createPackage('foo/bar6', ['foo/bar2']),
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2020-03-31 11:29:22 +00:00
|
|
|
'foo/bar2',
|
|
|
|
'foo/bar4',
|
|
|
|
'foo/bar1',
|
|
|
|
'foo/bar3',
|
|
|
|
'foo/bar5',
|
|
|
|
'foo/bar6',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'circular deps sorted alphabetically if weighted equally' => [
|
|
|
|
[
|
2022-11-24 13:39:08 +00:00
|
|
|
self::createPackage('foo/bar1', ['circular/part1']),
|
|
|
|
self::createPackage('foo/bar2', ['circular/part2']),
|
|
|
|
self::createPackage('circular/part1', ['circular/part2']),
|
|
|
|
self::createPackage('circular/part2', ['circular/part1']),
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2022-03-15 11:02:03 +00:00
|
|
|
'circular/part1',
|
|
|
|
'circular/part2',
|
|
|
|
'foo/bar1',
|
|
|
|
'foo/bar2',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'equal weight sorted alphabetically' => [
|
|
|
|
[
|
2022-11-24 13:39:08 +00:00
|
|
|
self::createPackage('foo/bar10', ['foo/dep']),
|
|
|
|
self::createPackage('foo/bar2', ['foo/dep']),
|
|
|
|
self::createPackage('foo/baz', ['foo/dep']),
|
|
|
|
self::createPackage('foo/dep', []),
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2022-03-15 10:04:30 +00:00
|
|
|
'foo/dep',
|
|
|
|
'foo/bar2',
|
|
|
|
'foo/bar10',
|
|
|
|
'foo/baz',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
'pre-weighted packages bumped to top incl their deps' => [
|
|
|
|
[
|
2022-11-24 13:39:08 +00:00
|
|
|
self::createPackage('foo/bar', ['foo/dep']),
|
|
|
|
self::createPackage('foo/bar2', ['foo/dep2']),
|
|
|
|
self::createPackage('foo/dep', []),
|
|
|
|
self::createPackage('foo/dep2', []),
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2022-03-15 21:11:19 +00:00
|
|
|
'foo/dep',
|
|
|
|
'foo/bar',
|
|
|
|
'foo/dep2',
|
|
|
|
'foo/bar2',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'foo/bar' => -1000,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2020-03-31 11:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider sortingOrdersDependenciesHigherThanPackageDataProvider
|
2021-10-27 14:18:46 +00:00
|
|
|
*
|
|
|
|
* @param Package[] $packages
|
|
|
|
* @param string[] $expectedOrderedList
|
2022-03-15 21:11:19 +00:00
|
|
|
* @param array<string, int> $weights
|
2020-03-31 11:29:22 +00:00
|
|
|
*/
|
2022-03-15 21:20:21 +00:00
|
|
|
public function testSortingOrdersDependenciesHigherThanPackage(array $packages, array $expectedOrderedList, array $weights = []): void
|
2020-03-31 11:29:22 +00:00
|
|
|
{
|
2022-03-15 21:11:19 +00:00
|
|
|
$sortedPackages = PackageSorter::sortPackages($packages, $weights);
|
2022-08-17 12:20:07 +00:00
|
|
|
$sortedPackageNames = array_map(static function ($package): string {
|
2020-11-22 13:48:56 +00:00
|
|
|
return $package->getName();
|
|
|
|
}, $sortedPackages);
|
2020-03-31 11:29:22 +00:00
|
|
|
|
|
|
|
self::assertSame($expectedOrderedList, $sortedPackageNames);
|
|
|
|
}
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $requires
|
|
|
|
*/
|
2022-11-24 13:39:08 +00:00
|
|
|
private static function createPackage(string $name, array $requires): Package
|
2020-03-31 11:29:22 +00:00
|
|
|
{
|
|
|
|
$package = new Package($name, '1.0.0.0', '1.0.0');
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$links = [];
|
2020-03-31 11:29:22 +00:00
|
|
|
foreach ($requires as $requireName) {
|
2021-08-21 15:41:52 +00:00
|
|
|
$links[$requireName] = new Link($package->getName(), $requireName, new MatchAllConstraint);
|
2020-03-31 11:29:22 +00:00
|
|
|
}
|
|
|
|
$package->setRequires($links);
|
|
|
|
|
|
|
|
return $package;
|
|
|
|
}
|
|
|
|
}
|