Completed
Push — master ( 520f00...657555 )
by Julien
02:13
created

VersionTest::testEquals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
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
    // __toString()
204
    ////////////////////////////////////////////////////////////////////////////
205
    public function testToString()
206
    {
207
        $data = [
208
            '1.0.0',
209
            '1.0.0-alpha.test',
210
            '1.0.0-alpha.test+build',
211
            '1.0.0+build',
212
        ];
213
214
        foreach ($data as $item) {
215
            static::assertEquals($item, (string) Version::fromString($item));
216
        }
217
    }
218
219
    ////////////////////////////////////////////////////////////////////////////
220
    // equals()
221
    ////////////////////////////////////////////////////////////////////////////
222
    public function testEquals()
223
    {
224
        $current = new Version(1, 0, 0);
225
        $other   = new Version(1, 0, 0);
226
        static::assertEquals(true, $other->equals($current));
227
        $current = new Version(1, 1, 0);
228
        $other   = new Version(1, 0, 0);
229
        static::assertEquals(false, $other->equals($current));
230
    }
231
232
    ////////////////////////////////////////////////////////////////////////////
233
    // greaterThan()
234
    ////////////////////////////////////////////////////////////////////////////
235 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...
236
    {
237
        $current = new Version(1, 0, 0);
238
        $other   = new Version(1, 1, 0);
239
        static::assertEquals(false, $current->greaterThan($other));
240
        static::assertEquals(true, $other->greaterThan($current));
241
    }
242
243
    ////////////////////////////////////////////////////////////////////////////
244
    // greaterThanOrEqual()
245
    ////////////////////////////////////////////////////////////////////////////
246 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...
247
    {
248
        $current = new Version(1, 0, 0);
249
        $other   = new Version(1, 1, 0);
250
        static::assertEquals(false, $current->greaterThanOrEqual($other));
251
        static::assertEquals(true, $other->greaterThanOrEqual($current));
252
    }
253
254
    ////////////////////////////////////////////////////////////////////////////
255
    // lessThan()
256
    ////////////////////////////////////////////////////////////////////////////
257 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...
258
    {
259
        $current = new Version(1, 0, 0);
260
        $other   = new Version(1, 1, 0);
261
        static::assertEquals(true, $current->lessThan($other));
262
        static::assertEquals(false, $other->lessThan($current));
263
    }
264
265
    ////////////////////////////////////////////////////////////////////////////
266
    // lessThanOrEqual()
267
    ////////////////////////////////////////////////////////////////////////////
268 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...
269
    {
270
        $current = new Version(1, 0, 0);
271
        $other   = new Version(1, 1, 0);
272
        static::assertEquals(true, $current->lessThanOrEqual($other));
273
        static::assertEquals(false, $other->lessThanOrEqual($current));
274
    }
275
}
276