View source
<?php
namespace Drupal\Tests\Component\Version;
use Drupal\Component\Version\Constraint;
use PHPUnit\Framework\TestCase;
class ConstraintTest extends TestCase {
public function testIsCompatible(Constraint $version_info, $current_version, $result) {
$this
->assertSame($result, $version_info
->isCompatible($current_version));
}
public function providerIsCompatible() {
$tests = [];
$tests['no-dependencies'] = [
new Constraint('', '8.x'),
'8.1.x',
TRUE,
];
$whitespace_variation_constraints = [
'>=1.0,<=2.0',
'>=1.0, <=2.0',
'>= 1.0,<= 2.0',
'>= 1.0, <= 2.0',
];
foreach ($whitespace_variation_constraints as $whitespace_variation_constraint) {
$tests += $this
->createTestsForVersions($whitespace_variation_constraint, [
'1.0',
'2.0',
'1.5',
], TRUE, '8.x');
$tests += $this
->createTestsForVersions($whitespace_variation_constraint, [
'0.9',
'2.1',
], FALSE, '8.x');
}
foreach ([
'',
'=',
'==',
] as $equal_operator) {
foreach ([
'!=',
'<>',
] as $not_equal_operator) {
foreach ([
'8.x',
'9.x',
'10.x',
] as $core_compatibility) {
if ($core_compatibility === '8.x') {
$constraint = "< 9.x-4.x, > 9.x-1.x";
}
else {
$constraint = "< 8.x-4.x, > 8.x-1.x";
}
$tests += $this
->createTestsForVersions($constraint, [
'4.0',
'3.9',
'2.1',
'1.9',
], FALSE, $core_compatibility);
$constraint = "{$equal_operator} {$core_compatibility}-1.0";
$tests += $this
->createTestsForVersions($constraint, [
'1.0',
], TRUE, $core_compatibility);
$tests += $this
->createTestsForVersions($constraint, [
'1.1',
'0.9',
], FALSE, $core_compatibility);
$constraint = "{$equal_operator} {$core_compatibility}-1.1-alpha12";
$tests += $this
->createTestsForVersions($constraint, [
'1.1-alpha12',
], TRUE, $core_compatibility);
$tests += $this
->createTestsForVersions($constraint, [
'1.1-alpha10',
'1.1-beta1',
], FALSE, $core_compatibility);
$constraint = "{$equal_operator} {$core_compatibility}-1.1-beta8";
$tests += $this
->createTestsForVersions($constraint, [
'1.1-beta8',
], TRUE, $core_compatibility);
$tests += $this
->createTestsForVersions($constraint, [
'1.1-beta4',
], FALSE, $core_compatibility);
$constraint = "{$equal_operator} {$core_compatibility}-1.1-rc11";
$tests += $this
->createTestsForVersions($constraint, [
'1.1-rc11',
], TRUE, $core_compatibility);
$tests += $this
->createTestsForVersions($constraint, [
'1.1-rc2',
], FALSE, $core_compatibility);
$constraint = "> {$core_compatibility}-1.x";
$tests += $this
->createTestsForVersions($constraint, [
'2.0',
], TRUE, $core_compatibility);
$tests += $this
->createTestsForVersions($constraint, [
'1.1',
'0.9',
], FALSE, $core_compatibility);
$tests += $this
->createTestsForVersions(">= {$core_compatibility}-1.0", [
'1.1',
'1.0',
], TRUE, $core_compatibility);
$tests += $this
->createTestsForVersions(">= {$core_compatibility}-1.1", [
'1.0',
], FALSE, $core_compatibility);
$constraint = "< {$core_compatibility}-1.1";
$tests += $this
->createTestsForVersions($constraint, [
'1.1',
], FALSE, $core_compatibility);
$tests += $this
->createTestsForVersions($constraint, [
'1.0',
], TRUE, $core_compatibility);
$tests += $this
->createTestsForVersions("< {$core_compatibility}-1.0", [
'1.1',
], FALSE, $core_compatibility);
$constraint = "<= {$core_compatibility}-1.x";
$tests += $this
->createTestsForVersions($constraint, [
'2.0',
], FALSE, $core_compatibility);
$tests += $this
->createTestsForVersions($constraint, [
'1.9',
'1.1',
'0.9',
], TRUE, $core_compatibility);
$constraint = "< {$core_compatibility}-4.x, > {$core_compatibility}-1.x";
$tests += $this
->createTestsForVersions($constraint, [
'4.0',
'1.9',
], FALSE, $core_compatibility);
$tests += $this
->createTestsForVersions($constraint, [
'3.9',
'2.1',
], TRUE, $core_compatibility);
$constraint = "< 4.x, > 1.x";
$tests += $this
->createTestsForVersions($constraint, [
'4.0',
'1.9',
], FALSE, $core_compatibility);
$tests += $this
->createTestsForVersions($constraint, [
'3.9',
'2.1',
], TRUE, $core_compatibility);
$constraint = "{$equal_operator} {$core_compatibility}-2.x, >= 2.4-alpha2";
$tests += $this
->createTestsForVersions($constraint, [
'2.4-beta3',
], TRUE, $core_compatibility);
$constraint = "{$equal_operator} {$core_compatibility}-2.0, >= 2.4-alpha2";
$tests += $this
->createTestsForVersions($constraint, [
'2.4-beta3',
], FALSE, $core_compatibility);
$constraint = "> {$core_compatibility}-4.x, < {$core_compatibility}-1.x";
$tests += $this
->createTestsForVersions($constraint, [
'4.0',
'3.9',
'2.1',
'1.9',
], FALSE, $core_compatibility);
$constraint = "{$equal_operator} {$core_compatibility}2.x, {$equal_operator} 2.4-beta3";
$tests += $this
->createTestsForVersions($constraint, [
'2.4-beta3',
], FALSE, $core_compatibility);
$constraint = "{$equal_operator} {$core_compatibility}-2.1, {$equal_operator} {$core_compatibility}-2.3,\"(> 1.0, <= 3.2, {$not_equal_operator} 3.0)-8.x.2.5";
$tests += $this
->createTestsForVersions($constraint, [
'2.1',
'2.2',
'2.3',
], FALSE, $core_compatibility);
$constraint = "> 1.0, <= 3.2, {$not_equal_operator} 3.0, {$not_equal_operator} 1.5, {$not_equal_operator} 2.7";
$tests += $this
->createTestsForVersions($constraint, [
'1.1',
'3.1',
'2.1',
], TRUE, $core_compatibility);
$tests += $this
->createTestsForVersions($constraint, [
'3.0',
'1.5',
'2.7',
'3.3',
], FALSE, $core_compatibility);
}
}
}
return $tests;
}
private function createTestsForVersions($constraint_string, array $versions, $expected_result, $core_compatibility = '8.x') {
$constraint = new Constraint($constraint_string, $core_compatibility);
$tests = [];
foreach ($versions as $version) {
$tests["{$core_compatibility}::({$constraint_string})::{$version}"] = [
$constraint,
$version,
$expected_result,
];
}
return $tests;
}
}