1
0
Fork 0
composer/tests/Composer/Test/InstalledVersionsTest.php

270 lines
7.9 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
/*
* 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;
use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
class InstalledVersionsTest extends TestCase
{
2022-02-02 21:18:00 +00:00
/** @var array<ClassLoader> */
private static $previousRegisteredLoaders;
2021-10-16 08:16:06 +00:00
/**
* @var string
*/
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);
self::$previousRegisteredLoaders = $prop->getValue();
$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
{
$prop = new \ReflectionProperty('Composer\Autoload\ClassLoader', 'registeredLoaders');
$prop->setAccessible(true);
$prop->setValue(null, self::$previousRegisteredLoaders);
InstalledVersions::reload(null); // @phpstan-ignore-line
2021-02-12 10:05:13 +00:00
}
2021-12-08 16:03:05 +00:00
public function setUp(): void
{
$this->root = self::getUniqueTmpDirectory();
$dir = $this->root;
InstalledVersions::reload(require __DIR__.'/Repository/Fixtures/installed.php');
}
public function testGetInstalledPackages(): void
{
2022-08-17 12:20:07 +00:00
$names = [
'__root__',
'a/provider',
'a/provider2',
'b/replacer',
'c/c',
'foo/impl',
'foo/impl2',
'foo/replaced',
'meta/package',
2022-08-17 12:20:07 +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
{
$this->assertSame($expected, InstalledVersions::isInstalled($name, $includeDevRequirements));
}
2022-02-21 12:42:28 +00:00
public static function isInstalledProvider(): array
{
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'],
];
}
/**
* @dataProvider satisfiesProvider
*/
2022-02-22 15:47:09 +00:00
public function testSatisfies(bool $expected, string $name, string $constraint): void
{
$this->assertSame($expected, InstalledVersions::satisfies(new VersionParser, $name, $constraint));
}
2022-02-21 12:42:28 +00:00
public static function satisfiesProvider(): array
{
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'],
];
}
/**
* @dataProvider getVersionRangesProvider
*/
2022-02-22 15:47:09 +00:00
public function testGetVersionRanges(string $expected, string $name): void
{
$this->assertSame($expected, InstalledVersions::getVersionRanges($name));
}
2022-02-21 12:42:28 +00:00
public static function getVersionRangesProvider(): array
{
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'],
];
}
/**
* @dataProvider getVersionProvider
*/
2022-02-22 15:47:09 +00:00
public function testGetVersion(?string $expected, string $name): void
{
$this->assertSame($expected, InstalledVersions::getVersion($name));
}
2022-02-21 12:42:28 +00:00
public static function getVersionProvider(): array
{
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'],
];
}
/**
* @dataProvider getPrettyVersionProvider
*/
2022-02-22 15:47:09 +00:00
public function testGetPrettyVersion(?string $expected, string $name): void
{
$this->assertSame($expected, InstalledVersions::getPrettyVersion($name));
}
2022-02-21 12:42:28 +00:00
public static function getPrettyVersionProvider(): array
{
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'],
];
}
public function testGetVersionOutOfBounds(): void
{
2021-12-09 19:55:26 +00:00
self::expectException('OutOfBoundsException');
InstalledVersions::getVersion('not/installed');
}
public function testGetRootPackage(): void
{
2022-08-17 12:20:07 +00:00
$this->assertSame([
'name' => '__root__',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'sourceref-by-default',
'type' => 'library',
'install_path' => $this->root . '/./',
2022-08-17 12:20:07 +00:00
'aliases' => [
'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());
}
2021-08-18 13:17:06 +00:00
/**
* @group legacy
*/
public function testGetRawData(): void
{
$dir = $this->root;
$this->assertSame(require __DIR__.'/Repository/Fixtures/installed.php', InstalledVersions::getRawData());
}
/**
* @dataProvider getReferenceProvider
*/
2022-02-22 15:47:09 +00:00
public function testGetReference(?string $expected, string $name): void
{
$this->assertSame($expected, InstalledVersions::getReference($name));
}
2022-02-21 12:42:28 +00:00
public static function getReferenceProvider(): array
{
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'],
];
}
public function testGetInstalledPackagesByType(): void
{
2022-08-17 12:20:07 +00:00
$names = [
'__root__',
'a/provider',
'a/provider2',
'b/replacer',
'c/c',
2022-08-17 12:20:07 +00:00
];
$this->assertSame($names, \Composer\InstalledVersions::getInstalledPackagesByType('library'));
}
public function testGetInstallPath(): void
{
$this->assertSame(realpath($this->root), realpath(\Composer\InstalledVersions::getInstallPath('__root__')));
$this->assertSame('/foo/bar/vendor/c/c', \Composer\InstalledVersions::getInstallPath('c/c'));
$this->assertNull(\Composer\InstalledVersions::getInstallPath('foo/impl'));
}
}