Issues (36)

tests/DataProviders/VersionTestProvider.php (1 issue)

1
<?php
2
3
/**
4
 * PHP: Nelson Martell Library file
5
 *
6
 * Copyright © 2016-2021 Nelson Martell (http://nelson6e65.github.io)
7
 *
8
 * Licensed under The MIT License (MIT)
9
 * For full copyright and license information, please see the LICENSE
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright 2016-2021 Nelson Martell
13
 * @link      http://nelson6e65.github.io/php_nml/
14
 * @since     v0.6.0
15
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
16
 * */
17
18
declare(strict_types=1);
19
20
namespace NelsonMartell\Test\DataProviders;
21
22
use InvalidArgumentException;
23
use stdClass;
24
use TypeError;
25
use NelsonMartell\Test\Helpers\ConstructorMethodTester;
26
use NelsonMartell\Test\Helpers\ExporterPlugin;
27
use NelsonMartell\Test\Helpers\HasReadOnlyProperties;
28
use NelsonMartell\Test\Helpers\HasUnaccesibleProperties;
29
use NelsonMartell\Test\Helpers\IComparableTester;
30
use NelsonMartell\Test\Helpers\IComparerTester;
31
use NelsonMartell\Test\Helpers\IEquatableTester;
32
use NelsonMartell\Test\Helpers\ImplementsIConvertibleToString;
33
use NelsonMartell\Test\Helpers\ImplementsIStrictPropertiesContainer;
34
use NelsonMartell\Version;
35
use NelsonMartell\VersionComponent;
36
37
/**
38
 *
39
 * @author Nelson Martell <[email protected]>
40
 * @internal
41
 * */
