Chris@18
|
1 <?php
|
Chris@18
|
2
|
Chris@18
|
3 namespace Drupal\Tests\Component\Version;
|
Chris@18
|
4
|
Chris@18
|
5 use Drupal\Component\Version\Constraint;
|
Chris@18
|
6 use PHPUnit\Framework\TestCase;
|
Chris@18
|
7
|
Chris@18
|
8 /**
|
Chris@18
|
9 * @coversDefaultClass \Drupal\Component\Version\Constraint
|
Chris@18
|
10 * @group Version
|
Chris@18
|
11 */
|
Chris@18
|
12 class ConstraintTest extends TestCase {
|
Chris@18
|
13
|
Chris@18
|
14 /**
|
Chris@18
|
15 * @covers ::isCompatible
|
Chris@18
|
16 * @dataProvider providerIsCompatible
|
Chris@18
|
17 */
|
Chris@18
|
18 public function testIsCompatible(Constraint $version_info, $current_version, $result) {
|
Chris@18
|
19 $this->assertSame($result, $version_info->isCompatible($current_version));
|
Chris@18
|
20 }
|
Chris@18
|
21
|
Chris@18
|
22 /**
|
Chris@18
|
23 * Provider for testIsCompatible.
|
Chris@18
|
24 */
|
Chris@18
|
25 public function providerIsCompatible() {
|
Chris@18
|
26 $tests = [];
|
Chris@18
|
27
|
Chris@18
|
28 $tests['no-dependencies'] = [new Constraint('', '8.x'), '8.1.x', TRUE];
|
Chris@18
|
29
|
Chris@18
|
30 // Stable version.
|
Chris@18
|
31 $stable = new Constraint('8.x-1.0', '8.x');
|
Chris@18
|
32 $tests['(=8.x-1.0)-1.0'] = [$stable, '1.0', TRUE];
|
Chris@18
|
33 $tests['(=8.x-1.0)-1.1'] = [$stable, '1.1', FALSE];
|
Chris@18
|
34 $tests['(=8.x-1.0)-0.9'] = [$stable, '0.9', FALSE];
|
Chris@18
|
35
|
Chris@18
|
36 // Alpha version.
|
Chris@18
|
37 $alpha = new Constraint('8.x-1.1-alpha12', '8.x');
|
Chris@18
|
38 $tests['(8.x-1.1-alpha12)-alpha12'] = [$alpha, '1.1-alpha12', TRUE];
|
Chris@18
|
39 $tests['(8.x-1.1-alpha12)-alpha10'] = [$alpha, '1.1-alpha10', FALSE];
|
Chris@18
|
40 $tests['(8.x-1.1-alpha12)-beta1'] = [$alpha, '1.1-beta1', FALSE];
|
Chris@18
|
41
|
Chris@18
|
42 // Beta version.
|
Chris@18
|
43 $beta = new Constraint('8.x-1.1-beta8', '8.x');
|
Chris@18
|
44 $tests['(8.x-1.1-beta8)-beta8'] = [$beta, '1.1-beta8', TRUE];
|
Chris@18
|
45 $tests['(8.x-1.1-beta8)-beta4'] = [$beta, '1.1-beta4', FALSE];
|
Chris@18
|
46
|
Chris@18
|
47 // RC version.
|
Chris@18
|
48 $rc = new Constraint('8.x-1.1-rc11', '8.x');
|
Chris@18
|
49 $tests['(8.x-1.1-rc11)-rc11'] = [$rc, '1.1-rc11', TRUE];
|
Chris@18
|
50 $tests['(8.x-1.1-rc11)-rc2'] = [$rc, '1.1-rc2', FALSE];
|
Chris@18
|
51
|
Chris@18
|
52 // Test greater than.
|
Chris@18
|
53 $greater = new Constraint('>8.x-1.x', '8.x');
|
Chris@18
|
54 $tests['(>8.x-1.x)-2.0'] = [$greater, '2.0', TRUE];
|
Chris@18
|
55 $tests['(>8.x-1.x)-1.1'] = [$greater, '1.1', FALSE];
|
Chris@18
|
56 $tests['(>8.x-1.x)-0.9'] = [$greater, '0.9', FALSE];
|
Chris@18
|
57
|
Chris@18
|
58 // Test greater than or equal.
|
Chris@18
|
59 $greater_or_equal = new Constraint('>=8.x-1.0', '8.x');
|
Chris@18
|
60 $tests['(>=8.x-1.0)-1.1'] = [$greater_or_equal, '1.1', TRUE];
|
Chris@18
|
61 $tests['(>=8.x-1.0)-1.0'] = [$greater_or_equal, '1.0', TRUE];
|
Chris@18
|
62 $tests['(>=8.x-1.1)-1.0'] = [new Constraint('>=8.x-1.1', '8.x'), '1.0', FALSE];
|
Chris@18
|
63
|
Chris@18
|
64 // Test less than.
|
Chris@18
|
65 $less = new Constraint('<8.x-1.1', '8.x');
|
Chris@18
|
66 $tests['(<8.x-1.1)-1.1'] = [$less, '1.1', FALSE];
|
Chris@18
|
67 $tests['(<8.x-1.1)-1.1'] = [$less, '1.0', TRUE];
|
Chris@18
|
68 $tests['(<8.x-1.0)-1.0'] = [new Constraint('<8.x-1.0', '8.x'), '1.1', FALSE];
|
Chris@18
|
69
|
Chris@18
|
70 // Test less than or equal.
|
Chris@18
|
71 $less_or_equal = new Constraint('<= 8.x-1.x', '8.x');
|
Chris@18
|
72 $tests['(<= 8.x-1.x)-2.0'] = [$less_or_equal, '2.0', FALSE];
|
Chris@18
|
73 $tests['(<= 8.x-1.x)-1.9'] = [$less_or_equal, '1.9', TRUE];
|
Chris@18
|
74 $tests['(<= 8.x-1.x)-1.1'] = [$less_or_equal, '1.1', TRUE];
|
Chris@18
|
75 $tests['(<= 8.x-1.x)-0.9'] = [$less_or_equal, '0.9', TRUE];
|
Chris@18
|
76
|
Chris@18
|
77 // Test greater than and less than.
|
Chris@18
|
78 $less_and_greater = new Constraint('<8.x-4.x,>8.x-1.x', '8.x');
|
Chris@18
|
79 $tests['(<8.x-4.x,>8.x-1.x)-4.0'] = [$less_and_greater, '4.0', FALSE];
|
Chris@18
|
80 $tests['(<8.x-4.x,>8.x-1.x)-3.9'] = [$less_and_greater, '3.9', TRUE];
|
Chris@18
|
81 $tests['(<8.x-4.x,>8.x-1.x)-2.1'] = [$less_and_greater, '2.1', TRUE];
|
Chris@18
|
82 $tests['(<8.x-4.x,>8.x-1.x)-1.9'] = [$less_and_greater, '1.9', FALSE];
|
Chris@18
|
83
|
Chris@18
|
84 // Test a nonsensical greater than and less than - no compatible versions.
|
Chris@18
|
85 $less_and_greater = new Constraint('>8.x-4.x,<8.x-1.x', '8.x');
|
Chris@18
|
86 $tests['(<8.x-4.x,>8.x-1.x)-4.0'] = [$less_and_greater, '4.0', FALSE];
|
Chris@18
|
87 $tests['(<8.x-4.x,>8.x-1.x)-3.9'] = [$less_and_greater, '3.9', FALSE];
|
Chris@18
|
88 $tests['(<8.x-4.x,>8.x-1.x)-2.1'] = [$less_and_greater, '2.1', FALSE];
|
Chris@18
|
89 $tests['(<8.x-4.x,>8.x-1.x)-1.9'] = [$less_and_greater, '1.9', FALSE];
|
Chris@18
|
90
|
Chris@18
|
91 return $tests;
|
Chris@18
|
92 }
|
Chris@18
|
93
|
Chris@18
|
94 /**
|
Chris@18
|
95 * @covers ::toArray
|
Chris@18
|
96 * @group legacy
|
Chris@18
|
97 * @expectedDeprecation Drupal\Component\Version\Constraint::toArray() only exists to provide a backwards compatibility layer. See https://www.drupal.org/node/2756875
|
Chris@18
|
98 */
|
Chris@18
|
99 public function testToArray() {
|
Chris@18
|
100 $constraint = new Constraint('<8.x-4.x,>8.x-1.x', '8.x');
|
Chris@18
|
101 $this->assertSame([
|
Chris@18
|
102 ['op' => '<', 'version' => '4.x'],
|
Chris@18
|
103 ['op' => '>', 'version' => '2.x'],
|
Chris@18
|
104 ], $constraint->toArray());
|
Chris@18
|
105 }
|
Chris@18
|
106
|
Chris@18
|
107 }
|