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;
|
2020-11-02 04:50:19 -06:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2019-08-08 09:20:37 -05:00
|
|
|
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
|
|
|
|
{
|
2020-11-02 04:50:19 -06:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-08-08 09:20:37 -05:00
|
|
|
/**
|
|
|
|
* @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
|
2019-08-08 09:20:37 -05:00
|
|
|
'foo' => bar
|
|
|
|
'something' => {$barValue}
|
|
|
|
EOT;
|
|
|
|
|
|
|
|
$inputFilter = $this->prophesize(InputFilterInterface::class);
|
|
|
|
$getMessages = $inputFilter->getMessages()->willReturn($invalidData);
|
|
|
|
|
|
|
|
$e = ValidationException::fromInputFilter($inputFilter->reveal());
|
|
|
|
|
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
|
|
|
$getMessages->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideExceptions(): iterable
|
|
|
|
{
|
|
|
|
return [[null, new RuntimeException(), new LogicException()]];
|
|
|
|
}
|
|
|
|
}
|