mirror of
https://github.com/shlinkio/shlink.git
synced 2025-01-22 14:33:16 -06:00
Created ValidationExceptionTest
This commit is contained in:
parent
9de0cf5c03
commit
564b65c8ca
@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Exception;
|
|||||||
use Throwable;
|
use Throwable;
|
||||||
use Zend\InputFilter\InputFilterInterface;
|
use Zend\InputFilter\InputFilterInterface;
|
||||||
|
|
||||||
|
use function Functional\reduce_left;
|
||||||
use function is_array;
|
use function is_array;
|
||||||
use function print_r;
|
use function print_r;
|
||||||
use function sprintf;
|
use function sprintf;
|
||||||
@ -27,21 +28,11 @@ class ValidationException extends RuntimeException
|
|||||||
parent::__construct($message, $code, $previous);
|
parent::__construct($message, $code, $previous);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param InputFilterInterface $inputFilter
|
|
||||||
* @param \Throwable|null $prev
|
|
||||||
* @return ValidationException
|
|
||||||
*/
|
|
||||||
public static function fromInputFilter(InputFilterInterface $inputFilter, ?Throwable $prev = null): self
|
public static function fromInputFilter(InputFilterInterface $inputFilter, ?Throwable $prev = null): self
|
||||||
{
|
{
|
||||||
return static::fromArray($inputFilter->getMessages(), $prev);
|
return static::fromArray($inputFilter->getMessages(), $prev);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $invalidData
|
|
||||||
* @param \Throwable|null $prev
|
|
||||||
* @return ValidationException
|
|
||||||
*/
|
|
||||||
private static function fromArray(array $invalidData, ?Throwable $prev = null): self
|
private static function fromArray(array $invalidData, ?Throwable $prev = null): self
|
||||||
{
|
{
|
||||||
return new self(
|
return new self(
|
||||||
@ -57,23 +48,17 @@ class ValidationException extends RuntimeException
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function formMessagesToString(array $messages = [])
|
private static function formMessagesToString(array $messages = []): string
|
||||||
{
|
{
|
||||||
$text = '';
|
return reduce_left($messages, function ($messageSet, $name, $_, string $acc) {
|
||||||
foreach ($messages as $name => $messageSet) {
|
return $acc . sprintf(
|
||||||
$text .= sprintf(
|
"\n '%s' => %s",
|
||||||
"\n\t'%s' => %s",
|
|
||||||
$name,
|
$name,
|
||||||
is_array($messageSet) ? print_r($messageSet, true) : $messageSet
|
is_array($messageSet) ? print_r($messageSet, true) : $messageSet
|
||||||
);
|
);
|
||||||
}
|
}, '');
|
||||||
|
|
||||||
return $text;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getInvalidElements(): array
|
public function getInvalidElements(): array
|
||||||
{
|
{
|
||||||
return $this->invalidElements;
|
return $this->invalidElements;
|
||||||
|
81
module/Core/test/Exception/ValidationExceptionTest.php
Normal file
81
module/Core/test/Exception/ValidationExceptionTest.php
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ShlinkioTest\Shlink\Core\Exception;
|
||||||
|
|
||||||
|
use LogicException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use RuntimeException;
|
||||||
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||||
|
use Throwable;
|
||||||
|
use Zend\InputFilter\InputFilterInterface;
|
||||||
|
|
||||||
|
use function print_r;
|
||||||
|
use function random_int;
|
||||||
|
|
||||||
|
class ValidationExceptionTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @dataProvider provideExceptionData
|
||||||
|
*/
|
||||||
|
public function createsExceptionWrappingExpectedData(
|
||||||
|
array $args,
|
||||||
|
string $expectedMessage,
|
||||||
|
array $expectedInvalidElements,
|
||||||
|
int $expectedCode,
|
||||||
|
?Throwable $expectedPrev
|
||||||
|
): void {
|
||||||
|
$e = new ValidationException(...$args);
|
||||||
|
|
||||||
|
$this->assertEquals($expectedMessage, $e->getMessage());
|
||||||
|
$this->assertEquals($expectedInvalidElements, $e->getInvalidElements());
|
||||||
|
$this->assertEquals($expectedCode, $e->getCode());
|
||||||
|
$this->assertEquals($expectedPrev, $e->getPrevious());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideExceptionData(): iterable
|
||||||
|
{
|
||||||
|
yield 'empty args' => [[], '', [], 0, null];
|
||||||
|
yield 'with message' => [['something'], 'something', [], 0, null];
|
||||||
|
yield 'with elements' => [['something_else', [1, 2, 3]], 'something_else', [1, 2, 3], 0, null];
|
||||||
|
yield 'with code' => [['foo', [], $foo = random_int(-100, 100)], 'foo', [], $foo, null];
|
||||||
|
yield 'with prev' => [['bar', [], 8, $e = new RuntimeException()], 'bar', [], 8, $e];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @dataProvider provideExceptions
|
||||||
|
*/
|
||||||
|
public function createsExceptionFromInputFilter(?Throwable $prev): void
|
||||||
|
{
|
||||||
|
$invalidData = [
|
||||||
|
'foo' => 'bar',
|
||||||
|
'something' => ['baz', 'foo'],
|
||||||
|
];
|
||||||
|
$barValue = print_r(['baz', 'foo'], true);
|
||||||
|
$expectedMessage = <<<EOT
|
||||||
|
Provided data is not valid. These are the messages:
|
||||||
|
|
||||||
|
'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());
|
||||||
|
$this->assertEquals($expectedMessage, $e->getMessage());
|
||||||
|
$this->assertEquals(-1, $e->getCode());
|
||||||
|
$this->assertEquals($prev, $e->getPrevious());
|
||||||
|
$getMessages->shouldHaveBeenCalledOnce();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideExceptions(): iterable
|
||||||
|
{
|
||||||
|
return [[null, new RuntimeException(), new LogicException()]];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user