2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2012-01-27 03:53:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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\Repository;
|
|
|
|
|
|
|
|
use Composer\Repository\CompositeRepository;
|
|
|
|
use Composer\Repository\ArrayRepository;
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2012-01-27 03:53:32 +00:00
|
|
|
|
|
|
|
class CompositeRepositoryTest extends TestCase
|
|
|
|
{
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testHasPackage(): void
|
2012-01-27 03:53:32 +00:00
|
|
|
{
|
|
|
|
$arrayRepoOne = new ArrayRepository;
|
|
|
|
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2012-01-27 03:53:32 +00:00
|
|
|
$arrayRepoTwo = new ArrayRepository;
|
|
|
|
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$repo = new CompositeRepository([$arrayRepoOne, $arrayRepoTwo]);
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2012-01-27 03:53:32 +00:00
|
|
|
$this->assertTrue($repo->hasPackage($this->getPackage('foo', '1')), "Should have package 'foo/1'");
|
|
|
|
$this->assertTrue($repo->hasPackage($this->getPackage('bar', '1')), "Should have package 'bar/1'");
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2012-01-27 03:53:32 +00:00
|
|
|
$this->assertFalse($repo->hasPackage($this->getPackage('foo', '2')), "Should not have package 'foo/2'");
|
|
|
|
$this->assertFalse($repo->hasPackage($this->getPackage('bar', '2')), "Should not have package 'bar/2'");
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testFindPackage(): void
|
2012-01-27 03:53:32 +00:00
|
|
|
{
|
|
|
|
$arrayRepoOne = new ArrayRepository;
|
|
|
|
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2012-01-27 03:53:32 +00:00
|
|
|
$arrayRepoTwo = new ArrayRepository;
|
|
|
|
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$repo = new CompositeRepository([$arrayRepoOne, $arrayRepoTwo]);
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2012-01-27 03:53:32 +00:00
|
|
|
$this->assertEquals('foo', $repo->findPackage('foo', '1')->getName(), "Should find package 'foo/1' and get name of 'foo'");
|
|
|
|
$this->assertEquals('1', $repo->findPackage('foo', '1')->getPrettyVersion(), "Should find package 'foo/1' and get pretty version of '1'");
|
|
|
|
$this->assertEquals('bar', $repo->findPackage('bar', '1')->getName(), "Should find package 'bar/1' and get name of 'bar'");
|
|
|
|
$this->assertEquals('1', $repo->findPackage('bar', '1')->getPrettyVersion(), "Should find package 'bar/1' and get pretty version of '1'");
|
|
|
|
$this->assertNull($repo->findPackage('foo', '2'), "Should not find package 'foo/2'");
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testFindPackages(): void
|
2012-01-27 03:53:32 +00:00
|
|
|
{
|
|
|
|
$arrayRepoOne = new ArrayRepository;
|
|
|
|
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
|
|
|
|
$arrayRepoOne->addPackage($this->getPackage('foo', '2'));
|
|
|
|
$arrayRepoOne->addPackage($this->getPackage('bat', '1'));
|
|
|
|
|
|
|
|
$arrayRepoTwo = new ArrayRepository;
|
|
|
|
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
|
|
|
|
$arrayRepoTwo->addPackage($this->getPackage('bar', '2'));
|
|
|
|
$arrayRepoTwo->addPackage($this->getPackage('foo', '3'));
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$repo = new CompositeRepository([$arrayRepoOne, $arrayRepoTwo]);
|
2012-02-21 13:02:08 +00:00
|
|
|
|
|
|
|
$bats = $repo->findPackages('bat');
|
2012-01-27 03:53:32 +00:00
|
|
|
$this->assertCount(1, $bats, "Should find one instance of 'bats' (defined in just one repository)");
|
|
|
|
$this->assertEquals('bat', $bats[0]->getName(), "Should find packages named 'bat'");
|
|
|
|
|
2012-02-21 13:02:08 +00:00
|
|
|
$bars = $repo->findPackages('bar');
|
2012-01-27 03:53:32 +00:00
|
|
|
$this->assertCount(2, $bars, "Should find two instances of 'bar' (both defined in the same repository)");
|
|
|
|
$this->assertEquals('bar', $bars[0]->getName(), "Should find packages named 'bar'");
|
2012-02-21 13:02:08 +00:00
|
|
|
|
|
|
|
$foos = $repo->findPackages('foo');
|
2012-01-27 03:53:32 +00:00
|
|
|
$this->assertCount(3, $foos, "Should find three instances of 'foo' (two defined in one repository, the third in the other)");
|
|
|
|
$this->assertEquals('foo', $foos[0]->getName(), "Should find packages named 'foo'");
|
|
|
|
}
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetPackages(): void
|
2012-01-27 03:53:32 +00:00
|
|
|
{
|
|
|
|
$arrayRepoOne = new ArrayRepository;
|
|
|
|
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2012-01-27 03:53:32 +00:00
|
|
|
$arrayRepoTwo = new ArrayRepository;
|
|
|
|
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$repo = new CompositeRepository([$arrayRepoOne, $arrayRepoTwo]);
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2012-01-27 03:53:32 +00:00
|
|
|
$packages = $repo->getPackages();
|
|
|
|
$this->assertCount(2, $packages, "Should get two packages");
|
|
|
|
$this->assertEquals("foo", $packages[0]->getName(), "First package should have name of 'foo'");
|
|
|
|
$this->assertEquals("1", $packages[0]->getPrettyVersion(), "First package should have pretty version of '1'");
|
|
|
|
$this->assertEquals("bar", $packages[1]->getName(), "Second package should have name of 'bar'");
|
|
|
|
$this->assertEquals("1", $packages[1]->getPrettyVersion(), "Second package should have pretty version of '1'");
|
|
|
|
}
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddRepository(): void
|
2012-01-27 03:53:32 +00:00
|
|
|
{
|
|
|
|
$arrayRepoOne = new ArrayRepository;
|
|
|
|
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2012-01-27 03:53:32 +00:00
|
|
|
$arrayRepoTwo = new ArrayRepository;
|
|
|
|
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
|
|
|
|
$arrayRepoTwo->addPackage($this->getPackage('bar', '2'));
|
|
|
|
$arrayRepoTwo->addPackage($this->getPackage('bar', '3'));
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$repo = new CompositeRepository([$arrayRepoOne]);
|
2012-01-27 03:53:32 +00:00
|
|
|
$this->assertCount(1, $repo, "Composite repository should have just one package before addRepository() is called");
|
|
|
|
$repo->addRepository($arrayRepoTwo);
|
|
|
|
$this->assertCount(4, $repo, "Composite repository should have four packages after addRepository() is called");
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testCount(): void
|
2012-01-27 03:53:32 +00:00
|
|
|
{
|
|
|
|
$arrayRepoOne = new ArrayRepository;
|
|
|
|
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2012-01-27 03:53:32 +00:00
|
|
|
$arrayRepoTwo = new ArrayRepository;
|
|
|
|
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$repo = new CompositeRepository([$arrayRepoOne, $arrayRepoTwo]);
|
2012-02-21 13:02:08 +00:00
|
|
|
|
2022-06-22 12:20:08 +00:00
|
|
|
$this->assertCount(2, $repo, "Should return '2' for count(\$repo)");
|
2012-01-27 03:53:32 +00:00
|
|
|
}
|
2013-06-19 21:21:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideMethodCalls
|
2021-10-27 13:29:52 +00:00
|
|
|
*
|
|
|
|
* @param mixed[] $args
|
2013-06-19 21:21:13 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testNoRepositories(string $method, array $args): void
|
2013-06-19 21:21:13 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$repo = new CompositeRepository([]);
|
|
|
|
$this->assertEquals([], call_user_func_array([$repo, $method], $args));
|
2013-06-19 21:21:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public function provideMethodCalls(): array
|
2013-06-19 21:21:13 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['findPackages', ['foo']],
|
|
|
|
['search', ['foo']],
|
|
|
|
['getPackages', []],
|
|
|
|
];
|
2013-06-19 21:21:13 +00:00
|
|
|
}
|
2012-01-27 03:53:32 +00:00
|
|
|
}
|