2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2020-04-19 14:00:21 +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;
|
|
|
|
|
2022-02-02 21:18:00 +00:00
|
|
|
use Composer\Autoload\ClassLoader;
|
2020-04-19 14:00:21 +00:00
|
|
|
use Composer\InstalledVersions;
|
|
|
|
use Composer\Semver\VersionParser;
|
|
|
|
|
|
|
|
class InstalledVersionsTest extends TestCase
|
|
|
|
{
|
2022-02-02 21:18:00 +00:00
|
|
|
/** @var array<ClassLoader> */
|
2022-02-02 15:08:36 +00:00
|
|
|
private static $previousRegisteredLoaders;
|
|
|
|
|
2021-10-16 08:16:06 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-02-25 09:00:36 +00:00
|
|
|
private $root;
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
public static function setUpBeforeClass(): void
|
2021-02-01 12:32:03 +00:00
|
|
|
{
|
|
|
|
// disable multiple-ClassLoader-based checks of InstalledVersions by making it seem like no
|
|
|
|
// class loaders are registered
|
|
|
|
$prop = new \ReflectionProperty('Composer\Autoload\ClassLoader', 'registeredLoaders');
|
|
|
|
$prop->setAccessible(true);
|
2022-02-02 15:08:36 +00:00
|
|
|
self::$previousRegisteredLoaders = $prop->getValue();
|
2023-08-30 09:42:33 +00:00
|
|
|
$prop->setValue(null, []);
|
2021-02-01 12:32:03 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
public static function tearDownAfterClass(): void
|
2021-02-12 10:05:13 +00:00
|
|
|
{
|
2022-02-02 15:08:36 +00:00
|
|
|
$prop = new \ReflectionProperty('Composer\Autoload\ClassLoader', 'registeredLoaders');
|
|
|
|
$prop->setAccessible(true);
|
2023-08-30 09:42:33 +00:00
|
|
|
$prop->setValue(null, self::$previousRegisteredLoaders);
|
2024-05-22 07:09:04 +00:00
|
|
|
InstalledVersions::reload(null); // @phpstan-ignore argument.type
|
2021-02-12 10:05:13 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
public function setUp(): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2022-05-11 14:05:35 +00:00
|
|
|
$this->root = self::getUniqueTmpDirectory();
|
2021-02-25 09:00:36 +00:00
|
|
|
|
|
|
|
$dir = $this->root;
|
2024-02-08 13:33:59 +00:00
|
|
|
InstalledVersions::reload(require __DIR__.'/Repository/Fixtures/installed_relative.php');
|
2020-04-19 14:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetInstalledPackages(): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$names = [
|
2020-04-19 14:00:21 +00:00
|
|
|
'__root__',
|
|
|
|
'a/provider',
|
|
|
|
'a/provider2',
|
|
|
|
'b/replacer',
|
|
|
|
'c/c',
|
|
|
|
'foo/impl',
|
|
|
|
'foo/impl2',
|
|
|
|
'foo/replaced',
|
2021-05-21 11:54:18 +00:00
|
|
|
'meta/package',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-04-19 14:00:21 +00:00
|
|
|
$this->assertSame($names, InstalledVersions::getInstalledPackages());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider isInstalledProvider
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testIsInstalled(bool $expected, string $name, bool $includeDevRequirements = true): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2021-02-09 10:36:19 +00:00
|
|
|
$this->assertSame($expected, InstalledVersions::isInstalled($name, $includeDevRequirements));
|
2020-04-19 14:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public static function isInstalledProvider(): array
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
[true, 'foo/impl'],
|
|
|
|
[true, 'foo/replaced'],
|
|
|
|
[true, 'c/c'],
|
|
|
|
[false, 'c/c', false],
|
|
|
|
[true, '__root__'],
|
|
|
|
[true, 'b/replacer'],
|
|
|
|
[false, 'not/there'],
|
|
|
|
[true, 'meta/package'],
|
|
|
|
];
|
2020-04-19 14:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider satisfiesProvider
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testSatisfies(bool $expected, string $name, string $constraint): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
|
|
|
$this->assertSame($expected, InstalledVersions::satisfies(new VersionParser, $name, $constraint));
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public static function satisfiesProvider(): array
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
[true, 'foo/impl', '1.5'],
|
|
|
|
[true, 'foo/impl', '1.2'],
|
|
|
|
[true, 'foo/impl', '^1.0'],
|
|
|
|
[true, 'foo/impl', '^3 || ^2'],
|
|
|
|
[false, 'foo/impl', '^3'],
|
|
|
|
|
|
|
|
[true, 'foo/replaced', '3.5'],
|
|
|
|
[true, 'foo/replaced', '^3.2'],
|
|
|
|
[false, 'foo/replaced', '4.0'],
|
|
|
|
|
|
|
|
[true, 'c/c', '3.0.0'],
|
|
|
|
[true, 'c/c', '^3'],
|
|
|
|
[false, 'c/c', '^3.1'],
|
|
|
|
|
|
|
|
[true, '__root__', 'dev-master'],
|
|
|
|
[true, '__root__', '^1.10'],
|
|
|
|
[false, '__root__', '^2'],
|
|
|
|
|
|
|
|
[true, 'b/replacer', '^2.1'],
|
|
|
|
[false, 'b/replacer', '^2.3'],
|
|
|
|
|
|
|
|
[true, 'a/provider2', '^1.2'],
|
|
|
|
[true, 'a/provider2', '^1.4'],
|
|
|
|
[false, 'a/provider2', '^1.5'],
|
|
|
|
];
|
2020-04-19 14:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getVersionRangesProvider
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testGetVersionRanges(string $expected, string $name): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
|
|
|
$this->assertSame($expected, InstalledVersions::getVersionRanges($name));
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public static function getVersionRangesProvider(): array
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['dev-master || 1.10.x-dev', '__root__'],
|
|
|
|
['^1.1 || 1.2 || 1.4 || 2.0', 'foo/impl'],
|
|
|
|
['2.2 || 2.0', 'foo/impl2'],
|
|
|
|
['^3.0', 'foo/replaced'],
|
|
|
|
['1.1', 'a/provider'],
|
|
|
|
['1.2 || 1.4', 'a/provider2'],
|
|
|
|
['2.2', 'b/replacer'],
|
|
|
|
['3.0', 'c/c'],
|
|
|
|
];
|
2020-04-19 14:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getVersionProvider
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testGetVersion(?string $expected, string $name): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
|
|
|
$this->assertSame($expected, InstalledVersions::getVersion($name));
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public static function getVersionProvider(): array
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['dev-master', '__root__'],
|
|
|
|
[null, 'foo/impl'],
|
|
|
|
[null, 'foo/impl2'],
|
|
|
|
[null, 'foo/replaced'],
|
|
|
|
['1.1.0.0', 'a/provider'],
|
|
|
|
['1.2.0.0', 'a/provider2'],
|
|
|
|
['2.2.0.0', 'b/replacer'],
|
|
|
|
['3.0.0.0', 'c/c'],
|
|
|
|
];
|
2020-04-19 14:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getPrettyVersionProvider
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testGetPrettyVersion(?string $expected, string $name): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
|
|
|
$this->assertSame($expected, InstalledVersions::getPrettyVersion($name));
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public static function getPrettyVersionProvider(): array
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['dev-master', '__root__'],
|
|
|
|
[null, 'foo/impl'],
|
|
|
|
[null, 'foo/impl2'],
|
|
|
|
[null, 'foo/replaced'],
|
|
|
|
['1.1', 'a/provider'],
|
|
|
|
['1.2', 'a/provider2'],
|
|
|
|
['2.2', 'b/replacer'],
|
|
|
|
['3.0', 'c/c'],
|
|
|
|
];
|
2020-04-19 14:00:21 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetVersionOutOfBounds(): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('OutOfBoundsException');
|
2020-04-19 14:00:21 +00:00
|
|
|
InstalledVersions::getVersion('not/installed');
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetRootPackage(): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$this->assertSame([
|
2022-04-29 08:30:00 +00:00
|
|
|
'name' => '__root__',
|
2020-04-19 14:00:21 +00:00
|
|
|
'pretty_version' => 'dev-master',
|
|
|
|
'version' => 'dev-master',
|
2022-04-29 08:30:00 +00:00
|
|
|
'reference' => 'sourceref-by-default',
|
2021-02-25 09:00:36 +00:00
|
|
|
'type' => 'library',
|
2021-05-21 11:25:31 +00:00
|
|
|
'install_path' => $this->root . '/./',
|
2022-08-17 12:20:07 +00:00
|
|
|
'aliases' => [
|
2020-04-19 14:00:21 +00:00
|
|
|
'1.10.x-dev',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
2021-04-07 12:39:42 +00:00
|
|
|
'dev' => true,
|
2022-08-17 12:20:07 +00:00
|
|
|
], InstalledVersions::getRootPackage());
|
2020-04-19 14:00:21 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:17:06 +00:00
|
|
|
/**
|
|
|
|
* @group legacy
|
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetRawData(): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2021-02-25 09:00:36 +00:00
|
|
|
$dir = $this->root;
|
2024-02-08 13:33:59 +00:00
|
|
|
$this->assertSame(require __DIR__.'/Repository/Fixtures/installed_relative.php', InstalledVersions::getRawData());
|
2020-04-19 14:00:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getReferenceProvider
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testGetReference(?string $expected, string $name): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
|
|
|
$this->assertSame($expected, InstalledVersions::getReference($name));
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public static function getReferenceProvider(): array
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['sourceref-by-default', '__root__'],
|
|
|
|
[null, 'foo/impl'],
|
|
|
|
[null, 'foo/impl2'],
|
|
|
|
[null, 'foo/replaced'],
|
|
|
|
['distref-as-no-source', 'a/provider'],
|
|
|
|
['distref-as-installed-from-dist', 'a/provider2'],
|
|
|
|
[null, 'b/replacer'],
|
|
|
|
[null, 'c/c'],
|
|
|
|
];
|
2020-04-19 14:00:21 +00:00
|
|
|
}
|
2021-02-25 09:00:36 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetInstalledPackagesByType(): void
|
2021-02-25 09:00:36 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$names = [
|
2021-02-25 09:00:36 +00:00
|
|
|
'__root__',
|
|
|
|
'a/provider',
|
|
|
|
'a/provider2',
|
|
|
|
'b/replacer',
|
|
|
|
'c/c',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2021-02-25 09:00:36 +00:00
|
|
|
|
|
|
|
$this->assertSame($names, \Composer\InstalledVersions::getInstalledPackagesByType('library'));
|
|
|
|
}
|
2021-05-21 11:25:31 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetInstallPath(): void
|
2021-05-21 11:25:31 +00:00
|
|
|
{
|
2021-05-21 11:54:18 +00:00
|
|
|
$this->assertSame(realpath($this->root), realpath(\Composer\InstalledVersions::getInstallPath('__root__')));
|
|
|
|
$this->assertSame('/foo/bar/vendor/c/c', \Composer\InstalledVersions::getInstallPath('c/c'));
|
2021-05-21 11:25:31 +00:00
|
|
|
$this->assertNull(\Composer\InstalledVersions::getInstallPath('foo/impl'));
|
|
|
|
}
|
2020-04-19 14:00:21 +00:00
|
|
|
}
|