2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2012-10-22 21:44:56 +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\IO\NullIO;
|
2021-12-08 13:10:42 +00:00
|
|
|
use Composer\Json\JsonFile;
|
2016-06-21 14:38:52 +00:00
|
|
|
use Composer\Repository\ComposerRepository;
|
|
|
|
use Composer\Repository\RepositoryInterface;
|
2012-10-22 21:44:56 +00:00
|
|
|
use Composer\Test\Mock\FactoryMock;
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2013-03-21 08:06:11 +00:00
|
|
|
use Composer\Package\Loader\ArrayLoader;
|
2012-10-22 21:44:56 +00:00
|
|
|
|
|
|
|
class ComposerRepositoryTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider loadDataProvider
|
2021-10-27 13:29:52 +00:00
|
|
|
*
|
|
|
|
* @param mixed[] $expected
|
|
|
|
* @param array<string, mixed> $repoPackages
|
2012-10-22 21:44:56 +00:00
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testLoadData(array $expected, array $repoPackages): void
|
2012-10-22 21:44:56 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$repoConfig = [
|
2012-11-05 14:39:43 +00:00
|
|
|
'url' => 'http://example.org',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2012-10-22 21:44:56 +00:00
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$repository = $this->getMockBuilder('Composer\Repository\ComposerRepository')
|
2022-08-17 12:20:07 +00:00
|
|
|
->onlyMethods(['loadRootServerFile'])
|
|
|
|
->setConstructorArgs([
|
2012-10-22 21:44:56 +00:00
|
|
|
$repoConfig,
|
|
|
|
new NullIO,
|
|
|
|
FactoryMock::createConfig(),
|
2018-11-12 14:34:54 +00:00
|
|
|
$this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(),
|
2020-11-22 13:48:56 +00:00
|
|
|
$this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock(),
|
2022-08-17 12:20:07 +00:00
|
|
|
])
|
2018-04-12 08:24:56 +00:00
|
|
|
->getMock();
|
2012-10-22 21:44:56 +00:00
|
|
|
|
|
|
|
$repository
|
2013-03-10 12:32:59 +00:00
|
|
|
->expects($this->exactly(2))
|
2012-10-22 21:44:56 +00:00
|
|
|
->method('loadRootServerFile')
|
|
|
|
->will($this->returnValue($repoPackages));
|
|
|
|
|
|
|
|
// Triggers initialization
|
|
|
|
$packages = $repository->getPackages();
|
|
|
|
|
|
|
|
// Final sanity check, ensure the correct number of packages were added.
|
|
|
|
$this->assertCount(count($expected), $packages);
|
2022-01-03 14:40:32 +00:00
|
|
|
|
|
|
|
foreach ($expected as $index => $pkg) {
|
|
|
|
self::assertSame($pkg['name'].' '.$pkg['version'], $packages[$index]->getName().' '.$packages[$index]->getPrettyVersion());
|
|
|
|
}
|
2012-10-22 21:44:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function loadDataProvider(): array
|
2012-10-22 21:44:56 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
2012-10-22 21:44:56 +00:00
|
|
|
// Old repository format
|
2022-08-17 12:20:07 +00:00
|
|
|
[
|
|
|
|
[
|
|
|
|
['name' => 'foo/bar', 'version' => '1.0.0'],
|
|
|
|
],
|
|
|
|
['foo/bar' => [
|
2012-10-22 21:44:56 +00:00
|
|
|
'name' => 'foo/bar',
|
2022-08-17 12:20:07 +00:00
|
|
|
'versions' => [
|
|
|
|
'1.0.0' => ['name' => 'foo/bar', 'version' => '1.0.0'],
|
|
|
|
],
|
|
|
|
]],
|
|
|
|
],
|
2012-10-22 21:44:56 +00:00
|
|
|
// New repository format
|
2022-08-17 12:20:07 +00:00
|
|
|
[
|
|
|
|
[
|
|
|
|
['name' => 'bar/foo', 'version' => '3.14'],
|
|
|
|
['name' => 'bar/foo', 'version' => '3.145'],
|
|
|
|
],
|
|
|
|
['packages' => [
|
|
|
|
'bar/foo' => [
|
|
|
|
'3.14' => ['name' => 'bar/foo', 'version' => '3.14'],
|
|
|
|
'3.145' => ['name' => 'bar/foo', 'version' => '3.145'],
|
|
|
|
],
|
|
|
|
]],
|
|
|
|
],
|
|
|
|
];
|
2012-10-22 21:44:56 +00:00
|
|
|
}
|
2013-03-21 08:06:11 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testWhatProvides(): void
|
2015-05-05 17:44:07 +00:00
|
|
|
{
|
2013-03-21 08:06:11 +00:00
|
|
|
$repo = $this->getMockBuilder('Composer\Repository\ComposerRepository')
|
2022-08-17 12:20:07 +00:00
|
|
|
->setConstructorArgs([
|
|
|
|
['url' => 'https://dummy.test.link'],
|
2019-02-20 12:41:43 +00:00
|
|
|
new NullIO,
|
|
|
|
FactoryMock::createConfig(),
|
|
|
|
$this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(),
|
2020-11-22 13:48:56 +00:00
|
|
|
$this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock(),
|
2022-08-17 12:20:07 +00:00
|
|
|
])
|
|
|
|
->onlyMethods(['fetchFile'])
|
2013-03-21 08:06:11 +00:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$cache = $this->getMockBuilder('Composer\Cache')->disableOriginalConstructor()->getMock();
|
|
|
|
$cache->expects($this->any())
|
|
|
|
->method('sha256')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$properties = [
|
2013-03-21 08:06:11 +00:00
|
|
|
'cache' => $cache,
|
|
|
|
'loader' => new ArrayLoader(),
|
2022-08-17 12:20:07 +00:00
|
|
|
'providerListing' => ['a' => ['sha256' => 'xxx']],
|
2016-02-15 20:51:21 +00:00
|
|
|
'providersUrl' => 'https://dummy.test.link/to/%package%/file',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2013-03-21 08:06:11 +00:00
|
|
|
|
|
|
|
foreach ($properties as $property => $value) {
|
|
|
|
$ref = new \ReflectionProperty($repo, $property);
|
|
|
|
$ref->setAccessible(true);
|
|
|
|
$ref->setValue($repo, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$repo->expects($this->any())
|
|
|
|
->method('fetchFile')
|
2022-08-17 12:20:07 +00:00
|
|
|
->will($this->returnValue([
|
|
|
|
'packages' => [
|
|
|
|
[[
|
2015-06-18 13:40:27 +00:00
|
|
|
'uid' => 1,
|
|
|
|
'name' => 'a',
|
|
|
|
'version' => 'dev-master',
|
2022-08-17 12:20:07 +00:00
|
|
|
'extra' => ['branch-alias' => ['dev-master' => '1.0.x-dev']],
|
|
|
|
]],
|
|
|
|
[[
|
2015-06-18 13:40:27 +00:00
|
|
|
'uid' => 2,
|
|
|
|
'name' => 'a',
|
|
|
|
'version' => 'dev-develop',
|
2022-08-17 12:20:07 +00:00
|
|
|
'extra' => ['branch-alias' => ['dev-develop' => '1.1.x-dev']],
|
|
|
|
]],
|
|
|
|
[[
|
2015-06-18 13:40:27 +00:00
|
|
|
'uid' => 3,
|
|
|
|
'name' => 'a',
|
|
|
|
'version' => '0.6',
|
2022-08-17 12:20:07 +00:00
|
|
|
]],
|
|
|
|
],
|
|
|
|
]));
|
2013-03-21 08:06:11 +00:00
|
|
|
|
2018-12-04 16:03:56 +00:00
|
|
|
$reflMethod = new \ReflectionMethod($repo, 'whatProvides');
|
|
|
|
$reflMethod->setAccessible(true);
|
2020-04-13 10:10:31 +00:00
|
|
|
$packages = $reflMethod->invoke($repo, 'a');
|
2013-03-21 08:06:11 +00:00
|
|
|
|
2019-01-14 14:30:55 +00:00
|
|
|
$this->assertCount(5, $packages);
|
2022-08-17 12:20:07 +00:00
|
|
|
$this->assertEquals(['1', '1-alias', '2', '2-alias', '3'], array_keys($packages));
|
2015-06-18 13:40:27 +00:00
|
|
|
$this->assertSame($packages['2'], $packages['2-alias']->getAliasOf());
|
2013-03-21 08:06:11 +00:00
|
|
|
}
|
2016-06-21 14:38:52 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testSearchWithType(): void
|
2016-06-21 14:38:52 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$repoConfig = [
|
2016-06-21 14:38:52 +00:00
|
|
|
'url' => 'http://example.org',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2016-06-21 14:38:52 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$result = [
|
|
|
|
'results' => [
|
|
|
|
[
|
2016-06-21 14:38:52 +00:00
|
|
|
'name' => 'foo',
|
2017-03-08 14:07:29 +00:00
|
|
|
'description' => null,
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2016-06-21 14:38:52 +00:00
|
|
|
|
2021-12-09 16:09:07 +00:00
|
|
|
$httpDownloader = $this->getHttpDownloaderMock();
|
|
|
|
$httpDownloader->expects(
|
|
|
|
[
|
2022-08-17 12:20:07 +00:00
|
|
|
['url' => 'http://example.org/packages.json', 'body' => JsonFile::encode(['search' => '/search.json?q=%query%&type=%type%'])],
|
2021-12-09 16:09:07 +00:00
|
|
|
['url' => 'http://example.org/search.json?q=foo&type=composer-plugin', 'body' => JsonFile::encode($result)],
|
2022-08-17 12:20:07 +00:00
|
|
|
['url' => 'http://example.org/search.json?q=foo&type=library', 'body' => JsonFile::encode([])],
|
2021-12-09 16:09:07 +00:00
|
|
|
],
|
|
|
|
true
|
|
|
|
);
|
2018-10-31 11:44:54 +00:00
|
|
|
$eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2021-12-09 16:09:07 +00:00
|
|
|
$config = FactoryMock::createConfig();
|
|
|
|
$config->merge(['config' => ['cache-read-only' => true]]);
|
|
|
|
$repository = new ComposerRepository($repoConfig, new NullIO, $config, $httpDownloader, $eventDispatcher);
|
2016-06-21 14:38:52 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
2022-08-17 12:20:07 +00:00
|
|
|
[['name' => 'foo', 'description' => null]],
|
2016-06-21 14:38:52 +00:00
|
|
|
$repository->search('foo', RepositoryInterface::SEARCH_FULLTEXT, 'composer-plugin')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEmpty(
|
|
|
|
$repository->search('foo', RepositoryInterface::SEARCH_FULLTEXT, 'library')
|
|
|
|
);
|
|
|
|
}
|
2019-05-10 11:55:31 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testSearchWithSpecialChars(): void
|
2022-02-04 09:23:10 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$repoConfig = [
|
2022-02-04 09:23:10 +00:00
|
|
|
'url' => 'http://example.org',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2022-02-04 09:23:10 +00:00
|
|
|
|
2022-02-04 15:50:01 +00:00
|
|
|
$httpDownloader = $this->getHttpDownloaderMock();
|
|
|
|
$httpDownloader->expects(
|
|
|
|
[
|
2022-08-17 12:20:07 +00:00
|
|
|
['url' => 'http://example.org/packages.json', 'body' => JsonFile::encode(['search' => '/search.json?q=%query%&type=%type%'])],
|
|
|
|
['url' => 'http://example.org/search.json?q=foo+bar&type=', 'body' => JsonFile::encode([])],
|
2022-02-04 15:50:01 +00:00
|
|
|
],
|
|
|
|
true
|
2022-02-04 09:23:10 +00:00
|
|
|
);
|
|
|
|
$eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2022-02-04 15:50:01 +00:00
|
|
|
$config = FactoryMock::createConfig();
|
|
|
|
$config->merge(['config' => ['cache-read-only' => true]]);
|
|
|
|
$repository = new ComposerRepository($repoConfig, new NullIO, $config, $httpDownloader, $eventDispatcher);
|
2022-02-04 09:23:10 +00:00
|
|
|
|
|
|
|
$this->assertEmpty(
|
|
|
|
$repository->search('foo bar', RepositoryInterface::SEARCH_FULLTEXT)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testSearchWithAbandonedPackages(): void
|
2021-11-08 09:27:45 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$repoConfig = [
|
2021-12-09 16:09:07 +00:00
|
|
|
'url' => 'http://example.org',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2021-11-08 09:27:45 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$result = [
|
|
|
|
'results' => [
|
|
|
|
[
|
2021-11-08 09:27:45 +00:00
|
|
|
'name' => 'foo1',
|
|
|
|
'description' => null,
|
|
|
|
'abandoned' => true,
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2021-11-08 09:27:45 +00:00
|
|
|
'name' => 'foo2',
|
|
|
|
'description' => null,
|
|
|
|
'abandoned' => 'bar',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2021-11-08 09:27:45 +00:00
|
|
|
|
2021-12-09 16:09:07 +00:00
|
|
|
$httpDownloader = $this->getHttpDownloaderMock();
|
|
|
|
$httpDownloader->expects(
|
|
|
|
[
|
2022-08-17 12:20:07 +00:00
|
|
|
['url' => 'http://example.org/packages.json', 'body' => JsonFile::encode(['search' => '/search.json?q=%query%'])],
|
2021-12-09 16:09:07 +00:00
|
|
|
['url' => 'http://example.org/search.json?q=foo', 'body' => JsonFile::encode($result)],
|
|
|
|
],
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2021-11-08 09:27:45 +00:00
|
|
|
$eventDispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2021-12-09 16:09:07 +00:00
|
|
|
$config = FactoryMock::createConfig();
|
|
|
|
$config->merge(['config' => ['cache-read-only' => true]]);
|
|
|
|
$repository = new ComposerRepository($repoConfig, new NullIO, $config, $httpDownloader, $eventDispatcher);
|
2021-11-08 09:27:45 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
2022-08-17 12:20:07 +00:00
|
|
|
[
|
|
|
|
['name' => 'foo1', 'description' => null, 'abandoned' => true],
|
|
|
|
['name' => 'foo2', 'description' => null, 'abandoned' => 'bar'],
|
|
|
|
],
|
2021-11-08 09:27:45 +00:00
|
|
|
$repository->search('foo')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-10 11:55:31 +00:00
|
|
|
/**
|
2021-10-27 13:29:52 +00:00
|
|
|
* @dataProvider provideCanonicalizeUrlTestCases
|
2022-10-11 14:51:47 +00:00
|
|
|
* @param non-empty-string $url
|
|
|
|
* @param non-empty-string $repositoryUrl
|
2019-05-10 11:55:31 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testCanonicalizeUrl(string $expected, string $url, string $repositoryUrl): void
|
2019-05-10 11:55:31 +00:00
|
|
|
{
|
|
|
|
$repository = new ComposerRepository(
|
2022-08-17 12:20:07 +00:00
|
|
|
['url' => $repositoryUrl],
|
2019-05-10 11:55:31 +00:00
|
|
|
new NullIO(),
|
2019-07-12 16:34:12 +00:00
|
|
|
FactoryMock::createConfig(),
|
|
|
|
$this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock(),
|
|
|
|
$this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock()
|
2019-05-10 11:55:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$object = new \ReflectionObject($repository);
|
|
|
|
|
|
|
|
$method = $object->getMethod('canonicalizeUrl');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
|
// ComposerRepository::__construct ensures that the repository URL has a
|
|
|
|
// protocol, so reset it here in order to test all cases.
|
|
|
|
$property = $object->getProperty('url');
|
|
|
|
$property->setAccessible(true);
|
|
|
|
$property->setValue($repository, $repositoryUrl);
|
|
|
|
|
|
|
|
$this->assertSame($expected, $method->invoke($repository, $url));
|
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function provideCanonicalizeUrlTestCases(): array
|
2019-05-10 11:55:31 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2019-05-10 11:55:31 +00:00
|
|
|
'https://example.org/path/to/file',
|
|
|
|
'/path/to/file',
|
|
|
|
'https://example.org',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2019-05-10 11:55:31 +00:00
|
|
|
'https://example.org/canonic_url',
|
|
|
|
'https://example.org/canonic_url',
|
|
|
|
'https://should-not-see-me.test',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2019-05-10 11:55:31 +00:00
|
|
|
'file:///path/to/repository/file',
|
|
|
|
'/path/to/repository/file',
|
|
|
|
'file:///path/to/repository',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2019-05-10 11:55:31 +00:00
|
|
|
// Assert that the repository URL is returned unchanged if it is
|
|
|
|
// not a URL.
|
|
|
|
// (Backward compatibility test)
|
|
|
|
'invalid_repo_url',
|
|
|
|
'/path/to/file',
|
|
|
|
'invalid_repo_url',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2019-05-10 11:55:31 +00:00
|
|
|
// Assert that URLs can contain sequences resembling pattern
|
|
|
|
// references as understood by preg_replace() without messing up
|
|
|
|
// the result.
|
|
|
|
// (Regression test)
|
|
|
|
'https://example.org/path/to/unusual_$0_filename',
|
|
|
|
'/path/to/unusual_$0_filename',
|
|
|
|
'https://example.org',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
];
|
2019-05-10 11:55:31 +00:00
|
|
|
}
|
2020-01-07 08:14:35 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetProviderNamesWillReturnPartialPackageNames(): void
|
2020-01-07 08:14:35 +00:00
|
|
|
{
|
2021-12-09 16:09:07 +00:00
|
|
|
$httpDownloader = $this->getHttpDownloaderMock();
|
|
|
|
$httpDownloader->expects(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'url' => 'http://example.org/packages.json',
|
2022-08-17 12:20:07 +00:00
|
|
|
'body' => JsonFile::encode([
|
2021-12-09 16:09:07 +00:00
|
|
|
'providers-lazy-url' => '/foo/p/%package%.json',
|
2022-08-17 12:20:07 +00:00
|
|
|
'packages' => ['foo/bar' => [
|
|
|
|
'dev-branch' => ['name' => 'foo/bar'],
|
|
|
|
'v1.0.0' => ['name' => 'foo/bar'],
|
|
|
|
]],
|
|
|
|
]),
|
2021-12-09 16:09:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
true
|
|
|
|
);
|
2020-01-07 08:14:35 +00:00
|
|
|
|
|
|
|
$repository = new ComposerRepository(
|
2022-08-17 12:20:07 +00:00
|
|
|
['url' => 'http://example.org/packages.json'],
|
2020-01-07 08:14:35 +00:00
|
|
|
new NullIO(),
|
|
|
|
FactoryMock::createConfig(),
|
2020-01-14 15:20:31 +00:00
|
|
|
$httpDownloader
|
2020-01-07 08:14:35 +00:00
|
|
|
);
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$this->assertEquals(['foo/bar'], $repository->getPackageNames());
|
2020-01-07 08:14:35 +00:00
|
|
|
}
|
2012-10-22 21:44:56 +00:00
|
|
|
}
|