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

284 lines
8.6 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();
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
{
$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
{
$this->root = $this->getUniqueTmpDirectory();
$dir = $this->root;
InstalledVersions::reload(require __DIR__.'/Repository/Fixtures/installed.php');
}
public function testGetInstalledPackages(): void
{
$names = array(
'__root__',
'a/provider',
'a/provider2',
'b/replacer',
'c/c',
'foo/impl',
'foo/impl2',
'foo/replaced',
'meta/package',
);
$this->assertSame($names, InstalledVersions::getInstalledPackages());
}
/**
* @dataProvider isInstalledProvider
* @param bool $expected
* @param string $name
* @param bool $includeDevRequirements
*/
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
{
return array(
array(true, 'foo/impl'),
array(true, 'foo/replaced'),
array(true, 'c/c'),
array(false, 'c/c', false),
array(true, '__root__'),
array(true, 'b/replacer'),
array(false, 'not/there'),
array(true, 'meta/package'),
);
}
/**
* @dataProvider satisfiesProvider
* @param bool $expected
* @param string $name
* @param string $constraint
*/
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
{
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
* @param string $expected
* @param string $name
*/
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
{
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
2022-02-22 15:47:09 +00:00
* @param null|string $expected
* @param string $name
*/
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
{
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
2022-02-22 15:47:09 +00:00
* @param null|string $expected
* @param string $name
*/
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
{
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'),
);
}
public function testGetVersionOutOfBounds(): void
{
2021-12-09 19:55:26 +00:00
self::expectException('OutOfBoundsException');
InstalledVersions::getVersion('not/installed');
}
public function testGetRootPackage(): void
{
$this->assertSame(array(
'name' => '__root__',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => 'sourceref-by-default',
'type' => 'library',
'install_path' => $this->root . '/./',
'aliases' => array(
'1.10.x-dev',
),
2021-04-07 12:39:42 +00:00
'dev' => true,
), 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
* @param null|string $expected
* @param string $name
*/
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
{
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'),
);
}
public function testGetInstalledPackagesByType(): void
{
$names = array(
'__root__',
'a/provider',
'a/provider2',
'b/replacer',
'c/c',
);
$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'));
}
}