2014-07-15 02:25:44 +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\Package\Version;
|
|
|
|
|
|
|
|
use Composer\Package\Version\VersionSelector;
|
2015-10-27 14:43:04 +00:00
|
|
|
use Composer\Package\Package;
|
|
|
|
use Composer\Package\Link;
|
2015-09-24 14:32:36 +00:00
|
|
|
use Composer\Semver\VersionParser;
|
2017-11-04 14:52:13 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2014-07-15 02:25:44 +00:00
|
|
|
|
2017-11-04 14:52:13 +00:00
|
|
|
class VersionSelectorTest extends TestCase
|
2014-07-15 02:25:44 +00:00
|
|
|
{
|
|
|
|
// A) multiple versions, get the latest one
|
|
|
|
// B) targetPackageVersion will pass to pool
|
|
|
|
// C) No results, throw exception
|
|
|
|
|
|
|
|
public function testLatestVersionIsReturned()
|
|
|
|
{
|
|
|
|
$packageName = 'foobar';
|
|
|
|
|
2015-10-27 14:43:04 +00:00
|
|
|
$package1 = $this->createPackage('1.2.1');
|
|
|
|
$package2 = $this->createPackage('1.2.2');
|
|
|
|
$package3 = $this->createPackage('1.2.0');
|
2014-07-15 02:25:44 +00:00
|
|
|
$packages = array($package1, $package2, $package3);
|
|
|
|
|
|
|
|
$pool = $this->createMockPool();
|
|
|
|
$pool->expects($this->once())
|
|
|
|
->method('whatProvides')
|
|
|
|
->with($packageName, null, true)
|
|
|
|
->will($this->returnValue($packages));
|
|
|
|
|
|
|
|
$versionSelector = new VersionSelector($pool);
|
|
|
|
$best = $versionSelector->findBestCandidate($packageName);
|
|
|
|
|
|
|
|
// 1.2.2 should be returned because it's the latest of the returned versions
|
2015-10-27 14:43:04 +00:00
|
|
|
$this->assertSame($package2, $best, 'Latest version should be 1.2.2');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLatestVersionIsReturnedThatMatchesPhpRequirement()
|
|
|
|
{
|
|
|
|
$packageName = 'foobar';
|
|
|
|
|
|
|
|
$parser = new VersionParser;
|
|
|
|
$package1 = $this->createPackage('1.0.0');
|
|
|
|
$package2 = $this->createPackage('2.0.0');
|
|
|
|
$package1->setRequires(array('php' => new Link($packageName, 'php', $parser->parseConstraints('>=5.4'), 'requires', '>=5.4')));
|
|
|
|
$package2->setRequires(array('php' => new Link($packageName, 'php', $parser->parseConstraints('>=5.6'), 'requires', '>=5.6')));
|
|
|
|
$packages = array($package1, $package2);
|
|
|
|
|
|
|
|
$pool = $this->createMockPool();
|
|
|
|
$pool->expects($this->once())
|
|
|
|
->method('whatProvides')
|
|
|
|
->with($packageName, null, true)
|
|
|
|
->will($this->returnValue($packages));
|
|
|
|
|
|
|
|
$versionSelector = new VersionSelector($pool);
|
|
|
|
$best = $versionSelector->findBestCandidate($packageName, null, '5.5.0');
|
|
|
|
|
|
|
|
$this->assertSame($package1, $best, 'Latest version supporting php 5.5 should be returned (1.0.0)');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMostStableVersionIsReturned()
|
|
|
|
{
|
|
|
|
$packageName = 'foobar';
|
|
|
|
|
|
|
|
$package1 = $this->createPackage('1.0.0');
|
|
|
|
$package2 = $this->createPackage('1.1.0-beta');
|
|
|
|
$packages = array($package1, $package2);
|
|
|
|
|
|
|
|
$pool = $this->createMockPool();
|
|
|
|
$pool->expects($this->once())
|
|
|
|
->method('whatProvides')
|
|
|
|
->with($packageName, null, true)
|
|
|
|
->will($this->returnValue($packages));
|
|
|
|
|
|
|
|
$versionSelector = new VersionSelector($pool);
|
|
|
|
$best = $versionSelector->findBestCandidate($packageName);
|
|
|
|
|
|
|
|
$this->assertSame($package1, $best, 'Latest most stable version should be returned (1.0.0)');
|
|
|
|
}
|
|
|
|
|
2016-09-27 14:59:48 +00:00
|
|
|
public function testMostStableVersionIsReturnedRegardlessOfOrder()
|
|
|
|
{
|
|
|
|
$packageName = 'foobar';
|
|
|
|
|
|
|
|
$package1 = $this->createPackage('2.x-dev');
|
|
|
|
$package2 = $this->createPackage('2.0.0-beta3');
|
|
|
|
$packages = array($package1, $package2);
|
|
|
|
|
|
|
|
$pool = $this->createMockPool();
|
|
|
|
$pool->expects($this->at(0))
|
|
|
|
->method('whatProvides')
|
|
|
|
->with($packageName, null, true)
|
|
|
|
->will($this->returnValue($packages));
|
|
|
|
|
|
|
|
$pool->expects($this->at(1))
|
|
|
|
->method('whatProvides')
|
|
|
|
->with($packageName, null, true)
|
|
|
|
->will($this->returnValue(array_reverse($packages)));
|
|
|
|
|
|
|
|
$versionSelector = new VersionSelector($pool);
|
|
|
|
$best = $versionSelector->findBestCandidate($packageName, null, null);
|
|
|
|
$this->assertSame($package2, $best, 'Expecting 2.0.0-beta3, cause beta is more stable than dev');
|
|
|
|
|
|
|
|
$best = $versionSelector->findBestCandidate($packageName, null, null);
|
|
|
|
$this->assertSame($package2, $best, 'Expecting 2.0.0-beta3, cause beta is more stable than dev');
|
|
|
|
}
|
|
|
|
|
2015-10-27 14:43:04 +00:00
|
|
|
public function testHighestVersionIsReturned()
|
|
|
|
{
|
|
|
|
$packageName = 'foobar';
|
|
|
|
|
|
|
|
$package1 = $this->createPackage('1.0.0');
|
|
|
|
$package2 = $this->createPackage('1.1.0-beta');
|
|
|
|
$packages = array($package1, $package2);
|
|
|
|
|
|
|
|
$pool = $this->createMockPool();
|
|
|
|
$pool->expects($this->once())
|
|
|
|
->method('whatProvides')
|
|
|
|
->with($packageName, null, true)
|
|
|
|
->will($this->returnValue($packages));
|
|
|
|
|
|
|
|
$versionSelector = new VersionSelector($pool);
|
2015-11-14 15:04:04 +00:00
|
|
|
$best = $versionSelector->findBestCandidate($packageName, null, null, 'dev');
|
|
|
|
|
|
|
|
$this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHighestVersionMatchingStabilityIsReturned()
|
|
|
|
{
|
|
|
|
$packageName = 'foobar';
|
|
|
|
|
|
|
|
$package1 = $this->createPackage('1.0.0');
|
|
|
|
$package2 = $this->createPackage('1.1.0-beta');
|
|
|
|
$package3 = $this->createPackage('1.2.0-alpha');
|
|
|
|
$packages = array($package1, $package2, $package3);
|
|
|
|
|
|
|
|
$pool = $this->createMockPool();
|
|
|
|
$pool->expects($this->once())
|
|
|
|
->method('whatProvides')
|
|
|
|
->with($packageName, null, true)
|
|
|
|
->will($this->returnValue($packages));
|
|
|
|
|
|
|
|
$versionSelector = new VersionSelector($pool);
|
|
|
|
$best = $versionSelector->findBestCandidate($packageName, null, null, 'beta');
|
2015-10-27 14:43:04 +00:00
|
|
|
|
2016-04-28 21:31:06 +00:00
|
|
|
$this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMostStableUnstableVersionIsReturned()
|
|
|
|
{
|
|
|
|
$packageName = 'foobar';
|
|
|
|
|
|
|
|
$package2 = $this->createPackage('1.1.0-beta');
|
|
|
|
$package3 = $this->createPackage('1.2.0-alpha');
|
|
|
|
$packages = array($package2, $package3);
|
|
|
|
|
|
|
|
$pool = $this->createMockPool();
|
|
|
|
$pool->expects($this->once())
|
|
|
|
->method('whatProvides')
|
|
|
|
->with($packageName, null, true)
|
|
|
|
->will($this->returnValue($packages));
|
|
|
|
|
|
|
|
$versionSelector = new VersionSelector($pool);
|
|
|
|
$best = $versionSelector->findBestCandidate($packageName, null, null, 'stable');
|
|
|
|
|
2015-10-27 14:43:04 +00:00
|
|
|
$this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
|
2014-07-15 02:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFalseReturnedOnNoPackages()
|
|
|
|
{
|
|
|
|
$pool = $this->createMockPool();
|
|
|
|
$pool->expects($this->once())
|
|
|
|
->method('whatProvides')
|
|
|
|
->will($this->returnValue(array()));
|
|
|
|
|
|
|
|
$versionSelector = new VersionSelector($pool);
|
|
|
|
$best = $versionSelector->findBestCandidate('foobaz');
|
|
|
|
$this->assertFalse($best, 'No versions are available returns false');
|
|
|
|
}
|
|
|
|
|
2014-09-12 15:23:20 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getRecommendedRequireVersionPackages
|
|
|
|
*/
|
2014-09-23 14:19:23 +00:00
|
|
|
public function testFindRecommendedRequireVersion($prettyVersion, $isDev, $stability, $expectedVersion, $branchAlias = null)
|
2014-09-12 15:23:20 +00:00
|
|
|
{
|
|
|
|
$pool = $this->createMockPool();
|
|
|
|
$versionSelector = new VersionSelector($pool);
|
2014-09-23 14:19:23 +00:00
|
|
|
$versionParser = new VersionParser();
|
2014-09-12 15:23:20 +00:00
|
|
|
|
|
|
|
$package = $this->getMock('\Composer\Package\PackageInterface');
|
2016-11-25 07:28:49 +00:00
|
|
|
$package
|
|
|
|
->expects($this->any())
|
2014-09-12 15:23:20 +00:00
|
|
|
->method('getPrettyVersion')
|
|
|
|
->will($this->returnValue($prettyVersion));
|
2016-11-25 07:28:49 +00:00
|
|
|
$package
|
|
|
|
->expects($this->any())
|
2014-09-23 14:19:23 +00:00
|
|
|
->method('getVersion')
|
|
|
|
->will($this->returnValue($versionParser->normalize($prettyVersion)));
|
2016-11-25 07:28:49 +00:00
|
|
|
$package
|
|
|
|
->expects($this->any())
|
2014-09-12 15:23:20 +00:00
|
|
|
->method('isDev')
|
|
|
|
->will($this->returnValue($isDev));
|
2016-11-25 07:28:49 +00:00
|
|
|
$package
|
|
|
|
->expects($this->any())
|
2014-09-12 15:23:20 +00:00
|
|
|
->method('getStability')
|
|
|
|
->will($this->returnValue($stability));
|
2016-11-25 07:28:49 +00:00
|
|
|
$package
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getTransportOptions')
|
|
|
|
->will($this->returnValue(array()));
|
2014-09-12 15:23:20 +00:00
|
|
|
|
2014-09-23 14:19:23 +00:00
|
|
|
$branchAlias = $branchAlias === null ? array() : array('branch-alias' => array($prettyVersion => $branchAlias));
|
|
|
|
$package->expects($this->any())
|
|
|
|
->method('getExtra')
|
|
|
|
->will($this->returnValue($branchAlias));
|
|
|
|
|
2014-09-12 15:23:20 +00:00
|
|
|
$recommended = $versionSelector->findRecommendedRequireVersion($package);
|
|
|
|
|
|
|
|
// assert that the recommended version is what we expect
|
2015-10-27 14:43:04 +00:00
|
|
|
$this->assertSame($expectedVersion, $recommended);
|
2014-09-12 15:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getRecommendedRequireVersionPackages()
|
|
|
|
{
|
|
|
|
return array(
|
2014-09-23 14:19:23 +00:00
|
|
|
// real version, is dev package, stability, expected recommendation, [branch-alias]
|
2015-04-29 22:53:40 +00:00
|
|
|
array('1.2.1', false, 'stable', '^1.2'),
|
|
|
|
array('1.2', false, 'stable', '^1.2'),
|
|
|
|
array('v1.2.1', false, 'stable', '^1.2'),
|
|
|
|
array('3.1.2-pl2', false, 'stable', '^3.1'),
|
|
|
|
array('3.1.2-patch', false, 'stable', '^3.1'),
|
|
|
|
array('2.0-beta.1', false, 'beta', '^2.0@beta'),
|
|
|
|
array('3.1.2-alpha5', false, 'alpha', '^3.1@alpha'),
|
|
|
|
array('3.0-RC2', false, 'RC', '^3.0@RC'),
|
2014-12-08 16:27:47 +00:00
|
|
|
array('0.1.0', false, 'stable', '^0.1.0'),
|
|
|
|
array('0.1.3', false, 'stable', '^0.1.3'),
|
|
|
|
array('0.0.3', false, 'stable', '^0.0.3'),
|
|
|
|
array('0.0.3-alpha', false, 'alpha', '^0.0.3@alpha'),
|
2014-09-23 14:19:23 +00:00
|
|
|
// date-based versions are not touched at all
|
|
|
|
array('v20121020', false, 'stable', 'v20121020'),
|
|
|
|
array('v20121020.2', false, 'stable', 'v20121020.2'),
|
|
|
|
// dev packages without alias are not touched at all
|
2014-09-12 15:23:20 +00:00
|
|
|
array('dev-master', true, 'dev', 'dev-master'),
|
|
|
|
array('3.1.2-dev', true, 'dev', '3.1.2-dev'),
|
2014-09-23 14:19:23 +00:00
|
|
|
// dev packages with alias inherit the alias
|
2015-04-29 22:53:40 +00:00
|
|
|
array('dev-master', true, 'dev', '^2.1@dev', '2.1.x-dev'),
|
|
|
|
array('dev-master', true, 'dev', '^2.1@dev', '2.1-dev'),
|
|
|
|
array('dev-master', true, 'dev', '^2.1@dev', '2.1.3.x-dev'),
|
|
|
|
array('dev-master', true, 'dev', '^2.0@dev', '2.x-dev'),
|
2014-12-08 16:27:47 +00:00
|
|
|
array('dev-master', true, 'dev', '^0.3.0@dev', '0.3.x-dev'),
|
|
|
|
array('dev-master', true, 'dev', '^0.0.3@dev', '0.0.3.x-dev'),
|
2014-12-01 05:09:35 +00:00
|
|
|
// numeric alias
|
2015-04-29 22:53:40 +00:00
|
|
|
array('3.x-dev', true, 'dev', '^3.0@dev', '3.0.x-dev'),
|
|
|
|
array('3.x-dev', true, 'dev', '^3.0@dev', '3.0-dev'),
|
2014-09-12 15:23:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-27 14:43:04 +00:00
|
|
|
private function createPackage($version)
|
2014-07-15 02:25:44 +00:00
|
|
|
{
|
2015-10-27 14:43:04 +00:00
|
|
|
$parser = new VersionParser();
|
2015-11-21 19:28:10 +00:00
|
|
|
|
2015-10-27 14:43:04 +00:00
|
|
|
return new Package('foo', $parser->normalize($version), $version);
|
2014-07-15 02:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function createMockPool()
|
|
|
|
{
|
|
|
|
return $this->getMock('Composer\DependencyResolver\Pool', array(), array(), '', true);
|
|
|
|
}
|
|
|
|
}
|