shlink/module/Core/test/Exception/ValidationExceptionTest.php

54 lines
1.7 KiB
PHP
Raw Normal View History

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;
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;
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);
$expectedStringRepresentation = <<<EOT
'foo' => bar
'something' => {$barValue}
EOT;
2019-08-08 09:20:37 -05:00
$inputFilter = $this->createMock(InputFilterInterface::class);
$inputFilter->expects($this->once())->method('getMessages')->with()->willReturn($invalidData);
2019-08-08 09:20:37 -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
{
return [[null], [new RuntimeException()], [new LogicException()]];
2019-08-08 09:20:37 -05:00
}
}