2019-08-08 09:20:37 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2019-08-08 09:20:37 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Exception;
|
|
|
|
|
2019-11-24 16:45:40 -06:00
|
|
|
use Fig\Http\Message\StatusCodeInterface;
|
2020-01-01 14:11:53 -06:00
|
|
|
use Laminas\InputFilter\InputFilterInterface;
|
2019-08-08 09:20:37 -05:00
|
|
|
use LogicException;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use RuntimeException;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
|
|
|
use Throwable;
|
|
|
|
|
2019-11-29 11:55:27 -06:00
|
|
|
use function array_keys;
|
2019-08-08 09:20:37 -05:00
|
|
|
use function print_r;
|
|
|
|
|
|
|
|
class ValidationExceptionTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideExceptions
|
|
|
|
*/
|
|
|
|
public function createsExceptionFromInputFilter(?Throwable $prev): void
|
|
|
|
{
|
|
|
|
$invalidData = [
|
|
|
|
'foo' => 'bar',
|
|
|
|
'something' => ['baz', 'foo'],
|
|
|
|
];
|
|
|
|
$barValue = print_r(['baz', 'foo'], true);
|
2019-11-21 13:05:06 -06:00
|
|
|
$expectedStringRepresentation = <<<EOT
|
2021-01-10 13:28:52 -06:00
|
|
|
'foo' => bar
|
|
|
|
'something' => {$barValue}
|
|
|
|
EOT;
|
2019-08-08 09:20:37 -05:00
|
|
|
|
2022-10-22 13:05:06 -05:00
|
|
|
$inputFilter = $this->createMock(InputFilterInterface::class);
|
|
|
|
$inputFilter->expects($this->once())->method('getMessages')->with()->willReturn($invalidData);
|
2019-08-08 09:20:37 -05:00
|
|
|
|
2022-10-22 13:05:06 -05:00
|
|
|
$e = ValidationException::fromInputFilter($inputFilter, $prev);
|
2019-08-08 09:20:37 -05:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertEquals($invalidData, $e->getInvalidElements());
|
|
|
|
self::assertEquals(['invalidElements' => array_keys($invalidData)], $e->getAdditionalData());
|
|
|
|
self::assertEquals('Provided data is not valid', $e->getMessage());
|
|
|
|
self::assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getCode());
|
|
|
|
self::assertEquals($prev, $e->getPrevious());
|
|
|
|
self::assertStringContainsString($expectedStringRepresentation, (string) $e);
|
2019-08-08 09:20:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideExceptions(): iterable
|
|
|
|
{
|
2021-01-11 08:20:26 -06:00
|
|
|
return [[null], [new RuntimeException()], [new LogicException()]];
|
2019-08-08 09:20:37 -05:00
|
|
|
}
|
|
|
|
}
|