mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Improved ValidationException to avoid polluting the message with invalid data but keeping it on the string representation
This commit is contained in:
@@ -34,24 +34,34 @@ class ValidationException extends RuntimeException
|
||||
return static::fromArray($inputFilter->getMessages(), $prev);
|
||||
}
|
||||
|
||||
private static function fromArray(array $invalidData, ?Throwable $prev = null): self
|
||||
public static function fromArray(array $invalidData, ?Throwable $prev = null): self
|
||||
{
|
||||
return new self(
|
||||
sprintf(
|
||||
'Provided data is not valid. These are the messages:%s%s%s',
|
||||
PHP_EOL,
|
||||
self::formMessagesToString($invalidData),
|
||||
PHP_EOL
|
||||
),
|
||||
$invalidData,
|
||||
-1,
|
||||
$prev
|
||||
return new self('Provided data is not valid', $invalidData, -1, $prev);
|
||||
}
|
||||
|
||||
public function getInvalidElements(): array
|
||||
{
|
||||
return $this->invalidElements;
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf(
|
||||
'%s %s in %s:%s%s%sStack trace:%s%s',
|
||||
__CLASS__,
|
||||
$this->getMessage(),
|
||||
$this->getFile(),
|
||||
$this->getLine(),
|
||||
$this->invalidElementsToString(),
|
||||
PHP_EOL,
|
||||
PHP_EOL,
|
||||
$this->getTraceAsString()
|
||||
);
|
||||
}
|
||||
|
||||
private static function formMessagesToString(array $messages = []): string
|
||||
private function invalidElementsToString(): string
|
||||
{
|
||||
return reduce_left($messages, function ($messageSet, $name, $_, string $acc) {
|
||||
return reduce_left($this->invalidElements, function ($messageSet, string $name, $_, string $acc) {
|
||||
return $acc . sprintf(
|
||||
"\n '%s' => %s",
|
||||
$name,
|
||||
@@ -59,9 +69,4 @@ class ValidationException extends RuntimeException
|
||||
);
|
||||
}, '');
|
||||
}
|
||||
|
||||
public function getInvalidElements(): array
|
||||
{
|
||||
return $this->invalidElements;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,12 +55,9 @@ class ValidationExceptionTest extends TestCase
|
||||
'something' => ['baz', 'foo'],
|
||||
];
|
||||
$barValue = print_r(['baz', 'foo'], true);
|
||||
$expectedMessage = <<<EOT
|
||||
Provided data is not valid. These are the messages:
|
||||
|
||||
$expectedStringRepresentation = <<<EOT
|
||||
'foo' => bar
|
||||
'something' => {$barValue}
|
||||
|
||||
EOT;
|
||||
|
||||
$inputFilter = $this->prophesize(InputFilterInterface::class);
|
||||
@@ -69,9 +66,10 @@ EOT;
|
||||
$e = ValidationException::fromInputFilter($inputFilter->reveal());
|
||||
|
||||
$this->assertEquals($invalidData, $e->getInvalidElements());
|
||||
$this->assertEquals($expectedMessage, $e->getMessage());
|
||||
$this->assertEquals('Provided data is not valid', $e->getMessage());
|
||||
$this->assertEquals(-1, $e->getCode());
|
||||
$this->assertEquals($prev, $e->getPrevious());
|
||||
$this->assertStringContainsString($expectedStringRepresentation, (string) $e);
|
||||
$getMessages->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user