|
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); |
|
|
|
|
|
|
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
|
|
|
|