provideValidatorsLists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 25
rs 9.7998
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkioTest\Shlink\Common\Validation;
6
7
use Laminas\Validator;
8
use PHPUnit\Framework\TestCase;
9
use Shlinkio\Shlink\Common\Validation\ExcludingValidatorChain;
10
11
class ExcludingValidatorChainTest extends TestCase
12
{
13
    /**
14
     * @param mixed $value
15
     * @test
16
     * @dataProvider provideValidatorsLists
17
     */
18
    public function validationPassesAsSoonAsOneWrappedValidatorPasses(array $validators, $value, bool $expected): void
19
    {
20
        $chain = new ExcludingValidatorChain(...$validators);
21
22
        $this->assertEquals($expected, $chain->isValid($value));
23
        $this->assertEquals($expected, ($chain)($value));
24
    }
25
26
    public function provideValidatorsLists(): iterable
27
    {
28
        yield [
29
            [
30
                new Validator\Between(['min' => 10, 'max' => 100]),
31
                new Validator\Between(['min' => 50, 'max' => 60]),
32
            ],
33
            80,
34
            true,
35
        ];
36
        yield [
37
            [
38
                new Validator\Between(['min' => 10, 'max' => 100]),
39
                new Validator\Digits(),
40
            ],
41
            5,
42
            true,
43
        ];
44
        yield [
45
            [
46
                new Validator\Between(['min' => 10, 'max' => 100]),
47
                new Validator\Between(['min' => 50, 'max' => 60]),
48
            ],
49
            'foo',
50
            false,
51
        ];
52
    }
53
54
    /** @test */
55
    public function messagesFromAllNonPassingValidatorsAreWrappedUntilOnePasses(): void
56
    {
57
        $chain = new ExcludingValidatorChain(
58
            new Validator\EmailAddress(),
59
            new Validator\Between(['min' => 50, 'max' => 60]),
60
            new Validator\Digits(),
61
        );
62
63
        $this->assertTrue($chain->isValid(1000));
64
        $this->assertEquals([
65
            Validator\EmailAddress::INVALID => 'Invalid type given. String expected',
66
            Validator\Between::NOT_BETWEEN => 'The input is not between \'50\' and \'60\', inclusively',
67
        ], $chain->getMessages());
68
    }
69
}
70