Completed
Push — master ( 9aa0e4...520f00 )
by Julien
02:19
created

VersionTest::provideCompareVersions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 110
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 110
rs 8.2857
cc 1
eloc 70
nc 1
nop 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A VersionTest::testGreaterThanOrEqual() 7 7 1
A VersionTest::testLessThan() 7 7 1
A VersionTest::testLessThanOrEqual() 7 7 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
19
final class VersionTest extends PHPUnit_Framework_TestCase
20
{
21
    ////////////////////////////////////////////////////////////////////////////
22
    // __construct()
23
    ////////////////////////////////////////////////////////////////////////////
24
25
    /**
26
     * @dataProvider provideValidVersion
27
     *
28
     * @param string $unused
29
     * @param int    $major
30
     * @param int    $minor
31
     * @param int    $patch
32
     * @param string $preRelease
33
     * @param string $build
34
     */
35
    public function testConstructor(
36
        string $unused,
0 ignored issues
show
Unused Code introduced by
The parameter $unused is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
        int $major,
38
        int $minor,
39
        int $patch,
40
        string $preRelease,
41
        string $build
42
    ) {
43
        $version = new Version($major, $minor, $patch, $preRelease, $build);
44
        static::assertEquals(
45
            $major,
46
            $version->getMajor(),
47
            '__construct() takes the major version as its first argument'
48
        );
49
        static::assertEquals(
50
            $minor,
51
            $version->getMinor(),
52
            '__construct() takes the minor version as its second argument'
53
        );
54
        static::assertEquals(
55
            $patch,
56
            $version->getPatch(),
57
            '__construct() takes the patch version as its third argument'
58
        );
59
        static::assertEquals(
60
            $preRelease,
61
            $version->getPreRelease(),
62
            '__construct() takes the pre-release version as its forth argument'
63
        );
64
        static::assertEquals(
65
            $build,
66
            $version->getBuild(),
67
            '__construct() takes the build version as its fifth argument'
68
        );
69
    }
70
71
    /**
72
     * @dataProvider             provideWrongPreReleaseVersion
73
     * @expectedException \SemVer\SemVer\Exception\InvalidArgumentException
74
     * @expectedExceptionMessage The pre-release version is not compatible with rule 9 of the specifications.
75
     *
76
     * @param string $preRelease
77
     */
78
    public function testConstructorFailsIfPreReleaseNotValid(string $preRelease)
79
    {
80
        new Version(10, 12, 14, $preRelease);
81
    }
82
83
    /**
84
     * @dataProvider             provideWrongPreReleaseVersion
85
     * @expectedException \SemVer\SemVer\Exception\InvalidArgumentException
86
     * @expectedExceptionMessage The build version is not compatible with rule 10 of the specifications.
87
     *
88
     * @param string $build
89
     */
90
    public function testConstructorFailsIfBuildNotValid(string $build)
91
    {
92
        new Version(10, 12, 14, '', $build);
93
    }
94
95
    /** @return array */
96
    public function provideWrongPreReleaseVersion() : array
97
    {
98
        return [
99
            ['é'],
100
            ['"'],
101
            ['~'],
102
            ['+'],
103
            ['+R'],
104
            ['R+'],
105
            ['.RC'],
106
            ['.'],
107
            ['RC.'],
108
        ];
109
    }
110
111
    ////////////////////////////////////////////////////////////////////////////
112
    // fromString()
113
    ////////////////////////////////////////////////////////////////////////////
114
115
    /**
116
     * @dataProvider             provideValidVersion
117
     *
118
     * @param string $string
119
     * @param int    $expectedMajor
120
     * @param int    $expectedMinor
121
     * @param int    $expectedPatch
122
     * @param string $expectedPreRelease
123
     * @param string $exceptedBuild
124
     */
125
    public function testFromString(
126
        string $string,
127
        int $expectedMajor,
128
        int $expectedMinor,
129
        int $expectedPatch,
130
        string $expectedPreRelease,
131
        string $exceptedBuild
132
    ) {
133
        $version = Version::fromString($string);
134
        static::assertEquals(
135
            $expectedMajor,
136
            $version->getMajor(),
137
            '__construct() takes the major version as its first argument'
138
        );
139
        static::assertEquals(
140
            $expectedMinor,
141
            $version->getMinor(),
142
            '__construct() takes the minor version as its second argument'
143
        );
144
        static::assertEquals(
145
            $expectedPatch,
146
            $version->getPatch(),
147
            '__construct() takes the patch version as its third argument'
148
        );
149
        static::assertEquals(
150
            $expectedPreRelease,
151
            $version->getPreRelease(),
152
            '__construct() takes the pre-release version as its forth argument'
153
        );
154
        static::assertEquals(
155
            $exceptedBuild,
156
            $version->getBuild(),
157
            '__construct() takes the build version as its fifth argument'
158
        );
159
    }
160
161
    /** @return array */
162
    public function provideValidVersion() : array
163
    {
164
        return [
165
            ['1.2.3', 1, 2, 3, '', ''],
166
            ['1.0.0-alpha', 1, 0, 0, 'alpha', ''],
167
            ['1.0.0-alpha.1', 1, 0, 0, 'alpha.1', ''],
168
            ['1.0.0-0.3.7', 1, 0, 0, '0.3.7', ''],
169
            ['1.0.0-x.7.z.92', 1, 0, 0, 'x.7.z.92', ''],
170
            ['1.0.0-alpha+001', 1, 0, 0, 'alpha', '001'],
171
            ['1.0.0+20130313144700', 1, 0, 0, '', '20130313144700'],
172
            ['1.0.0-beta+exp.sha.5114f85', 1, 0, 0, 'beta', 'exp.sha.5114f85'],
173
        ];
174
    }
175
176
    /**
177
     * @dataProvider                   provideWrongStringVersion
178
     * @expectedException \SemVer\SemVer\Exception\InvalidArgumentException
179
     * @expectedExceptionMessageRegExp /The string ".+" does not look like a version\./
180
     *
181
     * @param string $string
182
     */
183
    public function testFromStringFailsWithInvalidString(string $string)
184
    {
185
        Version::fromString($string);
186
    }
187
188
    /**
189
     * @return array
190
     */
191
    public function provideWrongStringVersion() : array
192
    {
193
        return [
194
            ['1'],
195
            ['a'],
196
            ['1.1'],
197
            ['1.a'],
198
            ['1.1.a'],
199
        ];
200
    }
201
202
    ////////////////////////////////////////////////////////////////////////////
203
    // sort()
204
    ////////////////////////////////////////////////////////////////////////////
205
    public function testSort()
206
    {
207
        $result = Version::sort(
208
            [
209
                Version::fromString('2.0.0'),
210
                Version::fromString('1.10.10'),
211
            ]
212
        );
213
        $expectedResult =
214
            [
215
                Version::fromString('1.10.10'),
216
                Version::fromString('2.0.0'),
217
            ];
218
        do {
219
            $shiftResult   = array_shift($result);
220
            $shiftExpected = array_shift($expectedResult);
221
            static::assertEquals(
222
                $shiftExpected->getMajor(),
223
                $shiftResult->getMajor(),
224
                '::sort() versions must be ordered by major version'
225
            );
226
            static::assertEquals(
227
                $shiftExpected->getMinor(),
228
                $shiftResult->getMinor(),
229
                '::sort() versions must be ordered by major version'
230
            );
231
            static::assertEquals(
232
                $shiftExpected->getPatch(),
233
                $shiftResult->getPatch(),
234
                '::sort() versions must be ordered by major version'
235
            );
236
            static::assertEquals(
237
                $shiftExpected->getPreRelease(),
238
                $shiftResult->getPreRelease(),
239
                '::sort() versions must be ordered by major version'
240
            );
241
        } while (count($result) || count($expectedResult));
242
    }
243
244
    ////////////////////////////////////////////////////////////////////////////
245
    // __toString()
246
    ////////////////////////////////////////////////////////////////////////////
247
    public function testToString()
248
    {
249
        $data = [
250
            '1.0.0',
251
            '1.0.0-alpha.test',
252
            '1.0.0-alpha.test+build',
253
            '1.0.0+build',
254
        ];
255
256
        foreach ($data as $item) {
257
            static::assertEquals($item, (string) Version::fromString($item));
258
        }
259
    }
260
261
    ////////////////////////////////////////////////////////////////////////////
262
    // greaterThan()
263
    ////////////////////////////////////////////////////////////////////////////
264 View Code Duplication
    public function testGreaterThan()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265
    {
266
        $current = new Version(1, 0, 0);
267
        $other   = new Version(1, 1, 0);
268
        static::assertEquals(false, $current->greaterThan($other));
269
        static::assertEquals(true, $other->greaterThan($current));
270
    }
271
272
    ////////////////////////////////////////////////////////////////////////////
273
    // greaterThanOrEqual()
274
    ////////////////////////////////////////////////////////////////////////////
275 View Code Duplication
    public function testGreaterThanOrEqual()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
276
    {
277
        $current = new Version(1, 0, 0);
278
        $other   = new Version(1, 1, 0);
279
        static::assertEquals(false, $current->greaterThanOrEqual($other));
280
        static::assertEquals(true, $other->greaterThanOrEqual($current));
281
    }
282
283
    ////////////////////////////////////////////////////////////////////////////
284
    // lessThan()
285
    ////////////////////////////////////////////////////////////////////////////
286 View Code Duplication
    public function testLessThan()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
287
    {
288
        $current = new Version(1, 0, 0);
289
        $other   = new Version(1, 1, 0);
290
        static::assertEquals(true, $current->lessThan($other));
291
        static::assertEquals(false, $other->lessThan($current));
292
    }
293
294
    ////////////////////////////////////////////////////////////////////////////
295
    // lessThanOrEqual()
296
    ////////////////////////////////////////////////////////////////////////////
297 View Code Duplication
    public function testLessThanOrEqual()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
298
    {
299
        $current = new Version(1, 0, 0);
300
        $other   = new Version(1, 1, 0);
301
        static::assertEquals(true, $current->lessThanOrEqual($other));
302
        static::assertEquals(false, $other->lessThanOrEqual($current));
303
    }
304
}
305