Skip to content

Commit 5433118

Browse files
committed
skip aliased packages
1 parent e8480dd commit 5433118

File tree

4 files changed

+107
-43
lines changed

4 files changed

+107
-43
lines changed

.github/workflows/test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ jobs:
5555
- name: Install Composer dependencies
5656
run: composer update -n --prefer-dist ${{ matrix.composer-flags }}
5757
- name: Run Tests
58+
if: ${{ matrix.php-versions == 8.0 && matrix.operating-system == 'ubuntu-latest' }}
5859
run: vendor/bin/simple-phpunit --coverage-clover coverage.xml
5960
- name: Upload coverage to Codecov
6061
uses: codecov/codecov-action@v1

src/Diff/DiffEntries.php

+2-27
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,10 @@
33
namespace IonBazan\ComposerDiff\Diff;
44

55
use ArrayIterator;
6-
use Countable;
7-
use IteratorAggregate;
86

97
/**
10-
* @implements IteratorAggregate<int, DiffEntry>
8+
* @extends ArrayIterator<int, DiffEntry>
119
*/
12-
class DiffEntries implements IteratorAggregate, Countable
10+
class DiffEntries extends ArrayIterator
1311
{
14-
/** @var DiffEntry[] */
15-
private $entries;
16-
17-
/**
18-
* @param DiffEntry[] $entries
19-
*/
20-
public function __construct(array $entries)
21-
{
22-
$this->entries = $entries;
23-
}
24-
25-
/**
26-
* @return ArrayIterator<int, DiffEntry>
27-
*/
28-
public function getIterator()
29-
{
30-
return new ArrayIterator($this->entries);
31-
}
32-
33-
public function count()
34-
{
35-
return \count($this->entries);
36-
}
3712
}

src/PackageDiff.php

+40-14
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
use Composer\DependencyResolver\Operation\OperationInterface;
77
use Composer\DependencyResolver\Operation\UninstallOperation;
88
use Composer\DependencyResolver\Operation\UpdateOperation;
9+
use Composer\Package\AliasPackage;
910
use Composer\Package\CompletePackage;
1011
use Composer\Package\Loader\ArrayLoader;
1112
use Composer\Repository\ArrayRepository;
13+
use Composer\Repository\RepositoryInterface;
1214
use IonBazan\ComposerDiff\Diff\DiffEntries;
1315
use IonBazan\ComposerDiff\Diff\DiffEntry;
1416

@@ -17,33 +19,41 @@ class PackageDiff
1719
const LOCKFILE = 'composer.lock';
1820

1921
/**
20-
* @param string $from
21-
* @param string $to
22-
* @param bool $dev
23-
* @param bool $withPlatform
24-
*
2522
* @return DiffEntries
2623
*/
27-
public function getPackageDiff($from, $to, $dev, $withPlatform)
24+
public function getDiff(RepositoryInterface $oldPackages, RepositoryInterface $targetPackages)
2825
{
29-
$oldPackages = $this->loadPackages($from, $dev, $withPlatform);
30-
$targetPackages = $this->loadPackages($to, $dev, $withPlatform);
31-
3226
$operations = array();
3327

3428
foreach ($targetPackages->getPackages() as $newPackage) {
35-
if ($oldPackage = $oldPackages->findPackage($newPackage->getName(), '*')) {
36-
if ($oldPackage->getFullPrettyVersion() !== $newPackage->getFullPrettyVersion()) {
37-
$operations[] = new UpdateOperation($oldPackage, $newPackage);
38-
}
29+
$matchingPackages = $oldPackages->findPackages($newPackage->getName());
30+
31+
if ($newPackage instanceof AliasPackage) {
32+
continue;
33+
}
34+
35+
if (0 === count($matchingPackages)) {
36+
$operations[] = new InstallOperation($newPackage);
3937

4038
continue;
4139
}
4240

43-
$operations[] = new InstallOperation($newPackage);
41+
foreach ($matchingPackages as $oldPackage) {
42+
if ($oldPackage instanceof AliasPackage) {
43+
continue;
44+
}
45+
46+
if ($oldPackage->getFullPrettyVersion() !== $newPackage->getFullPrettyVersion()) {
47+
$operations[] = new UpdateOperation($oldPackage, $newPackage);
48+
}
49+
}
4450
}
4551

4652
foreach ($oldPackages->getPackages() as $oldPackage) {
53+
if ($oldPackage instanceof AliasPackage) {
54+
continue;
55+
}
56+
4757
if (!$targetPackages->findPackage($oldPackage->getName(), '*')) {
4858
$operations[] = new UninstallOperation($oldPackage);
4959
}
@@ -54,6 +64,22 @@ public function getPackageDiff($from, $to, $dev, $withPlatform)
5464
}, $operations));
5565
}
5666

