Skip to content

Commit

Permalink
Merge pull request #12 from IonBazan/bugfix/aliased-packages
Browse files Browse the repository at this point in the history
skip aliased packages
  • Loading branch information
IonBazan authored Nov 5, 2021
2 parents e8480dd + 5433118 commit 0bb9e24
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 43 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
- name: Install Composer dependencies
run: composer update -n --prefer-dist ${{ matrix.composer-flags }}
- name: Run Tests
if: ${{ matrix.php-versions == 8.0 && matrix.operating-system == 'ubuntu-latest' }}
run: vendor/bin/simple-phpunit --coverage-clover coverage.xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
Expand Down
29 changes: 2 additions & 27 deletions src/Diff/DiffEntries.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,10 @@
namespace IonBazan\ComposerDiff\Diff;

use ArrayIterator;
use Countable;
use IteratorAggregate;

/**
* @implements IteratorAggregate<int, DiffEntry>
* @extends ArrayIterator<int, DiffEntry>
*/
class DiffEntries implements IteratorAggregate, Countable
class DiffEntries extends ArrayIterator
{
/** @var DiffEntry[] */
private $entries;

/**
* @param DiffEntry[] $entries
*/
public function __construct(array $entries)
{
$this->entries = $entries;
}

/**
* @return ArrayIterator<int, DiffEntry>
*/
public function getIterator()
{
return new ArrayIterator($this->entries);
}

public function count()
{
return \count($this->entries);
}
}
54 changes: 40 additions & 14 deletions src/PackageDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
use Composer\DependencyResolver\Operation\OperationInterface;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\Package\AliasPackage;
use Composer\Package\CompletePackage;
use Composer\Package\Loader\ArrayLoader;
use Composer\Repository\ArrayRepository;
use Composer\Repository\RepositoryInterface;
use IonBazan\ComposerDiff\Diff\DiffEntries;
use IonBazan\ComposerDiff\Diff\DiffEntry;

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

/**
* @param string $from
* @param string $to
* @param bool $dev
* @param bool $withPlatform
*
* @return DiffEntries
*/
public function getPackageDiff($from, $to, $dev, $withPlatform)
public function getDiff(RepositoryInterface $oldPackages, RepositoryInterface $targetPackages)
{
$oldPackages = $this->loadPackages($from, $dev, $withPlatform);
$targetPackages = $this->loadPackages($to, $dev, $withPlatform);

$operations = array();

foreach ($targetPackages->getPackages() as $newPackage) {
if ($oldPackage = $oldPackages->findPackage($newPackage->getName(), '*')) {
if ($oldPackage->getFullPrettyVersion() !== $newPackage->getFullPrettyVersion()) {
$operations[] = new UpdateOperation($oldPackage, $newPackage);
}
$matchingPackages = $oldPackages->findPackages($newPackage->getName());

if ($newPackage instanceof AliasPackage) {
continue;
}

if (0 === count($matchingPackages)) {
$operations[] = new InstallOperation($newPackage);

continue;
}

$operations[] = new InstallOperation($newPackage);
foreach ($matchingPackages as $oldPackage) {
if ($oldPackage instanceof AliasPackage) {
continue;
}

if ($oldPackage->getFullPrettyVersion() !== $newPackage->getFullPrettyVersion()) {
$operations[] = new UpdateOperation($oldPackage, $newPackage);
}
}
}

foreach ($oldPackages->getPackages() as $oldPackage) {
if ($oldPackage instanceof AliasPackage) {
continue;
}

if (!$targetPackages->findPackage($oldPackage->getName(), '*')) {
$operations[] = new UninstallOperation($oldPackage);
}
Expand All @@ -54,6 +64,22 @@ public function getPackageDiff($from, $to, $dev, $withPlatform)
}, $operations));
}

/**
* @param string $from
* @param string $to
* @param bool $dev
* @param bool $withPlatform
*
* @return DiffEntries
*/
public function getPackageDiff($from, $to, $dev, $withPlatform)
{
return $this->getDiff(
$this->loadPackages($from, $dev, $withPlatform),
$this->loadPackages($to, $dev, $withPlatform)
);
}

/**
* @param string $path
* @param bool $dev
Expand Down
66 changes: 64 additions & 2 deletions tests/PackageDiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\Package\AliasPackage;
use Composer\Package\Package;
use Composer\Repository\ArrayRepository;
use Composer\Repository\RepositoryInterface;
use IonBazan\ComposerDiff\Diff\DiffEntry;
use IonBazan\ComposerDiff\PackageDiff;

Expand All @@ -27,7 +31,7 @@ public function testBasicUsage(array $expected, $dev, $withPlatform)
$withPlatform
);

$this->assertSame($expected, array_map(array($this, 'entryToString'), $operations->getIterator()->getArrayCopy()));
$this->assertSame($expected, array_map(array($this, 'entryToString'), $operations->getArrayCopy()));
}

public function testSameBaseAndTarget()
Expand All @@ -43,6 +47,19 @@ public function testSameBaseAndTarget()
$this->assertEmpty($operations);
}

/**
* @param string[] $expected
*
* @dataProvider diffOperationsProvider
*/
public function testDiff(array $expected, RepositoryInterface $oldRepository, RepositoryInterface $newRepository)
{
$diff = new PackageDiff();
$operations = $diff->getDiff($oldRepository, $newRepository);

$this->assertSame($expected, array_map(array($this, 'entryToString'), $operations->getArrayCopy()));
}

/**
* @param string[] $expected
* @param bool $dev
Expand All @@ -56,7 +73,7 @@ public function testGitUsage(array $expected, $dev, $withPlatform)
$this->prepareGit();
$operations = $diff->getPackageDiff('HEAD', '', $dev, $withPlatform);

$this->assertSame($expected, array_map(array($this, 'entryToString'), $operations->getIterator()->getArrayCopy()));
$this->assertSame($expected, array_map(array($this, 'entryToString'), $operations->getArrayCopy()));
}

public function testInvalidGitRef()
Expand All @@ -67,6 +84,51 @@ public function testInvalidGitRef()
$diff->getPackageDiff('invalid-ref', '', true, true);
}

public function diffOperationsProvider()
{
return array(
'update alias version' => array(
array(),
new ArrayRepository(array(
new AliasPackage(new Package('vendor/package-a', '1.0', '1.0'), '1.0', '1.0'),
)),
new ArrayRepository(array(
new AliasPackage(new Package('vendor/package-a', '1.0', '1.0'), '2.0', '2.0'),
)),
),
'same alias version but different actual package version' => array(
array(
'update vendor/package-a from 1.0 to 2.0',
),
new ArrayRepository(array(
new AliasPackage(new Package('vendor/package-a', '1.0', '1.0'), '1.0', '1.0'),
)),
new ArrayRepository(array(
new AliasPackage(new Package('vendor/package-a', '2.0', '2.0'), '1.0', '1.0'),
)),
),
'uninstall aliased package' => array(
array(
'uninstall vendor/package-a 1.0',
),
new ArrayRepository(array(
new AliasPackage(new Package('vendor/package-a', '1.0', '1.0'), '2.0', '2.0'),
)),
new ArrayRepository(array(
)),
),
'add aliased package' => array(
array(
'install vendor/package-a 1.0',
),
new ArrayRepository(array()),
new ArrayRepository(array(
new AliasPackage(new Package('vendor/package-a', '1.0', '1.0'), '2.0', '2.0'),
)),
),
);
}

public function operationsProvider()
{
return array(
Expand Down

0 comments on commit 0bb9e24

Please sign in to comment.