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

53 lines
1.7 KiB
PHP
Raw Normal View History

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;
use Fig\Http\Message\StatusCodeInterface;
2020-01-01 23:11:53 +03:00
use Laminas\InputFilter\InputFilterInterface;
2019-08-08 17:20:37 +03:00
use LogicException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
2019-08-08 17:20:37 +03:00
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Shlinkio\Shlink\Core\Exception\ValidationException;
use Throwable;
use function array_keys;
2019-08-08 17:20:37 +03:00
use function print_r;
class ValidationExceptionTest extends TestCase
{
#[Test, DataProvider('provideExceptions')]
2019-08-08 17:20:37 +03:00
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 17:20:37 +03:00
$inputFilter = $this->createMock(InputFilterInterface::class);
$inputFilter->expects($this->once())->method('getMessages')->with()->willReturn($invalidData);
2019-08-08 17:20:37 +03:00
$e = ValidationException::fromInputFilter($inputFilter, $prev);
2019-08-08 17:20:37 +03:00
2020-10-04 01:35:14 +03: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 17:20:37 +03:00
}
2023-02-09 11:32:38 +03:00
public static function provideExceptions(): iterable
2019-08-08 17:20:37 +03:00
{
return [[null], [new RuntimeException()], [new LogicException()]];
2019-08-08 17:20:37 +03:00
}
}