2020-03-31 11:29:22 +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\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
|
|
|
|
{
|
|
|
|
public function testSortingDoesNothingWithNoDependencies()
|
|
|
|
{
|
|
|
|
$packages[] = $this->createPackage('foo/bar1', array());
|
|
|
|
$packages[] = $this->createPackage('foo/bar2', array());
|
|
|
|
$packages[] = $this->createPackage('foo/bar3', array());
|
|
|
|
$packages[] = $this->createPackage('foo/bar4', array());
|
|
|
|
|
|
|
|
$sortedPackages = PackageSorter::sortPackages($packages);
|
|
|
|
|
|
|
|
self::assertSame($packages, $sortedPackages);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sortingOrdersDependenciesHigherThanPackageDataProvider()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'one package is dep' => array(
|
|
|
|
array(
|
|
|
|
$this->createPackage('foo/bar1', array('foo/bar4')),
|
|
|
|
$this->createPackage('foo/bar2', array('foo/bar4')),
|
|
|
|
$this->createPackage('foo/bar3', array('foo/bar4')),
|
|
|
|
$this->createPackage('foo/bar4', array()),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'foo/bar4',
|
|
|
|
'foo/bar1',
|
|
|
|
'foo/bar2',
|
|
|
|
'foo/bar3',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'one package has more deps' => array(
|
|
|
|
array(
|
|
|
|
$this->createPackage('foo/bar1', array('foo/bar2')),
|
|
|
|
$this->createPackage('foo/bar2', array('foo/bar4')),
|
|
|
|
$this->createPackage('foo/bar3', array('foo/bar4')),
|
|
|
|
$this->createPackage('foo/bar4', array()),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'foo/bar4',
|
|
|
|
'foo/bar2',
|
|
|
|
'foo/bar1',
|
|
|
|
'foo/bar3',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'package is required by many, but requires one other' => array(
|
|
|
|
array(
|
|
|
|
$this->createPackage('foo/bar1', array('foo/bar3')),
|
|
|
|
$this->createPackage('foo/bar2', array('foo/bar3')),
|
|
|
|
$this->createPackage('foo/bar3', array('foo/bar4')),
|
|
|
|
$this->createPackage('foo/bar4', array()),
|
|
|
|
$this->createPackage('foo/bar5', array('foo/bar3')),
|
|
|
|
$this->createPackage('foo/bar6', array('foo/bar3')),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'foo/bar4',
|
|
|
|
'foo/bar3',
|
|
|
|
'foo/bar1',
|
|
|
|
'foo/bar2',
|
|
|
|
'foo/bar5',
|
|
|
|
'foo/bar6',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'one package has many requires' => array(
|
|
|
|
array(
|
|
|
|
$this->createPackage('foo/bar1', array('foo/bar2')),
|
|
|
|
$this->createPackage('foo/bar2', array()),
|
|
|
|
$this->createPackage('foo/bar3', array('foo/bar4')),
|
|
|
|
$this->createPackage('foo/bar4', array()),
|
|
|
|
$this->createPackage('foo/bar5', array('foo/bar2')),
|
|
|
|
$this->createPackage('foo/bar6', array('foo/bar2')),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'foo/bar2',
|
|
|
|
'foo/bar4',
|
|
|
|
'foo/bar1',
|
|
|
|
'foo/bar3',
|
|
|
|
'foo/bar5',
|
|
|
|
'foo/bar6',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider sortingOrdersDependenciesHigherThanPackageDataProvider
|
2021-10-27 14:18:46 +00:00
|
|
|
*
|
|
|
|
* @param Package[] $packages
|
|
|
|
* @param string[] $expectedOrderedList
|
2020-03-31 11:29:22 +00:00
|
|
|
*/
|
|
|
|
public function testSortingOrdersDependenciesHigherThanPackage($packages, $expectedOrderedList)
|
|
|
|
{
|
|
|
|
$sortedPackages = PackageSorter::sortPackages($packages);
|
2020-11-22 13:48:56 +00:00
|
|
|
$sortedPackageNames = array_map(function ($package) {
|
|
|
|
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 $name
|
|
|
|
* @param string[] $requires
|
|
|
|
*
|
|
|
|
* @return Package
|
|
|
|
*/
|
2020-03-31 11:29:22 +00:00
|
|
|
private function createPackage($name, $requires)
|
|
|
|
{
|
|
|
|
$package = new Package($name, '1.0.0.0', '1.0.0');
|
|
|
|
|
|
|
|
$links = array();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|