42
trait VersionTestProvider
43
{
44
    use ConstructorMethodTester;
45
    use ExporterPlugin;
46
    use HasReadOnlyProperties;
47
    use HasUnaccesibleProperties;
48
    use IComparableTester;
49
    use IComparerTester;
50
    use IEquatableTester;
51
    use ImplementsIConvertibleToString;
52
    use ImplementsIStrictPropertiesContainer;
53
54
    public function unaccesiblePropertiesProvider(): array
55
    {
56
        $version = Version::parse(NML_VERSION);
57
58
        return [
59
            '$major with case changed'    => [$version, 'Major'],
60
            '$minor with case changed'    => [$version, 'Minor'],
61
            '$build with case changed'    => [$version, 'Build'],
62
            '$revision with case changed' => [$version, 'Revision'],
63
        ];
64
    }
65
66
    /**
67
     * Provides invalid arguments for constructor.
68
     *
69
     * @return array
70
     */
71
    public function badConstructorArgumentsProvider(): array
72
    {
73
        return [
74
            'Type: null (all)'               => [TypeError::class, null, null],
75
            'Only first argument'            => [TypeError::class, 1, null],
76
            'Invalid $major and $minor type' => [TypeError::class, 'hello', 'world'],
77
            'Invalid $major type (string)'   => [TypeError::class, 'hello', 1],
78
            'Invalid $minor type (string)'   => [TypeError::class, 1, 'world'],
79
            '$major value < 0'               => [InvalidArgumentException::class, -1, 0],
80
            '$minor value < 0'               => [InvalidArgumentException::class, 1, -3],
81
            '$build value < 0'               => [InvalidArgumentException::class, 1, 0, -1, null],
82
            '$revision value < 0'            => [InvalidArgumentException::class, 1, 0, 1, -1],
83
            '$revision while $build is not'  => [InvalidArgumentException::class, 1, 0, null, -1],
84
        ];
85
    }
86
87
    /**
88
     * Provides valid arguments for constructor.
89
     *
90
     * @return array
91
     */
92
    public function goodConstructorArgumentsProvider(): array
93
    {
94
        return [
95
            'SemVer: Normal'            => [1, 0, 0],
96
            'SemVer: Patch release '    => [1, 0, 1],
97
            'SemVer: Minor release'     => [1, 1, 0],
98
            'SemVer: Major release'     => [2, 0, 0],
99
            'SemVer: Pre-release alpha' => [1, 0, '0-alpha'],
100
            'SemVer: Pre-release beta'  => [1, 0, '0-beta', 1],
101
            // '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...
102
            'Windows version: Major'    => [1, 0, 0, 0],
103
            'Windows version: Minor'    => [1, 1, 0, 0],
104
            'Windows version: Build'    => [1, 2, 1, 0],
105
            'Windows version: Revision' => [1, 3, 1, 2344234],
106
            'Git: describe'             => [0, 5, '1-34-g6e5462c'],
107
            'Zero (minor)'              => [0, 0], // is invalid, but can be created
108
            'Zero (build)'              => [0, 0, 0], // is invalid, but can be created
109
            'Zero (revision)'           => [0, 0, 0, 0], // is invalid, but can be created
110
        ];
111
    }
112
113
114
    public function objectInstanceProvider(): array
115
    {
116
        return [[new Version(0, 7, '0-beta')]];
117
    }
118
119
    public function readOnlyPropertiesProvider(): array
120
    {
121
        $obj = new Version(0, 7, '0-beta');
122
123
        return [
124
            [$obj, 'major', 0],
125
            [$obj, 'minor', 7],
126
            [$obj, 'build', new VersionComponent(0, '-beta')],
127
            [$obj, 'revision', new VersionComponent(null)],
128
        ];
129
    }
130
131
    public function IComparableCompareToMethodArgumentsProvider(): array
132
    {
133
        $v             = new Version(1, 0, 9);
134
        $obj           = new stdClass();
135
        $obj->major    = 1;
136
        $obj->minor    = 0;
137
        $obj->build    = 9;
138
        $obj->revision = null;
139
140
141
        $args = [
142
            'Equals by reference'            => [0, $v, $v],
143
            'Equals by value'                => [0, new Version(1, 0, 1), Version::parse('1.0.1')],
144
            'Major difference'               => [-1, Version::parse('1.0.0'), Version::parse('2.0.0')],
145
            'Minor difference'               => [1, Version::parse('1.1.0'), Version::parse('1.0.0')],
146
            'Build difference'               => [1, Version::parse('1.0.1'), Version::parse('1.0.0')],
147
            'Revision difference'            => [-1, Version::parse('1.0.0.254'), Version::parse('1.0.0.389')],
148
            'Version < object'               => [null, $v, $obj],
149
            'Version > array parseable'      => [1, Version::parse('1.1.0'), [0, 1, 999]],
150
            'Version < array parseable'      => [-1, Version::parse('1.1.0'), [2, 0]],
151
            'Version > array not parseable'  => [1, Version::parse('0.0.0'), ['invalid array']],
152
            'Version > string parseable'     => [1, Version::parse('1.1.0'), '0.1.999'],
153
            'Version < string parseable'     => [-1, Version::parse('1.1.0'), '2.0'],
154
            'Version > string not parseable' => [1, Version::parse('1.1.0'), 'invalid string'],
155
            'integer|Version'                => [1, $v, 9976645645656],
156
            'Version > null'                 => [1, Version::parse('1.1.0'), null],
157
        ];
158
159
        return $args;
160
    }
161
162
    public function compareMethodArgumentsProvider(): array
163
    {
164
        $v             = new Version(1, 0, 9);
165
        $obj           = new \stdClass();
166
        $obj->major    = 1;
167
        $obj->minor    = 0;
168
        $obj->build    = 9;
169
        $obj->revision = null;
170
171
        $args = [
172
            'stdClass|Version' => [null, $obj, $v],
173
            'string|Version'   => [-1, '1.0.0.254', $v],
174
            'integer|Version'  => [-1, 9976645645656, $v],
175
            'float|Version'    => [-1, 1.342333, $v],
176
            'array|Version'    => [-1, [0, 1, 999], Version::parse('1.1.0')],
177
        ];
178
179
        return $args;
180
    }
181
182
    public function compareMethodArraysProvider(): array
183
    {
184
        return [
185
            'Version[]'                                     => [[
186
                new Version(1, 0, 1, 3),
187
                new Version(1, 0, 11, 3),
188
                new Version(1, 1, 1, 0),
189
                new Version(1, 3, 1, 9),
190
                Version::parse('2.3.2-3-g'),
191
                Version::parse('2.3.2-3-g726356'),
192
                Version::parse('2.3.2-4-g'),
193
                Version::parse('2.3.4-3-g'),
194
                Version::parse('2.3.4-3-gsh4hajk7'),
195
                Version::parse('2.3.4-3-gsh4hbjk7'),
196
                Version::parse('2.31.0-4-g'),
197
                Version::parse('2.31.1-4-g'),
198
                Version::parse('2.31.11-4-g'),
199
            ],
200
            ],
201
            'Version[] + integer[]'                         => [[
202
                1,
203
                new Version(1, 0, 1, 3),
204
                new Version(1, 0, 11, 3),
205
                new Version(1, 1, 1, 0),
206
            ],
207
            ],
208
            'Version[] + string[]'                          => [[
209
                '0.0',
210
                new Version(0, 0, 9, 3),
211
                '0.1.0',
212
            ],
213
            ],
214
            'Version[] + string[] (1 non parseable string)' => [[
215
                '0.1.0',
216
                'invalid string',
217
                new Version(1, 0, 1, 3),
218
            ],
219
            ],
220
            'Version[] + array[]'                           => [[
221
                [],
222
                [0, 1, 0],
223
                new Version(1, 0, 1, 3),
224
            ],
225
            ],
226
        ];
227
    }
228
229
    public function IEquatableMethodArgumentsProvider(): array
230
    {
231
        return [
232
            [true, new Version(1, 2), new Version(1, 2)],
233
            [false, new Version(1, 4), new Version(1, 2)],
234
            [false, new Version(1, 2, 1), new Version(1, 2, 2)],
235
            [false, new Version(1, 2, 1), 123],
236
            [false, new Version(1, 2, 1), 2345654675675675673453],
237
            [false, new Version(1, 2, 1), '1.2.1'],
238
            [false, new Version(1, 2, 1), [1, 2, 1]],
239
            [false, new Version(1, 2, 1), new \stdClass()],
240
        ];
241
    }
242
243
    protected $parseableStrings = [
244
        'valid'   => [
245
            '1.0',
246
            '0.2',
247
            '2.3.2-3-g726351',
248
            '2.3.2.3-2-g726352',
249
            '3.0.1',
250
            '4.0.2.0',
251
            '5.0.0.3-beta',
252
            '6.0.0-alpha',
253
            NML_VERSION,
254
        ],
255
        'invalid' => [
256
            '0.0',
257
            '1.0..1',
258
            '2.0.0-alpha.0',
259
            '2.3.2-3-g726353.3',
260
            '2.3.2-3-g726356.1-2-gyt4f4',
261
            '3.0.1-alpha.1',
262
            '4.0.0-alpha.0-beta',
263
            '5.0.1-alpha.2-beta',
264
        ],
265
    ];
266
267
    public function isValidProvider(): array
268
    {
269
        $args = [];
270
271
        foreach ($this->parseableStrings['valid'] as $str) {
272
            $args[$str] = [true, Version::parse($str)];
273
        }
274
275
        foreach ($this->parseableStrings['invalid'] as $str) {
276
            $args[$str] = [false, Version::parse($str)];
277
        }
278
279
        return $args;
280
    }
281
282
283
    public function parseableStringsProvider(): array
284
    {
285
        $strings = [
286
            NML_VERSION,
287
            '1.0',
288
            '0.2',
289
            '2.3.2-3-g726351',
290
            '2.3.2.3-2-g726352',
291
            '3.0.1',
292
            '4.0.2.0',
293
            '5.0.0.3-beta',
294
            '6.0.0-alpha',
295
296
            // Invalid?
297
            '0.0',
298
            '1.0..1',
299
            '2.0.0-alpha.0',
300
            '2.3.2-3-g726353.3',
301
            '2.3.2-3-g726356.1-2-gyt4f4',
302
            '3.0.1-alpha.1',
303
            '4.0.0-alpha.0-beta',
304
            '5.0.1-alpha.2-beta',
305
        ];
306
307
        $r = [];
308
309
        foreach ($strings as $str) {
310
            $r[$str] = [$str];
311
        }
312
313
        return $r;
314
    }
315
316
    public function parseableArraysProvider(): array
317
    {
318
        return [
319
            'minimum version'  => [[1, 0]],
320
            'build version'    => [[1, 1, 2]],
321
            'revision version' => [[1, 0, '0-beta', 3]],
322
        ];
323
    }
324
325
    public function nonParseableValuesProvider(): array
326
    {
327
        return [
328
            'empty string'              => [''],
329
            'empty array'               => [[]],
330
            'array with only 1 element' => [[1]],
331
        ];
332
    }
333
334
    public function toStringProvider(): array
335
    {
336
        return [
337
            ['1.0', new Version(1, 0)],
338
            ['0.2', new Version(0, 2)],
339
            ['2.3.2-3-g726351', new Version(2, 3, '2-3-g726351')],
340
            ['2.3.2.3-2-g726352', new Version(2, 3, 2, '3-2-g726352')],
341
            ['3.0.1', new Version(3, 0, 1)],
342
            ['4.0.2.0', new Version(4, 0, 2, 0)],
343
            ['5.0.0.3-beta', new Version(5, 0, 0, '3-beta')],
344
            ['6.0.0-alpha', new Version(6, 0, '0-alpha')],
345
        ];
346
    }
347
}
348