2019-08-08 17:20:37 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2019-08-08 17:20:37 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Exception;
|
|
|
|
|
2019-11-25 01:45:40 +03:00
|
|
|
use Fig\Http\Message\StatusCodeInterface;
|
2019-08-08 17:20:37 +03:00
|
|
|
use LogicException;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use RuntimeException;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
|
|
|
use Throwable;
|
|
|
|
use Zend\InputFilter\InputFilterInterface;
|
|
|
|
|
2019-11-29 20:55:27 +03:00
|
|
|
use function array_keys;
|
2019-08-08 17:20:37 +03: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 22:05:06 +03:00
|
|
|
$expectedStringRepresentation = <<<EOT
|
2019-08-08 17:20:37 +03:00
|
|
|
'foo' => bar
|
|
|
|
'something' => {$barValue}
|
|
|
|
EOT;
|
|
|
|
|
|
|
|
$inputFilter = $this->prophesize(InputFilterInterface::class);
|
|
|
|
$getMessages = $inputFilter->getMessages()->willReturn($invalidData);
|
|
|
|
|
|
|
|
$e = ValidationException::fromInputFilter($inputFilter->reveal());
|
|
|
|
|
|
|
|
$this->assertEquals($invalidData, $e->getInvalidElements());
|
2019-11-29 20:55:27 +03:00
|
|
|
$this->assertEquals(['invalidElements' => array_keys($invalidData)], $e->getAdditionalData());
|
2019-11-21 22:05:06 +03:00
|
|
|
$this->assertEquals('Provided data is not valid', $e->getMessage());
|
2019-11-25 01:45:40 +03:00
|
|
|
$this->assertEquals(StatusCodeInterface::STATUS_BAD_REQUEST, $e->getCode());
|
2019-08-08 17:20:37 +03:00
|
|
|
$this->assertEquals($prev, $e->getPrevious());
|
2019-11-21 22:05:06 +03:00
|
|
|
$this->assertStringContainsString($expectedStringRepresentation, (string) $e);
|
2019-08-08 17:20:37 +03:00
|
|
|
$getMessages->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideExceptions(): iterable
|
|
|
|
{
|
|
|
|
return [[null, new RuntimeException(), new LogicException()]];
|
|
|
|
}
|
|
|
|
}
|