Test Failed
Push — master ( 322b96...cb950d )
by Nelson
03:16
created

VersionTestProvider::compareMethodArraysProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 29
nc 1
nop 0
dl 0
loc 38
rs 9.456
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 TypeError;
20
use InvalidArgumentException;
21
22
use NelsonMartell\Test\Helpers\ConstructorMethodTester;
23
use NelsonMartell\Test\Helpers\ExporterPlugin;
24
use NelsonMartell\Test\Helpers\HasReadOnlyProperties;
25
use NelsonMartell\Test\Helpers\HasUnaccesibleProperties;
26
use NelsonMartell\Test\Helpers\IComparableTester;
27
use NelsonMartell\Test\Helpers\IComparerTester;
28
use NelsonMartell\Test\Helpers\IEquatableTester;
29
use NelsonMartell\Test\Helpers\ImplementsIConvertibleToString;
30
use NelsonMartell\Test\Helpers\ImplementsIStrictPropertiesContainer;
31
32
use NelsonMartell\Version;
33
use NelsonMartell\VersionComponent;
34
35
/**
36
 *
37
 * @author Nelson Martell <[email protected]>
38
 * @internal
39
 * */
40
trait VersionTestProvider
41
{
42
    use ConstructorMethodTester;
43
    use ExporterPlugin;
44
    use HasReadOnlyProperties;
45
    use HasUnaccesibleProperties;
46
    use IComparableTester;
47
    use IComparerTester;
48
    use IEquatableTester;
49
    use ImplementsIConvertibleToString;
50
    use ImplementsIStrictPropertiesContainer;
51
52
    public function unaccesiblePropertiesProvider()
53
    {
54
        $version = Version::parse(NML_VERSION);
55
56
        return [
57
            '$major with case changed'    => [$version, 'Major'],
58
            '$minor with case changed'    => [$version, 'Minor'],
59
            '$build with case changed'    => [$version, 'Build'],
60
            '$revision with case changed' => [$version, 'Revision'],
61
        ];
62
    }
63
64
    /**
65
     * Provides invalid arguments for constructor.
66
     *
67
     * @return array
68
     */
69
    public function badConstructorArgumentsProvider()
70
    {
71
        return [
72
            'Type: null (all)'               => [TypeError::class, null, null],
73
            'Only first argument'            => [TypeError::class, 1, null],
74
            'Invalid $major and $minor type' => [TypeError::class, 'hello', 'world'],
75
            'Invalid $major type (string)'   => [TypeError::class, 'hello', 1],
76
            'Invalid $minor type (string)'   => [TypeError::class, 1, 'world'],
77
            '$major value < 0'               => [InvalidArgumentException::class, -1, 0],
78
            '$minor value < 0'               => [InvalidArgumentException::class, 1, -3],
79
            '$build value < 0'               => [InvalidArgumentException::class, 1, 0, -1, null],
80
            '$revision value < 0'            => [InvalidArgumentException::class, 1, 0, 1, -1],
81
            '$revision while $build is not'  => [InvalidArgumentException::class, 1, 0, null, -1],
82
        ];
83
    }
84
85
    /**
86
     * Provides valid arguments for constructor.
87
     *
88
     * @return array
89
     */
90
    public function goodConstructorArgumentsProvider()
91
    {
92
        return [
93
            'SemVer: Normal'                     => [1, 0, 0],
94
            'SemVer: Patch release '             => [1, 0, 1],
95
            'SemVer: Minor release'              => [1, 1, 0],
96
            'SemVer: Major release'              => [2, 0, 0],
97
            'SemVer: Pre-release alpha'          => [1, 0, '0-alpha'],
98
            'SemVer: Pre-release beta'           => [1, 0, '0-beta', 1],
99
            // '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...
100
            'Windows version: Major'             => [1, 0, 0, 0],
101
            'Windows version: Minor'             => [1, 1, 0, 0],
102
            'Windows version: Build'             => [1, 2, 1, 0],
103
            'Windows version: Revision'          => [1, 3, 1, 2344234],
104
            'Git: describe'                      => [0, 5, '1-34-g6e5462c'],
105
            'Zero (minor)'                       => [0, 0], // is invalid, but can be created
106
            'Zero (build)'                       => [0, 0, 0], // is invalid, but can be created
107
            'Zero (revision)'                    => [0, 0, 0, 0], // is invalid, but can be created
108
        ];
109
    }
110
111
112
    public function objectInstanceProvider()
113
    {
114
        return [[new Version(0, 7, '0-beta')]];
115
    }
116
117
    public function readOnlyPropertiesProvider()
118
    {
119
        $obj = new Version(0, 7, '0-beta');
120
121
        return [
122
            [$obj, 'major', 0],
123
            [$obj, 'minor', 7],
124
            [$obj, 'build', new VersionComponent(0, '-beta')],
125
            [$obj, 'revision', new VersionComponent(null)],
126
        ];
127
    }
128
129
    public function IComparableCompareToMethodArgumentsProvider()
130
    {
131
        $v             = new Version(1, 0, 9);
132
        $obj           = new \stdClass();
133
        $obj->major    = 1;
134
        $obj->minor    = 0;
135
        $obj->build    = 9;
136
        $obj->revision = null;
137
138
139
        $args = [
140
            'Equals by reference'               => [0, $v, $v],
141
            'Equals by value'                   => [0, new Version(1, 0, 1), Version::parse('1.0.1')],
142
            'Major difference'                  => [-1, Version::parse('1.0.0'), Version::parse('2.0.0')],
143
            'Minor difference'                  => [1, Version::parse('1.1.0'), Version::parse('1.0.0')],
144
            'Build difference'                  => [1, Version::parse('1.0.1'), Version::parse('1.0.0')],
145
            'Revision difference'               => [-1, Version::parse('1.0.0.254'), Version::parse('1.0.0.389')],
146
            'Version < object'                  => [null, $v, $obj],
147
            'Version > array parseable'         => [1, Version::parse('1.1.0'), [0, 1, 999]],
148
            'Version < array parseable'         => [-1, Version::parse('1.1.0'), [2, 0]],
149
            'Version > array not parseable'     => [1, Version::parse('0.0.0'), ['invalid array']],
150
            'Version > string parseable'        => [1, Version::parse('1.1.0'), '0.1.999'],
151
            'Version < string parseable'        => [-1, Version::parse('1.1.0'), '2.0'],
152
            'Version > string not parseable'    => [1, Version::parse('1.1.0'), 'invalid string'],
153
            'integer|Version'                   => [1, $v, 9976645645656],
154
        ];
155
156
        return $args;
157
    }
158
159
    public function compareMethodArgumentsProvider()
160
    {
161
        $v             = new Version(1, 0, 9);
162
        $obj           = new \stdClass();
163
        $obj->major    = 1;
164
        $obj->minor    = 0;
165
        $obj->build    = 9;
166
        $obj->revision = null;
167
168
        $args = [
169
            'stdClass|Version' => [null, $obj, $v],
170
            'string|Version'   => [-1, '1.0.0.254', $v],
171
            'integer|Version'  => [-1, 9976645645656, $v],
172
            'float|Version'    => [-1, 1.342333, $v],
173
            'array|Version'    => [-1, [0, 1, 999], Version::parse('1.1.0')],
174
        ];
175
176
        return $args;
177
    }
178
179
    public function compareMethodArraysProvider()
180
    {
181
        return [
182
            'Version[]' => [[
183
                new Version(1, 0, 1, 3),
184
                new Version(1, 0, 11, 3),
185
                new Version(1, 1, 1, 0),
186
                new Version(1, 3, 1, 9),
187
                Version::parse('2.3.2-3-g'),
188
                Version::parse('2.3.2-3-g726356'),
189
                Version::parse('2.3.2-4-g'),
190
                Version::parse('2.3.4-3-g'),
191
                Version::parse('2.3.4-3-gsh4hajk7'),
192
                Version::parse('2.3.4-3-gsh4hbjk7'),
193
                Version::parse('2.31.0-4-g'),
194
                Version::parse('2.31.1-4-g'),
195
                Version::parse('2.31.11-4-g'),
196
            ]],
197
            'Version[] + integer[]' => [[
198
                1,
199
                new Version(1, 0, 1, 3),
200
                new Version(1, 0, 11, 3),
201
                new Version(1, 1, 1, 0),
202
            ]],
203
            'Version[] + string[]'  => [[
204
                '0.0',
205
                new Version(0, 0, 9, 3),
206
                '0.1.0',
207
            ]],
208
            'Version[] + string[] (1 non parseable string)'  => [[
209
                '0.1.0',
210
                'invalid string',
211
                new Version(1, 0, 1, 3),
212
            ]],
213
            'Version[] + array[]'   => [[
214
                [],
215
                [0, 1, 0],
216
                new Version(1, 0, 1, 3),
217
            ]],
218
        ];
219
    }
220
221
    public function IEquatableMethodArgumentsProvider()
222
    {
223
        return [
224
            [true, new Version(1, 2), new Version(1, 2)],
225
            [false, new Version(1, 4), new Version(1, 2)],
226
            [false, new Version(1, 2, 1), new Version(1, 2, 2)],
227
            [false, new Version(1, 2, 1), 123],
228
            [false, new Version(1, 2, 1), 2345654675675675673453],
229
            [false, new Version(1, 2, 1), '1.2.1'],
230
            [false, new Version(1, 2, 1), [1, 2, 1]],
231
            [false, new Version(1, 2, 1), new \stdClass],
232
        ];
233
    }
234
235
    protected $parseableStrings = [
236
        'valid' => [
237
            '1.0',
238
            '0.2',
239
            '2.3.2-3-g726351',
240
            '2.3.2.3-2-g726352',
241
            '3.0.1',
242
            '4.0.2.0',
243
            '5.0.0.3-beta',
244
            '6.0.0-alpha',
245
        ],
246
        'invalid' => [
247
            '0.0',
248
            '1.0..1',
249
            '2.0.0-alpha.0',
250
            '2.3.2-3-g726353.3',
251
            '2.3.2-3-g726356.1-2-gyt4f4',
252
            '3.0.1-alpha.1',
253
            '4.0.0-alpha.0-beta',
254
            '5.0.1-alpha.2-beta',
255
        ],
256
    ];
257
258
    public function isValidProvider()
259
    {
260
        $args = [];
261
262
        foreach ($this->parseableStrings['valid'] as $str) {
263
            $args[$str] = [true, Version::parse($str)];
264
        }
265
266
        foreach ($this->parseableStrings['invalid'] as $str) {
267
            $args[$str] = [false, Version::parse($str)];
268
        }
269
270
        return $args;
271
    }
272
273
    public function toStringProvider()
274
    {
275
        return [
276
            ['1.0', new Version(1, 0)],
277
            ['0.2', new Version(0, 2)],
278
            ['2.3.2-3-g726351', new Version(2, 3, '2-3-g726351')],
279
            ['2.3.2.3-2-g726352', new Version(2, 3, 2, '3-2-g726352')],
280
            ['3.0.1', new Version(3, 0, 1)],
281
            ['4.0.2.0', new Version(4, 0, 2, 0)],
282
            ['5.0.0.3-beta', new Version(5, 0, 0, '3-beta')],
283
            ['6.0.0-alpha', new Version(6, 0, '0-alpha')],
284
        ];
285
    }
286
}
287