2020-04-19 14:00:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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();
|
2021-02-01 12:32:03 +00:00
|
|
|
$prop->setValue(array());
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
$prop->setValue(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
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
2021-02-25 09:00:36 +00:00
|
|
|
$this->root = $this->getUniqueTmpDirectory();
|
|
|
|
|
|
|
|
$dir = $this->root;
|
2020-04-19 14:00:21 +00:00
|
|
|
InstalledVersions::reload(require __DIR__.'/Repository/Fixtures/installed.php');
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetInstalledPackages(): void
|
2020-04-19 14:00:21 +00:00
|
|
|
{
|
|
|
|
$names = array(
|
|
|
|
'__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',
|
2020-04-19 14:00:21 +00:00
|
|
|
);
|
|
|
|
$this->assertSame($names, InstalledVersions::getInstalledPackages());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider isInstalledProvider
|
2021-11-02 13:32:09 +00:00
|
|
|
* @param bool $expected
|
|
|
|
* @param string $name
|
|
|
|
* @param bool $includeDevRequirements
|
2020-04-19 14:00:21 +00:00
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsInstalled($expected, $name, $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
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(true, 'foo/impl'),
|
|
|
|
array(true, 'foo/replaced'),
|
|
|
|
array(true, 'c/c'),
|
2021-02-09 10:36:19 +00:00
|
|
|
array(false, 'c/c', false),
|
2020-04-19 14:00:21 +00:00
|
|
|
array(true, '__root__'),
|
|
|
|
array(true, 'b/replacer'),
|
|
|
|
array(false, 'not/there'),
|
2021-05-21 11:54:18 +00:00
|
|
|
array(true, 'meta/package'),
|
2020-04-19 14:00:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider satisfiesProvider
|
2021-11-02 13:32:09 +00:00
|
|
|
* @param bool $expected
|
|
|
|
* @param string $name
|
|
|
|
* @param string $constraint
|
2020-04-19 14:00:21 +00:00
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testSatisfies($expected, $name, $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
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(true, 'foo/impl', '1.5'),
|
|
|
|
array(true, 'foo/impl', '1.2'),
|
|
|
|
array(true, 'foo/impl', '^1.0'),
|
|
|
|
array(true, 'foo/impl', '^3 || ^2'),
|
|
|
|
array(false, 'foo/impl', '^3'),
|
|
|
|
|
|
|
|
array(true, 'foo/replaced', '3.5'),
|
|
|
|
array(true, 'foo/replaced', '^3.2'),
|
|
|
|
array(false, 'foo/replaced', '4.0'),
|
|
|
|
|
|
|
|
array(true, 'c/c', '3.0.0'),
|
|
|
|
array(true, 'c/c', '^3'),
|
|
|
|
array(false, 'c/c', '^3.1'),
|
|
|
|
|
|
|
|
array(true, '__root__', 'dev-master'),
|
|
|
|
array(true, '__root__', '^1.10'),
|
|
|
|
array(false, '__root__', '^2'),
|
|
|
|
|
|
|
|
array(true, 'b/replacer', '^2.1'),
|
|
|
|
array(false, 'b/replacer', '^2.3'),
|
|
|
|
|
|
|
|
array(true, 'a/provider2', '^1.2'),
|
|
|
|
array(true, 'a/provider2', '^1.4'),
|
|
|
|
array(false, 'a/provider2', '^1.5'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getVersionRangesProvider
|
2021-11-02 13:32:09 +00:00
|
|
|
* @param string $expected
|
|
|
|
* @param string $name
|
2020-04-19 14:00:21 +00:00
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetVersionRanges($expected, $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
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('dev-master || 1.10.x-dev', '__root__'),
|
|
|
|
array('^1.1 || 1.2 || 1.4 || 2.0', 'foo/impl'),
|
|
|
|
array('2.2 || 2.0', 'foo/impl2'),
|
|
|
|
array('^3.0', 'foo/replaced'),
|
|
|
|
array('1.1', 'a/provider'),
|
|
|
|
array('1.2 || 1.4', 'a/provider2'),
|
|
|
|
array('2.2', 'b/replacer'),
|
|
|
|
array('3.0', 'c/c'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getVersionProvider
|
2021-11-02 13:32:09 +00:00
|
|
|
* @param ?string $expected
|
|
|
|
* @param string $name
|
2020-04-19 14:00:21 +00:00
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetVersion($expected, $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
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('dev-master', '__root__'),
|
|
|
|
array(null, 'foo/impl'),
|
|
|
|
array(null, 'foo/impl2'),
|
|
|
|
array(null, 'foo/replaced'),
|
|
|
|
array('1.1.0.0', 'a/provider'),
|
|
|
|
array('1.2.0.0', 'a/provider2'),
|
|
|
|
array('2.2.0.0', 'b/replacer'),
|
|
|
|
array('3.0.0.0', 'c/c'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getPrettyVersionProvider
|
2021-11-02 13:32:09 +00:00
|
|
|
* @param ?string $expected
|
|
|
|
* @param string $name
|
2020-04-19 14:00:21 +00:00
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetPrettyVersion($expected, $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
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('dev-master', '__root__'),
|
|
|
|
array(null, 'foo/impl'),
|
|
|
|
array(null, 'foo/impl2'),
|
|
|
|
array(null, 'foo/replaced'),
|
|
|
|
array('1.1', 'a/provider'),
|
|
|
|
array('1.2', 'a/provider2'),
|
|
|
|
array('2.2', 'b/replacer'),
|
|
|
|
array('3.0', 'c/c'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
$this->assertSame(array(
|
|
|
|
'pretty_version' => 'dev-master',
|
|
|
|
'version' => 'dev-master',
|
2021-02-25 09:00:36 +00:00
|
|
|
'type' => 'library',
|
2021-05-21 11:25:31 +00:00
|
|
|
'install_path' => $this->root . '/./',
|
2020-04-19 14:00:21 +00:00
|
|
|
'aliases' => array(
|
|
|
|
'1.10.x-dev',
|
|
|
|
),
|
|
|
|
'reference' => 'sourceref-by-default',
|
|
|
|
'name' => '__root__',
|
2021-04-07 12:39:42 +00:00
|
|
|
'dev' => true,
|
2020-04-19 14:00:21 +00:00
|
|
|
), InstalledVersions::getRootPackage());
|
|
|
|
}
|
|
|
|
|
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;
|
2020-04-19 14:00:21 +00:00
|
|
|
$this->assertSame(require __DIR__.'/Repository/Fixtures/installed.php', InstalledVersions::getRawData());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getReferenceProvider
|
2021-11-02 13:32:09 +00:00
|
|
|
* @param ?string $expected
|
|
|
|
* @param string $name
|
2020-04-19 14:00:21 +00:00
|
|
|
*/
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetReference($expected, $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
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('sourceref-by-default', '__root__'),
|
|
|
|
array(null, 'foo/impl'),
|
|
|
|
array(null, 'foo/impl2'),
|
|
|
|
array(null, 'foo/replaced'),
|
|
|
|
array('distref-as-no-source', 'a/provider'),
|
|
|
|
array('distref-as-installed-from-dist', 'a/provider2'),
|
|
|
|
array(null, 'b/replacer'),
|
|
|
|
array(null, 'c/c'),
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
$names = array(
|
|
|
|
'__root__',
|
|
|
|
'a/provider',
|
|
|
|
'a/provider2',
|
|
|
|
'b/replacer',
|
|
|
|
'c/c',
|
|
|
|
);
|
|
|
|
|
|
|
|
$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
|
|
|
}
|