From 564b65c8ca63713e5bb3aefdf4a0e59b49b9b564 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 8 Aug 2019 16:20:37 +0200 Subject: [PATCH] Created ValidationExceptionTest --- .../src/Exception/ValidationException.php | 27 ++----- .../Exception/ValidationExceptionTest.php | 81 +++++++++++++++++++ 2 files changed, 87 insertions(+), 21 deletions(-) create mode 100644 module/Core/test/Exception/ValidationExceptionTest.php diff --git a/module/Core/src/Exception/ValidationException.php b/module/Core/src/Exception/ValidationException.php index 6058c6a6..f8d14c55 100644 --- a/module/Core/src/Exception/ValidationException.php +++ b/module/Core/src/Exception/ValidationException.php @@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Exception; use Throwable; use Zend\InputFilter\InputFilterInterface; +use function Functional\reduce_left; use function is_array; use function print_r; use function sprintf; @@ -27,21 +28,11 @@ class ValidationException extends RuntimeException parent::__construct($message, $code, $previous); } - /** - * @param InputFilterInterface $inputFilter - * @param \Throwable|null $prev - * @return ValidationException - */ public static function fromInputFilter(InputFilterInterface $inputFilter, ?Throwable $prev = null): self { 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 { 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 = ''; - foreach ($messages as $name => $messageSet) { - $text .= sprintf( - "\n\t'%s' => %s", + return reduce_left($messages, function ($messageSet, $name, $_, string $acc) { + return $acc . sprintf( + "\n '%s' => %s", $name, is_array($messageSet) ? print_r($messageSet, true) : $messageSet ); - } - - return $text; + }, ''); } - /** - * @return array - */ public function getInvalidElements(): array { return $this->invalidElements; diff --git a/module/Core/test/Exception/ValidationExceptionTest.php b/module/Core/test/Exception/ValidationExceptionTest.php new file mode 100644 index 00000000..4b7af6d2 --- /dev/null +++ b/module/Core/test/Exception/ValidationExceptionTest.php @@ -0,0 +1,81 @@ +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 = << 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()]]; + } +}