67+
/**
68+
* @param string $from
69+
* @param string $to
70+
* @param bool $dev
71+
* @param bool $withPlatform
72+
*
73+
* @return DiffEntries
74+
*/
75+
public function getPackageDiff($from, $to, $dev, $withPlatform)
76+
{
77+
return $this->getDiff(
78+
$this->loadPackages($from, $dev, $withPlatform),
79+
$this->loadPackages($to, $dev, $withPlatform)
80+
);
81+
}
82+
5783
/**
5884
* @param string $path
5985
* @param bool $dev

tests/PackageDiffTest.php

+64-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use Composer\DependencyResolver\Operation\InstallOperation;
66
use Composer\DependencyResolver\Operation\UninstallOperation;
77
use Composer\DependencyResolver\Operation\UpdateOperation;
8+
use Composer\Package\AliasPackage;
9+
use Composer\Package\Package;
10+
use Composer\Repository\ArrayRepository;
11+
use Composer\Repository\RepositoryInterface;
812
use IonBazan\ComposerDiff\Diff\DiffEntry;
913
use IonBazan\ComposerDiff\PackageDiff;
1014

@@ -27,7 +31,7 @@ public function testBasicUsage(array $expected, $dev, $withPlatform)
2731
$withPlatform
2832
);
2933

30-
$this->assertSame($expected, array_map(array($this, 'entryToString'), $operations->getIterator()->getArrayCopy()));
34+
$this->assertSame($expected, array_map(array($this, 'entryToString'), $operations->getArrayCopy()));
3135
}
3236

3337
public function testSameBaseAndTarget()
@@ -43,6 +47,19 @@ public function testSameBaseAndTarget()
4347
$this->assertEmpty($operations);
4448
}
4549

50+
/**
51+
* @param string[] $expected
52+
*
53+
* @dataProvider diffOperationsProvider
54+
*/
55+
public function testDiff(array $expected, RepositoryInterface $oldRepository, RepositoryInterface $newRepository)
56+
{
57+
$diff = new PackageDiff();
58+
$operations = $diff->getDiff($oldRepository, $newRepository);
59+
60+
$this->assertSame($expected, array_map(array($this, 'entryToString'), $operations->getArrayCopy()));
61+
}
62+
4663
/**
4764
* @param string[] $expected
4865
* @param bool $dev
@@ -56,7 +73,7 @@ public function testGitUsage(array $expected, $dev, $withPlatform)
5673
$this->prepareGit();
5774
$operations = $diff->getPackageDiff('HEAD', '', $dev, $withPlatform);
5875

59-
$this->assertSame($expected, array_map(array($this, 'entryToString'), $operations->getIterator()->getArrayCopy()));
76+
$this->assertSame($expected, array_map(array($this, 'entryToString'), $operations->getArrayCopy()));
6077
}
6178

6279
public function testInvalidGitRef()
@@ -67,6 +84,51 @@ public function testInvalidGitRef()
6784
$diff->getPackageDiff('invalid-ref', '', true, true);
6885
}
6986

87+
public function diffOperationsProvider()
88+
{
89+
return array(
90+
'update alias version' => array(
91+
array(),
92+
new ArrayRepository(array(
93+
new AliasPackage(new Package('vendor/package-a', '1.0', '1.0'), '1.0', '1.0'),
94+
)),
95+
new ArrayRepository(array(
96+
new AliasPackage(new Package('vendor/package-a', '1.0', '1.0'), '2.0', '2.0'),
97+
)),
98+
),
99+
'same alias version but different actual package version' => array(
100+
array(
101+
'update vendor/package-a from 1.0 to 2.0',
102+
),
103+
new ArrayRepository(array(
104+
new AliasPackage(new Package('vendor/package-a', '1.0', '1.0'), '1.0', '1.0'),
105+
)),
106+
new ArrayRepository(array(
107+
new AliasPackage(new Package('vendor/package-a', '2.0', '2.0'), '1.0', '1.0'),
108+
)),
109+
),
110+
'uninstall aliased package' => array(
111+
array(
112+
'uninstall vendor/package-a 1.0',
113+
),
114+
new ArrayRepository(array(
115+
new AliasPackage(new Package('vendor/package-a', '1.0', '1.0'), '2.0', '2.0'),
116+
)),
117+
new ArrayRepository(array(
118+
)),
119+
),
120+
'add aliased package' => array(
121+
array(
122+
'install vendor/package-a 1.0',
123+
),
124+
new ArrayRepository(array()),
125+
new ArrayRepository(array(
126+
new AliasPackage(new Package('vendor/package-a', '1.0', '1.0'), '2.0', '2.0'),
127+
)),
128+
),
129+
);
130+
}
131+
70132
public function operationsProvider()
71133
{
72134
return array(

0 commit comments

Comments
 (0)