Passed
Push — master ( ec0d02...c0f595 )
by Jan
06:31 queued 10s
created

RangeParserTest::testIsValidRange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace App\Tests\Services\Misc;
22
23
use App\Services\Misc\RangeParser;
24
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
25
26
class RangeParserTest extends WebTestCase
27
{
28
    /**
29
     * @var RangeParser
30
     */
31
    protected $service;
32
33
    public function setUp(): void
34
    {
35
        self::bootKernel();
36
        $this->service = self::$container->get(RangeParser::class);
37
    }
38
39
    public function dataProvider(): array
40
    {
41
        return [
42
            [[], ''],
43
            [[], '   '],
44
            [[], "\t"],
45
            [[1], "1"],
46
            [[1, 2, 3], "1,2, 3"],
47
            [[1, 2, 3], "1-3"],
48
            [[1, 2, 3, 4], "1- 3, 4"],
49
            [[1, 2, 3, 4], "1, 2,3 -   4"],
50
            [[1, 2, 3], "  1; 2, 3"],
51
            [[-1, 0, 1, 2], "-1; 0; 1, 2"],
52
            [[4,3, 1, 2], "4,3, 1;2"],
53
            [[1, 2, 3, 4], "2-1, 3-4"],
54
            [[1], "1-1"],
55
            [[-3, -2, -1], "-3--1"],
56
            [[1, 2, 3], "1,,2;;,,3"],
57
            [[100, 1000, 1], "100, 1000, 1"],
58
            [[], 'test', true],
59
            [[], '1-2-3-4,5', true],
60
            [[], '1 2 3, 455, 23', true],
61
            [[], '1, 2, test', true],
62
        ];
63
    }
64
65
    public function validDataProvider(): array
66
    {
67
        return [
68
            [true, ''],
69
            [true, '    '],
70
            [true, '1, 2, 3'],
71
            [true, '1-2,3, 4- 5'],
72
            [true, '1 -2, 3- 4, 6'],
73
            [true, '1--2'],
74
            [true, '1- -2'],
75
            [true, ',,12,33'],
76
            [false, 'test'],
77
            [false, '1-2-3'],
78
            [false, '1, 2 test'],
79
        ];
80
    }
81
82
    /**
83
     * @dataProvider  dataProvider
84
     */
85
    public function testParse(array $expected, string $input, bool $must_throw = false): void
86
    {
87
        if ($must_throw) {
88
            $this->expectException(\InvalidArgumentException::class);
89
            $this->service->parse($input);
90
        } else {
91
            $this->assertSame($expected, $this->service->parse($input));
92
        }
93
    }
94
95
    /**
96
     * @dataProvider validDataProvider
97
     */
98
    public function testIsValidRange(bool $expected, string $input): void
99
    {
100
        $this->assertSame($expected, $this->service->isValidRange($input));
101
    }
102
}
103