1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
<?php
/*
* This file is part of composer/semver.
*
* (c) Composer <https://github.com/composer>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Composer\Semver;
use PHPUnit\Framework\TestCase;
use Composer\Semver\Constraint\MatchNoneConstraint;
use Composer\Semver\Constraint\MatchAllConstraint;
class SubsetsTest extends TestCase
{
/**
* @dataProvider subsets
* @param string $aStr
* @param string $bStr
*/
public function testIsSubsetOf($aStr, $bStr)
{
$versionParser = new VersionParser;
$a = $versionParser->parseConstraints($aStr);
$b = $versionParser->parseConstraints($bStr);
$this->assertTrue(Intervals::isSubsetOf($a, $b), $aStr.' ('.$a.') should be seen as a subset of '.$bStr.' ('.$b.')');
}
/**
* @return array<mixed>
*/
public static function subsets()
{
return array(
// x is subset of y
array('*', '*'),
array('*', '!= 1 || == 1'),
array('1.0.0', '*'),
array('1.0.*', '*'),
array('^1.0 || ^2.0', '*'),
array('^3.0', '^3.2 || *'),
array('^1.0 || ^2.0', '^1.0 || ^2.0'),
array('^1.0 || ^2.0', '^1.0 || ^2.0 || ^4.0'),
array('^1.0 || ^2.1', '^1.0 || ^2.1 || ^4.0'),
array('^1.2', '^1.0 || ^2.0'),
array('1.2.3', '^1.0 || ^2.0'),
array('2.0.0-dev', '^1.0 || ^2.0'),
array('>= 2.1.0', '>= 2.0.0'),
array('^2.0', '<3.0.0'),
array('^3.0', '> 2.1.3'),
array('3.0.0', '<= 3.0.0'),
array('!= 3.0.0', '*'),
array('!= 3.0.0', '!= 3.0'),
array('!= 3.0, != 2.0', '!= 2.0, != 3.0'),
array('>3', '^2 || ^3 || >=4'),
array('>3', '>=3'),
array('<3', '<=3'),
array('= dev-foo', '= dev-foo'),
array('!= dev-foo', '!= dev-foo'),
array('< dev-foo', '= dev-foo'), // invalid range matches nothing so is a subset of any other
array('1.5.*', '^1.4'),
array('1.5.*', '1.3 - 1.6 || 1.8 - 1.9'),
array('1.3.2', '1.3.0 || 1.3.1 || 1.3.2'),
array('1.3.1', '1.3.0 || 1.3.1 || 1.3.2'),
array('1.3.1 || 1.3.1', '1.3.1'),
array('^1.0 || ^3.2', '^1.0 || ^3.0'),
array('^1.3 || ^3.2', '>1.2'),
array('^1.6', '<1.3 || >1.5'),
array('>1.6', '<1.3 || >1.5'),
array('>1.6', '>1.5, >1.4, !=1.1'),
array('>1.6', '>1.5 || >1.7'),
array('^1.1', '> 1.0.0'),
array('^1.1, !=1.5.0', '> 1.0.0'),
array('^1.1, !=0.5.0', '> 1.0.0'),
array('^2.0 || dev-foo', '> 1.0 || dev-foo || dev-bar'),
array('^1.0, ^1.2', '>=1.2'),
array('^1.0, ^1.2', '^1.2'),
array('^1.0, ^1.2 || ^1.3', '^1.2'),
);
}
/**
* @dataProvider notSubsets
* @param string $aStr
* @param string $bStr
*/
public function testIsNotSubsetOf($aStr, $bStr)
{
$versionParser = new VersionParser;
$a = $versionParser->parseConstraints($aStr);
$b = $versionParser->parseConstraints($bStr);
$this->assertFalse(Intervals::isSubsetOf($a, $b), $aStr.' ('.$a.') should not be seen as a subset of '.$bStr.' ('.$b.')');
}
/**
* @return array<mixed>
*/
public static function notSubsets()
{
return array(
// x is subset of y
array('*', '>= 1 || < 1'), // it is a subset of the numeric interval, but * allows dev- branches while the latter does not
array('*', '1.0.0'),
array('*', '1.0.*'),
array('*', '^1.0 || ^2.0'),
array('^1.0 || ^2.0', '^1.0, ^2.0'), // buggy constraint on the right here, checking it does not match
array('^1.0 || ^2.0', '^1.2'),
array('^1.0 || ^2.0', '^1.0'),
array('^1.0 || ^2.0', '1.2.3'),
array('^1.0 || ^3.0', '1.2.3'),
array('3.0.0', '^1.0 || ^2.0'),
array('3.0.0', '< 3.0.0'),
array('3.0.0', '>= 3.0.1'),
array('!= 3.0.0', '> 3.0.0 || < 3.0.0-stable'), // it is a subset of the numeric interval, but != x allows dev- branches while the right side does not
array('!= 3.0.0-dev', '^2.0 || <2 || >3.0-dev'), // it is a subset of the numeric interval, but != x allows dev- branches while the right side does not
array('!= 3.0.0', '= 3.0.0'),
array('!= 3.0.0', '!= 3.0.1'),
array('!= 3.0.0', 'dev-foo || dev-bar'),
array('!= 3.0.0', '<dev-foo || >dev-bar'),
array('>= 1.0.0', '= 1.2.3'),
array('< 2.0.0', '= 1.2.3'),
array('>3', '^2 || ^3 || >4'),
array('>=3', '>3'),
array('<=3', '<3'),
array('^2.1', '^2.0, !=2.1.3'),
array('<2.0', '>=1.1'),
array('!= dev-foo', '!= dev-bar'),
array('!= dev-foo', '= dev-bar'),
array('1.3.3', '1.3.0 || 1.3.1 || 1.3.2'),
array('1.3.1 || 1.3.2', '1.3.1'),
array('>1.6', '>1.5, >1.4, !=1.7'),
array('>1.6', '>1.5, >1.7'),
array('^1.0 || ^3.2', '^1.2 || ^3.0'),
array('^1.0 || ^3.2', '^3.0'),
array('^1.3 || ^3.2', '>1.4'),
array('^2.0 || dev-foo', '> 1.0 || dev-bar'),
);
}
public function testMatchNoneIsNoSubsetNorSupersetExceptOfMatchAll()
{
$versionParser = new VersionParser;
$matchNone = new MatchNoneConstraint;
$notSubsets = array(
'1.0.0',
'^1.0',
'>3',
'<3',
'dev-foo',
'!= 1',
'!= dev-foo',
'<= dev-foo',
);
foreach ($notSubsets as $constraint) {
$c = $versionParser->parseConstraints($constraint);
$this->assertFalse(Intervals::isSubsetOf($c, $matchNone), $constraint.' ('.$c.') should not be seen as a subset of '.$matchNone);
$this->assertFalse(Intervals::isSubsetOf($matchNone, $c), $matchNone.' should not be seen as a subset of '.$constraint.' ('.$c.')');
}
$empty = new MatchAllConstraint;
$this->assertFalse(Intervals::isSubsetOf($empty, $matchNone), $empty.' should not be seen as a subset of '.$matchNone);
$this->assertTrue(Intervals::isSubsetOf($matchNone, $empty), $matchNone.' should be seen as a subset of '.$empty);
}
}
|