Passed
Push — master ( 39c97e...cdd54f )
by Nelson
02:32
created

IComparableCompareToMethodArgumentsProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 0
loc 29
rs 9.552
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Copyright © 2016-2019 Nelson Martell (http://nelson6e65.github.io)
6
 *
7
 * Licensed under The MIT License (MIT)
8
 * For full copyright and license information, please see the LICENSE
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright 2016-2019 Nelson Martell
12
 * @link      http://nelson6e65.github.io/php_nml/
13
 * @since     v0.6.0
14
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
15
 * */
16
17
namespace NelsonMartell\Test\DataProviders;
18
19
use InvalidArgumentException;
20
use stdClass;
21
use TypeError;
22
23
use NelsonMartell\Test\Helpers\ConstructorMethodTester;
24
use NelsonMartell\Test\Helpers\ExporterPlugin;
25
use NelsonMartell\Test\Helpers\HasReadOnlyProperties;
26
use NelsonMartell\Test\Helpers\HasUnaccesibleProperties;
27
use NelsonMartell\Test\Helpers\IComparableTester;
28
use NelsonMartell\Test\Helpers\IComparerTester;
29
use NelsonMartell\Test\Helpers\IEquatableTester;
30
use NelsonMartell\Test\Helpers\ImplementsIConvertibleToString;
31
use NelsonMartell\Test\Helpers\ImplementsIStrictPropertiesContainer;
32
33
use NelsonMartell\Version;
34
use NelsonMartell\VersionComponent;
35
36
/**
37
 *
38
 * @author Nelson Martell <[email protected]>
39
 * @internal
40
 * */
41
trait VersionTestProvider
42
{
43
    use ConstructorMethodTester;
44
    use ExporterPlugin;
45
    use HasReadOnlyProperties;
46
    use HasUnaccesibleProperties;
47
    use IComparableTester;
48
    use IComparerTester;
49
    use IEquatableTester;
50
    use ImplementsIConvertibleToString;
51
    use ImplementsIStrictPropertiesContainer;
52
53
    public function unaccesiblePropertiesProvider()
54
    {
55
        $version = Version::parse(NML_VERSION);
56
57
        return [
58
            '$major with case changed'    => [$version, 'Major'],
59
            '$minor with case changed'    => [$version, 'Minor'],
60
            '$build with case changed'    => [$version, 'Build'],
61
            '$revision with case changed' => [$version, 'Revision'],
62
        ];
63
    }
64
65
    /**
66
     * Provides invalid arguments for constructor.
67
     *
68
     * @return array
69
     */
70
    public function badConstructorArgumentsProvider()
71
    {
72
        return [
73
            'Type: null (all)'               => [TypeError::class, null, null],
74
            'Only first argument'            => [TypeError::class, 1, null],
75
            'Invalid $major and $minor type' => [TypeError::class, 'hello', 'world'],
76
            'Invalid $major type (string)'   => [TypeError::class, 'hello', 1],
77
            'Invalid $minor type (string)'   => [TypeError::class, 1, 'world'],
78
            '$major value < 0'               => [InvalidArgumentException::class, -1, 0],
79
            '$minor value < 0'               => [InvalidArgumentException::class, 1, -3],
80
            '$build value < 0'               => [InvalidArgumentException::class, 1, 0, -1, null],
81
            '$revision value < 0'            => [InvalidArgumentException::class, 1, 0, 1, -1],
82
            '$revision while $build is not'  => [InvalidArgumentException::class, 1, 0, null, -1],
83
        ];
84
    }
85
86
    /**
87
     * Provides valid arguments for constructor.
88
     *
89
     * @return array
90
     */
91
    public function goodConstructorArgumentsProvider()
92
    {
93
        return [
94
            'SemVer: Normal'                     => [1, 0, 0],
95
            'SemVer: Patch release '             => [1, 0, 1],
96
            'SemVer: Minor release'              => [1, 1, 0],
97
            'SemVer: Major release'              => [2, 0, 0],
98
            'SemVer: Pre-release alpha'          => [1, 0, '0-alpha'],
99
            'SemVer: Pre-release beta'           => [1, 0, '0-beta', 1],
100
            // 'SemVer: Pre-release build metadata' => [1, 0, '0-beta', '1+20130313144700'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
            'Windows version: Major'             => [1, 0, 0, 0],
102
            'Windows version: Minor'             => [1, 1, 0, 0],
103
            'Windows version: Build'             => [1, 2, 1, 0],
104
            'Windows version: Revision'          => [1, 3, 1, 2344234],
105
            'Git: describe'                      => [0, 5, '1-34-g6e5462c'],
106
            'Zero (minor)'                       => [0, 0], // is invalid, but can be created
107
            'Zero (build)'                       => [0, 0, 0], // is invalid, but can be created
108
            'Zero (revision)'                    => [0, 0, 0, 0], // is invalid, but can be created
109
        ];
110
    }
111
112
113
    public function objectInstanceProvider()
114
    {
115
        return [[new Version(0, 7, '0-beta')]];
116
    }
117
118
    public function readOnlyPropertiesProvider()
119
    {
120
        $obj = new Version(0, 7, '0-beta');
121
122
        return [
123
            [$obj, 'major', 0],
124
            [$obj, 'minor', 7],
125
            [$obj, 'build', new VersionComponent(0, '-beta')],
126
            [$obj, 'revision', new VersionComponent(null)],
127
        ];
128
    }
129
130
    public function IComparableCompareToMethodArgumentsProvider()
131
    {
132
        $v             = new Version(1, 0, 9);
133
        $obj           = new stdClass();
134
        $obj->major    = 1;
135
        $obj->minor    = 0;
136
        $obj->build    = 9;
137
        $obj->revision = null;
138
139
140
        $args = [
141
            'Equals by reference'               => [0, $v, $v],
142
            'Equals by value'                   => [0, new Version(1, 0, 1), Version::parse('1.0.1')],
143
            'Major difference'                  => [-1, Version::parse('1.0.0'), Version::parse('2.0.0')],
144
            'Minor difference'                  => [1, Version::parse('1.1.0'), Version::parse('1.0.0')],
145
            'Build difference'                  => [1, Version::parse('1.0.1'), Version::parse('1.0.0')],
146
            'Revision difference'               => [-1, Version::parse('1.0.0.254'), Version::parse('1.0.0.389')],
147
            'Version < object'                  => [null, $v, $obj],
148
            'Version > array parseable'         => [1, Version::parse('1.1.0'), [0, 1, 999]],
149
            'Version < array parseable'         => [-1, Version::parse('1.1.0'), [2, 0]],
150
            'Version > array not parseable'     => [1, Version::parse('0.0.0'), ['invalid array']],
151
            'Version > string parseable'        => [1, Version::parse('1.1.0'), '0.1.999'],
152
            'Version < string parseable'        => [-1, Version::parse('1.1.0'), '2.0'],
153
            'Version > string not parseable'    => [1, Version::parse('1.1.0'), 'invalid string'],
154
            'integer|Version'                   => [1, $v, 9976645645656],
155
            'Version > null'                    => [1, Version::parse('1.1.0'), null],
156
        ];
157
158
        return $args;
159
    }
160
161
    public function compareMethodArgumentsProvider()
162
    {
163
        $v             = new Version(1, 0, 9);
164
        $obj           = new \stdClass();
165
        $obj->major    = 1;
166
        $obj->minor    = 0;
167
        $obj->build    = 9;
168
        $obj->revision = null;
169
170
        $args = [
171
            'stdClass|Version' => [null, $obj, $v],
172
            'string|Version'   => [-1, '1.0.0.254', $v],
173
            'integer|Version'  => [-1, 9976645645656, $v],
174
            'float|Version'    => [-1, 1.342333, $v],
175
            'array|Version'    => [-1, [0, 1, 999], Version::parse('1.1.0')],
176
        ];
177
178
        return $args;
179
    }
180
181
    public function compareMethodArraysProvider()
182
    {
183
        return [
184
            'Version[]' => [[
185
                new Version(1, 0, 1, 3),
186
                new Version(1, 0, 11, 3),
187
                new Version(1, 1, 1, 0),
188
                new Version(1, 3, 1, 9),
189
                Version::parse('2.3.2-3-g'),
190
                Version::parse('2.3.2-3-g726356'),
191
                Version::parse('2.3.2-4-g'),
192
                Version::parse('2.3.4-3-g'),
193
                Version::parse('2.3.4-3-gsh4hajk7'),
194
                Version::parse('2.3.4-3-gsh4hbjk7'),
195
                Version::parse('2.31.0-4-g'),
196
                Version::parse('2.31.1-4-g'),
197
                Version::parse('2.31.11-4-g'),
198
            ]],
199
            'Version[] + integer[]' => [[
200
                1,
201
                new Version(1, 0, 1, 3),
202
                new Version(1, 0, 11, 3),
203
                new Version(1, 1, 1, 0),
204
            ]],
205
            'Version[] + string[]'  => [[
206
                '0.0',
207
                new Version(0, 0, 9, 3),
208
                '0.1.0',
209
            ]],
210
            'Version[] + string[] (1 non parseable string)'  => [[
211
                '0.1.0',
212
                'invalid string',
213
                new Version(1, 0, 1, 3),
214
            ]],
215
            'Version[] + array[]'   => [[
216
                [],
217
                [0, 1, 0],
218
                new Version(1, 0, 1, 3),
219
            ]],
220
        ];
221
    }
222
223
    public function IEquatableMethodArgumentsProvider()
224
    {
225
        return [
226
            [true, new Version(1, 2), new Version(1, 2)],
227
            [false, new Version(1, 4), new Version(1, 2)],
228
            [false, new Version(1, 2, 1), new Version(1, 2, 2)],
229
            [false, new Version(1, 2, 1), 123],
230
            [false, new Version(1, 2, 1), 2345654675675675673453],
231
            [false, new Version(1, 2, 1), '1.2.1'],
232
            [false, new Version(1, 2, 1), [1, 2, 1]],
233
            [false, new Version(1, 2, 1), new \stdClass],
234
        ];
235
    }
236
237
    protected $parseableStrings = [
238
        'valid' => [
239
            '1.0',
240
            '0.2',
241
            '2.3.2-3-g726351',
242
            '2.3.2.3-2-g726352',
243
            '3.0.1',
244
            '4.0.2.0',
245
            '5.0.0.3-beta',
246
            '6.0.0-alpha',
247
            NML_VERSION,
248
        ],
249
        'invalid' => [
250
            '0.0',
251
            '1.0..1',
252
            '2.0.0-alpha.0',
253
            '2.3.2-3-g726353.3',
254
            '2.3.2-3-g726356.1-2-gyt4f4',
255
            '3.0.1-alpha.1',
256
            '4.0.0-alpha.0-beta',
257
            '5.0.1-alpha.2-beta',
258
        ],
259
    ];
260
261
    public function isValidProvider()
262
    {
263
        $args = [];
264
265
        foreach ($this->parseableStrings['valid'] as $str) {
266
            $args[$str] = [true, Version::parse($str)];
267
        }
268
269
        foreach ($this->parseableStrings['invalid'] as $str) {
270
            $args[$str] = [false, Version::parse($str)];
271
        }
272
273
        return $args;
274
    }
275
276
277
    public function parseableStringsProvider() : array
278
    {
279
        $strings = [
280
            NML_VERSION,
281
            '1.0',
282
            '0.2',
283
            '2.3.2-3-g726351',
284
            '2.3.2.3-2-g726352',
285
            '3.0.1',
286
            '4.0.2.0',
287
            '5.0.0.3-beta',
288
            '6.0.0-alpha',
289
290
            // Invalid?
291
            '0.0',
292
            '1.0..1',
293
            '2.0.0-alpha.0',
294
            '2.3.2-3-g726353.3',
295
            '2.3.2-3-g726356.1-2-gyt4f4',
296
            '3.0.1-alpha.1',
297
            '4.0.0-alpha.0-beta',
298
            '5.0.1-alpha.2-beta',
299
        ];
300
301
        $r = [];
302
303
        foreach ($strings as $str) {
304
            $r[$str] = [$str];
305
        }
306
307
        return $r;
308
    }
309
310
    public function parseableArraysProvider() : array
311
    {
312
        return [
313
            'minimum version'   => [[1, 0]],
314
            'build version'     => [[1, 1, 2]],
315
            'revision version'  => [[1, 0, '0-beta', 3]],
316
        ];
317
    }
318
319
    public function nonParseableValuesProvider()
320
    {
321
        return [
322
            'empty string'              => [''],
323
            'empty array'               => [[]],
324
            'array with only 1 element' => [[1]],
325
        ];
326
    }
327
328
    public function toStringProvider()
329
    {
330
        return [
331
            ['1.0', new Version(1, 0)],
332
            ['0.2', new Version(0, 2)],
333
            ['2.3.2-3-g726351', new Version(2, 3, '2-3-g726351')],
334
            ['2.3.2.3-2-g726352', new Version(2, 3, 2, '3-2-g726352')],
335
            ['3.0.1', new Version(3, 0, 1)],
336
            ['4.0.2.0', new Version(4, 0, 2, 0)],
337
            ['5.0.0.3-beta', new Version(5, 0, 0, '3-beta')],
338
            ['6.0.0-alpha', new Version(6, 0, '0-alpha')],
339
        ];
340
    }
341
}
342