Completed
Pull Request — master (#2)
by Julien
02:17
created

VersionComparatorTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 0
loc 145
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testCompare() 0 4 1
B provideCompareVersions() 0 122 1
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
final class VersionComparatorTest extends PHPUnit_Framework_TestCase
21
{
22
    ////////////////////////////////////////////////////////////////////////////
23
    // compare()
24
    ////////////////////////////////////////////////////////////////////////////
25
26
    /**
27
     * @dataProvider provideCompareVersions
28
     *
29
     * @param Version $version1
30
     * @param Version $version2
31
     * @param int     $expectedResult
32
     * @param string  $message
33
     */
34
    public function testCompare(Version $version1, Version $version2, int $expectedResult, string $message)
35
    {
36
        static::assertEquals($expectedResult, VersionComparator::compare($version1, $version2), $message);
37
    }
38
39
    /**
40
     * @return array
41
     */
42
    public function provideCompareVersions() : array
43
    {
44
        return [
45
            // major
46
            [
47
                Version::fromString('1.0.0'),
48
                Version::fromString('2.0.0'),
49
                -1,
50
                '::compare() versions must be ordered by major version (current lower than other)',
51
            ],
52
            [
53
                Version::fromString('2.0.0'),
54
                Version::fromString('1.0.0'),
55
                1,
56
                '::compare() versions must be ordered by major version (current greater than other)',
57
            ],
58
            [
59
                Version::fromString('2.0.0'),
60
                Version::fromString('10.0.0'),
61
                -1,
62
                '::compare() versions must be ordered by major version numerically',
63
            ],
64
            // minor
65
            [
66
                Version::fromString('2.10.0'),
67
                Version::fromString('2.0.0'),
68
                1,
69
                '::compare() if major versions are equals, then it must be ordered by minor version (current lower than other)',
70
            ],
71
            [
72
                Version::fromString('2.0.0'),
73
                Version::fromString('2.10.0'),
74
                -1,
75
                '::compare() if major versions are equals, then it must be ordered by minor version (current greater than other)',
76
            ],
77
            [
78
                Version::fromString('2.10.0'),
79
                Version::fromString('2.2.0'),
80
                1,
81
                '::compare() if major versions are equals, then it must be ordered by minor version numerically',
82
            ],
83
            // patch
84
            [
85
                Version::fromString('2.0.10'),
86
                Version::fromString('2.0.0'),
87
                1,
88
                '::compare() if major and minor versions are equals, then it must be ordered by patch version numerically (current lower than other)',
89
            ],
90
            [
91
                Version::fromString('2.0.0'),
92
                Version::fromString('2.0.10'),
93
                -1,
94
                '::compare() if major and minor versions are equals, then it must be ordered by patch version numerically (current greater than other)',
95
            ],
96
            [
97
                Version::fromString('2.0.10'),
98
                Version::fromString('2.0.2'),
99
                1,
100
                '::compare() if major and minor versions are equals, then it must be ordered by patch version numerically',
101
            ],
102
            [
103
                Version::fromString('2.0.0'),
104
                Version::fromString('2.0.0+build'),
105
                0,
106
                '::compare() if major, minor and patch versions are equals and both versions do not have pre-release, then they are equals',
107
            ],
108
            [
109
                Version::fromString('2.0.0'),
110
                Version::fromString('2.0.0-alpha'),
111
                1,
112
                '::compare() When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version. (current without pre-release)',
113
            ],
114
            [
115
                Version::fromString('2.0.0-alpha'),
116
                Version::fromString('2.0.0'),
117
                -1,
118
                '::compare() A larger set of pre-release fields has a higher precedence than a smaller set',
119
            ],
120
            [
121
                Version::fromString('2.0.0-alpha.1'),
122
                Version::fromString('2.0.0-alpha'),
123
                1,
124
                '::compare() A larger set of pre-release fields has a higher precedence than a smaller set (multiple level)',
125
            ],
126
            [
127
                Version::fromString('2.0.0-alpha'),
128
                Version::fromString('2.0.0-alpha.1'),
129
                -1,
130
                '::compare() A larger set of pre-release fields has a higher precedence than a smaller set (multiple level)',
131
            ],
132
            [
133
                Version::fromString('2.0.0-1'),
134
                Version::fromString('2.0.0-beta'),
135
                -1,
136
                '::compare() Precedence for two pre-release versions with the same major, minor, and patch version. Numeric identifiers always have lower precedence than non-numeric identifiers',
137
            ],
138
            [
139
                Version::fromString('2.0.0-beta'),
140
                Version::fromString('2.0.0-1'),
141
                1,
142
                '::compare() Precedence for two pre-release versions with the same major, minor, and patch version. Numeric identifiers always have lower precedence than non-numeric identifiers',
143
            ],
144
            [
145
                Version::fromString('2.0.0-alpha.1'),
146
                Version::fromString('2.0.0-alpha.beta'),
147
                -1,
148
                '::compare() Precedence for two pre-release versions with the same major, minor, and patch version. Numeric identifiers always have lower precedence than non-numeric identifiers. Test with multiple identifiers level.',
149
            ],
150
            [
151
                Version::fromString('2.0.0-alpha.10'),
152
                Version::fromString('2.0.0-alpha.2'),
153
                1,
154
                '::compare() numeric pre-release, minor, and patch version. Numeric identifiers always have lower precedence than non-numeric identifiers. Test with multiple identifiers level.',
155
            ],
156
            [
157
                Version::fromString('2.0.0-alpha+build127'),
158
                Version::fromString('2.0.0-alpha+build128'),
159
                0,
160
                '::compare() numeric pre-release, minor, and patch version. Numeric identifiers always have lower precedence than non-numeric identifiers. Test with multiple identifiers level.',
161
            ],
162
        ];
163
    }
164
}
165