From 767279b41ac88a72d138e27b7088f0bc21b42976 Mon Sep 17 00:00:00 2001 From: Malte Wunsch Date: Mon, 3 Aug 2015 18:28:04 +0200 Subject: [PATCH 1/2] getCanonicalPackages returns packages with same name in different versions --- src/Composer/Repository/ArrayRepository.php | 1 + .../Repository/WritableArrayRepository.php | 7 +++-- .../WritableArrayRepositoryTest.php | 29 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/Composer/Test/Repository/WritableArrayRepositoryTest.php diff --git a/src/Composer/Repository/ArrayRepository.php b/src/Composer/Repository/ArrayRepository.php index 4f6e2daa7..a5023c1fe 100644 --- a/src/Composer/Repository/ArrayRepository.php +++ b/src/Composer/Repository/ArrayRepository.php @@ -26,6 +26,7 @@ use Composer\Package\LinkConstraint\VersionConstraint; */ class ArrayRepository implements RepositoryInterface { + /** @var PackageInterface[] */ protected $packages; public function __construct(array $packages = array()) diff --git a/src/Composer/Repository/WritableArrayRepository.php b/src/Composer/Repository/WritableArrayRepository.php index 756f24137..6b47e7e47 100644 --- a/src/Composer/Repository/WritableArrayRepository.php +++ b/src/Composer/Repository/WritableArrayRepository.php @@ -42,11 +42,12 @@ class WritableArrayRepository extends ArrayRepository implements WritableReposit { $packages = $this->getPackages(); - // get at most one package of each name, prefering non-aliased ones + // get at most one package of each (name, version) combination, prefering non-aliased ones $packagesByName = array(); foreach ($packages as $package) { - if (!isset($packagesByName[$package->getName()]) || $packagesByName[$package->getName()] instanceof AliasPackage) { - $packagesByName[$package->getName()] = $package; + $index = $package->getName() . $package->getVersion(); + if (!isset($packagesByName[$index]) || $packagesByName[$index] instanceof AliasPackage) { + $packagesByName[$index] = $package; } } diff --git a/tests/Composer/Test/Repository/WritableArrayRepositoryTest.php b/tests/Composer/Test/Repository/WritableArrayRepositoryTest.php new file mode 100644 index 000000000..e4cb36ccc --- /dev/null +++ b/tests/Composer/Test/Repository/WritableArrayRepositoryTest.php @@ -0,0 +1,29 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Repository; + +use Composer\Repository\WritableArrayRepository; +use Composer\TestCase; + +final class WritableArrayRepositoryTest extends TestCase +{ + public function testGetCanonicalPackagesReturnsDifferentVersionsOfSameNamedPackage() + { + $repository = new WritableArrayRepository(); + + $repository->addPackage($this->getPackage('foo', 1)); + $repository->addPackage($this->getPackage('foo', 2)); + + $this->assertCount(2, $repository->getCanonicalPackages()); + } +} From 70ff00c9de4c7bd52c7078f5d53f1bfd4a1f6a9d Mon Sep 17 00:00:00 2001 From: Malte Wunsch Date: Tue, 4 Aug 2015 09:32:23 +0200 Subject: [PATCH 2/2] Reverted inadvertent API change, improved API documentation --- .../Repository/WritableArrayRepository.php | 7 ++--- .../WritableRepositoryInterface.php | 4 +-- .../WritableArrayRepositoryTest.php | 29 ------------------- 3 files changed, 5 insertions(+), 35 deletions(-) delete mode 100644 tests/Composer/Test/Repository/WritableArrayRepositoryTest.php diff --git a/src/Composer/Repository/WritableArrayRepository.php b/src/Composer/Repository/WritableArrayRepository.php index 6b47e7e47..041e40562 100644 --- a/src/Composer/Repository/WritableArrayRepository.php +++ b/src/Composer/Repository/WritableArrayRepository.php @@ -42,12 +42,11 @@ class WritableArrayRepository extends ArrayRepository implements WritableReposit { $packages = $this->getPackages(); - // get at most one package of each (name, version) combination, prefering non-aliased ones + // get at most one package of each name, preferring non-aliased ones $packagesByName = array(); foreach ($packages as $package) { - $index = $package->getName() . $package->getVersion(); - if (!isset($packagesByName[$index]) || $packagesByName[$index] instanceof AliasPackage) { - $packagesByName[$index] = $package; + if (!isset($packagesByName[$package->getName()]) || $packagesByName[$package->getName()] instanceof AliasPackage) { + $packagesByName[$package->getName()] = $package; } } diff --git a/src/Composer/Repository/WritableRepositoryInterface.php b/src/Composer/Repository/WritableRepositoryInterface.php index c046c377c..4500005d9 100644 --- a/src/Composer/Repository/WritableRepositoryInterface.php +++ b/src/Composer/Repository/WritableRepositoryInterface.php @@ -41,14 +41,14 @@ interface WritableRepositoryInterface extends RepositoryInterface public function removePackage(PackageInterface $package); /** - * Get unique packages, with aliases resolved and removed + * Get unique packages (at most one package of each name), with aliases resolved and removed. * * @return PackageInterface[] */ public function getCanonicalPackages(); /** - * Forces a reload of all packages + * Forces a reload of all packages. */ public function reload(); } diff --git a/tests/Composer/Test/Repository/WritableArrayRepositoryTest.php b/tests/Composer/Test/Repository/WritableArrayRepositoryTest.php deleted file mode 100644 index e4cb36ccc..000000000 --- a/tests/Composer/Test/Repository/WritableArrayRepositoryTest.php +++ /dev/null @@ -1,29 +0,0 @@ - - * Jordi Boggiano - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Composer\Test\Repository; - -use Composer\Repository\WritableArrayRepository; -use Composer\TestCase; - -final class WritableArrayRepositoryTest extends TestCase -{ - public function testGetCanonicalPackagesReturnsDifferentVersionsOfSameNamedPackage() - { - $repository = new WritableArrayRepository(); - - $repository->addPackage($this->getPackage('foo', 1)); - $repository->addPackage($this->getPackage('foo', 2)); - - $this->assertCount(2, $repository->getCanonicalPackages()); - } -}