1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of semver/semver. |
5
|
|
|
* |
6
|
|
|
* (c) SemVer <https://github.com/git-pull-request> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace SemVer\SemVer\Tests; |
15
|
|
|
|
16
|
|
|
use PHPUnit_Framework_TestCase; |
17
|
|
|
use SemVer\SemVer\Version; |
18
|
|
|
use SemVer\SemVer\VersionComparator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Test of VersionComparator object. |
22
|
|
|
*/ |
23
|
|
|
final class VersionComparatorTest extends PHPUnit_Framework_TestCase |
24
|
|
|
{ |
25
|
|
|
//////////////////////////////////////////////////////////////////////////// |
26
|
|
|
// ::compare() |
27
|
|
|
//////////////////////////////////////////////////////////////////////////// |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @dataProvider provideCompareVersions |
31
|
|
|
* |
32
|
|
|
* @param Version $version1 |
33
|
|
|
* @param Version $version2 |
34
|
|
|
* @param int $expectedResult |
35
|
|
|
* @param string $message |
36
|
|
|
*/ |
37
|
|
|
public function testCompare(Version $version1, Version $version2, int $expectedResult, string $message) |
38
|
|
|
{ |
39
|
|
|
static::assertEquals($expectedResult, VersionComparator::compare($version1, $version2), $message); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function provideCompareVersions() : array |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
// major |
49
|
|
|
[ |
50
|
|
|
Version::fromString('1.0.0'), |
51
|
|
|
Version::fromString('2.0.0'), |
52
|
|
|
-1, |
53
|
|
|
'::compare() major version. 1.0.0 is lower than 2.0.0', |
54
|
|
|
], |
55
|
|
|
[ |
56
|
|
|
Version::fromString('2.0.0'), |
57
|
|
|
Version::fromString('1.0.0'), |
58
|
|
|
1, |
59
|
|
|
'::compare() major version. 2.0.0 is greater than 1.0.0', |
60
|
|
|
], |
61
|
|
|
[ |
62
|
|
|
Version::fromString('10.0.0'), |
63
|
|
|
Version::fromString('2.0.0'), |
64
|
|
|
1, |
65
|
|
|
'::compare() major version compares numerically. 10.0.0 is greater than 2.0.0', |
66
|
|
|
], |
67
|
|
|
// minor |
68
|
|
|
[ |
69
|
|
|
Version::fromString('2.0.0'), |
70
|
|
|
Version::fromString('2.10.0'), |
71
|
|
|
-1, |
72
|
|
|
'::compare() minor version. 2.0.0 is lower than 2.10.0', |
73
|
|
|
], |
74
|
|
|
[ |
75
|
|
|
Version::fromString('2.10.0'), |
76
|
|
|
Version::fromString('2.0.0'), |
77
|
|
|
1, |
78
|
|
|
'::compare() minor version. 2.10.0 is greater than 2.0.0', |
79
|
|
|
], |
80
|
|
|
[ |
81
|
|
|
Version::fromString('2.10.0'), |
82
|
|
|
Version::fromString('2.2.0'), |
83
|
|
|
1, |
84
|
|
|
'::compare() minor version compares numerically. 2.10.0 is greater than 2.2.0', |
85
|
|
|
], |
86
|
|
|
// patch |
87
|
|
|
[ |
88
|
|
|
Version::fromString('2.0.0'), |
89
|
|
|
Version::fromString('2.0.10'), |
90
|
|
|
-1, |
91
|
|
|
'::compare() patch version. 2.0.0 is lower than 2.0.10', |
92
|
|
|
], |
93
|
|
|
[ |
94
|
|
|
Version::fromString('2.0.10'), |
95
|
|
|
Version::fromString('2.0.0'), |
96
|
|
|
1, |
97
|
|
|
'::compare() patch version. 2.0.10 is greater than 2.0.0', |
98
|
|
|
], |
99
|
|
|
[ |
100
|
|
|
Version::fromString('2.0.10'), |
101
|
|
|
Version::fromString('2.0.2'), |
102
|
|
|
1, |
103
|
|
|
'::compare() patch version. 2.0.10 is greater than 2.0.2', |
104
|
|
|
], |
105
|
|
|
[ |
106
|
|
|
Version::fromString('2.0.0'), |
107
|
|
|
Version::fromString('2.0.0+build'), |
108
|
|
|
0, |
109
|
|
|
'::compare() build differs. versions should be equals.', |
110
|
|
|
], |
111
|
|
|
// pre release |
112
|
|
|
[ |
113
|
|
|
Version::fromString('2.0.0'), |
114
|
|
|
Version::fromString('2.0.0-alpha'), |
115
|
|
|
1, |
116
|
|
|
'::compare() second version has a pre-release, the first not', |
117
|
|
|
], |
118
|
|
|
[ |
119
|
|
|
Version::fromString('2.0.0-alpha'), |
120
|
|
|
Version::fromString('2.0.0'), |
121
|
|
|
-1, |
122
|
|
|
'::compare() first version has a pre-release, the second not', |
123
|
|
|
], |
124
|
|
|
[ |
125
|
|
|
Version::fromString('2.0.0-alpha.1'), |
126
|
|
|
Version::fromString('2.0.0-alpha'), |
127
|
|
|
1, |
128
|
|
|
'::compare() first has two pre-release identifiers, second only has one', |
129
|
|
|
], |
130
|
|
|
[ |
131
|
|
|
Version::fromString('2.0.0-alpha'), |
132
|
|
|
Version::fromString('2.0.0-alpha.1'), |
133
|
|
|
-1, |
134
|
|
|
'::compare() second has two pre-release identifiers, first only has one', |
135
|
|
|
], |
136
|
|
|
[ |
137
|
|
|
Version::fromString('2.0.0-1'), |
138
|
|
|
Version::fromString('2.0.0-beta'), |
139
|
|
|
-1, |
140
|
|
|
'::compare() a numeric identifier is lower than an alphabetical one', |
141
|
|
|
], |
142
|
|
|
[ |
143
|
|
|
Version::fromString('2.0.0-beta'), |
144
|
|
|
Version::fromString('2.0.0-1'), |
145
|
|
|
1, |
146
|
|
|
'::compare() an alphabetical identifier is greater than a numeric one', |
147
|
|
|
], |
148
|
|
|
[ |
149
|
|
|
Version::fromString('2.0.0-alpha.1'), |
150
|
|
|
Version::fromString('2.0.0-alpha.beta'), |
151
|
|
|
-1, |
152
|
|
|
'::compare() a numeric identifier is lower than an alphabetical one even when multiple identifiers given', |
153
|
|
|
], |
154
|
|
|
[ |
155
|
|
|
Version::fromString('2.0.0-alpha.10'), |
156
|
|
|
Version::fromString('2.0.0-alpha.2'), |
157
|
|
|
1, |
158
|
|
|
'::compare() an alphabetical identifier is greater than a numeric one even when multiple identifiers given', |
159
|
|
|
], |
160
|
|
|
[ |
161
|
|
|
Version::fromString('2.0.0-alpha+build127'), |
162
|
|
|
Version::fromString('2.0.0-alpha+build128'), |
163
|
|
|
0, |
164
|
|
|
'::compare() two versions that only differs with their build are equals.', |
165
|
|
|
], |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|