Passed
Pull Request — master (#448)
by Alejandro
06:34
created

ValidationExceptionTest::provideExceptions()   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
eloc 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace ShlinkioTest\Shlink\Core\Exception;
5
6
use LogicException;
7
use PHPUnit\Framework\TestCase;
8
use RuntimeException;
9
use Shlinkio\Shlink\Core\Exception\ValidationException;
10
use Throwable;
11
use Zend\InputFilter\InputFilterInterface;
12
13
use function print_r;
14
use function random_int;
15
16
class ValidationExceptionTest extends TestCase
17
{
18
    /**
19
     * @test
20
     * @dataProvider provideExceptionData
21
     */
22
    public function createsExceptionWrappingExpectedData(
23
        array $args,
24
        string $expectedMessage,
25
        array $expectedInvalidElements,
26
        int $expectedCode,
27
        ?Throwable $expectedPrev
28
    ): void {
29
        $e = new ValidationException(...$args);
0 ignored issues
show
Bug introduced by
$args is expanded, but the parameter $message of Shlinkio\Shlink\Core\Exc...xception::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        $e = new ValidationException(/** @scrutinizer ignore-type */ ...$args);
Loading history...
30
31
        $this->assertEquals($expectedMessage, $e->getMessage());
32
        $this->assertEquals($expectedInvalidElements, $e->getInvalidElements());
33
        $this->assertEquals($expectedCode, $e->getCode());
34
        $this->assertEquals($expectedPrev, $e->getPrevious());
35
    }
36
37
    public function provideExceptionData(): iterable
38
    {
39
        yield 'empty args' => [[], '', [], 0, null];
40
        yield 'with message' => [['something'], 'something', [], 0, null];
41
        yield 'with elements' => [['something_else', [1, 2, 3]], 'something_else', [1, 2, 3], 0, null];
42
        yield 'with code' => [['foo', [], $foo = random_int(-100, 100)], 'foo', [], $foo, null];
43
        yield 'with prev' => [['bar', [], 8, $e = new RuntimeException()], 'bar', [], 8, $e];
44
    }
45
46
    /**
47
     * @test
48
     * @dataProvider provideExceptions
49
     */
50
    public function createsExceptionFromInputFilter(?Throwable $prev): void
51
    {
52
        $invalidData = [
53
            'foo' => 'bar',
54
            'something' => ['baz', 'foo'],
55
        ];
56
        $barValue = print_r(['baz', 'foo'], true);
57
        $expectedMessage = <<<EOT
58
Provided data is not valid. These are the messages:
59
60
    'foo' => bar
61
    'something' => {$barValue}
62
63
EOT;
64
65
        $inputFilter = $this->prophesize(InputFilterInterface::class);
66
        $getMessages = $inputFilter->getMessages()->willReturn($invalidData);
67
68
        $e = ValidationException::fromInputFilter($inputFilter->reveal());
69
70
        $this->assertEquals($invalidData, $e->getInvalidElements());
71
        $this->assertEquals($expectedMessage, $e->getMessage());
72
        $this->assertEquals(-1, $e->getCode());
73
        $this->assertEquals($prev, $e->getPrevious());
74
        $getMessages->shouldHaveBeenCalledOnce();
75
    }
76
77
    public function provideExceptions(): iterable
78
    {
79
        return [[null, new RuntimeException(), new LogicException()]];
80
    }
81
}
82