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

58 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;
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;
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);
$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()]];
}
}