1
0
Fork 0

Transaction: Define a total order on packages to keep behavior on PHP8

So far the ordering of alias packages with the same name was undefined
so the actual order was determined by implementation of the sorting
algorithm in PHP. As of PHP8 sort is stable by default which changes the
outcome in some of our test cases.

With the fully defined total order the order can longer change depending
on sorting algorithm used and remains the same across PHP versions.
pull/9285/head
Nils Adermann 2020-10-14 11:13:51 +02:00
parent 9ca7457698
commit 85eb007f4f
3 changed files with 8 additions and 4 deletions

View File

@ -61,8 +61,12 @@ class Transaction
{ {
$packageSort = function (PackageInterface $a, PackageInterface $b) { $packageSort = function (PackageInterface $a, PackageInterface $b) {
// sort alias packages by the same name behind their non alias version // sort alias packages by the same name behind their non alias version
if ($a->getName() == $b->getName() && $a instanceof AliasPackage != $b instanceof AliasPackage) { if ($a->getName() == $b->getName()) {
return $a instanceof AliasPackage ? -1 : 1; if ($a instanceof AliasPackage != $b instanceof AliasPackage) {
return $a instanceof AliasPackage ? -1 : 1;
}
// if names are the same, compare version, e.g. to sort aliases reliably, actual order does not matter
return strcmp($b->getVersion(), $a->getVersion());
} }
return strcmp($b->getName(), $a->getName()); return strcmp($b->getName(), $a->getName());
}; };

View File

@ -58,8 +58,8 @@ class TransactionTest extends TestCase
array('job' => 'update', 'from' => $packageB, 'to' => $packageBnew), array('job' => 'update', 'from' => $packageB, 'to' => $packageBnew),
array('job' => 'install', 'package' => $packageG), array('job' => 'install', 'package' => $packageG),
array('job' => 'install', 'package' => $packageF), array('job' => 'install', 'package' => $packageF),
array('job' => 'markAliasInstalled', 'package' => $packageFalias1),
array('job' => 'markAliasInstalled', 'package' => $packageFalias2), array('job' => 'markAliasInstalled', 'package' => $packageFalias2),
array('job' => 'markAliasInstalled', 'package' => $packageFalias1),
array('job' => 'install', 'package' => $packageD), array('job' => 'install', 'package' => $packageD),
); );

View File

@ -32,7 +32,7 @@ Installing double aliased package
install install
--EXPECT-- --EXPECT--
Installing b/b (dev-foo) Installing b/b (dev-foo)
Marking b/b (dev-master) as installed, alias of b/b (dev-foo)
Marking b/b (1.0.x-dev) as installed, alias of b/b (dev-foo) Marking b/b (1.0.x-dev) as installed, alias of b/b (dev-foo)
Marking b/b (dev-master) as installed, alias of b/b (dev-foo)
Installing a/a (dev-master) Installing a/a (dev-master)
Marking a/a (9999999-dev) as installed, alias of a/a (dev-master) Marking a/a (9999999-dev) as installed, alias of a/a (dev-master)