From 4e17c221508fdec713920da84624aa01038714e7 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Wed, 6 Mar 2024 17:31:08 +0300 Subject: [PATCH] Raise `yiisoft/hydrator-validator` to `^2.0` --- composer.json | 2 +- src/RequestInputParametersResolver.php | 2 +- tests/RequestInputParametersResolverTest.php | 14 +++++++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index d69180f..8987abc 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "psr/http-server-middleware": "^1.0", "yiisoft/arrays": "^3.0", "yiisoft/hydrator": "^1.0", - "yiisoft/hydrator-validator": "^1.0", + "yiisoft/hydrator-validator": "^2.0", "yiisoft/middleware-dispatcher": "^5.1", "yiisoft/validator": "^1.1", "yiisoft/request-provider": "^1.0" diff --git a/src/RequestInputParametersResolver.php b/src/RequestInputParametersResolver.php index 0fe7a74..e440c9b 100644 --- a/src/RequestInputParametersResolver.php +++ b/src/RequestInputParametersResolver.php @@ -78,7 +78,7 @@ private function processValidation(mixed $value): void } $result = $value->getValidationResult(); - if ($result !== null && !$result->isValid()) { + if (!$result->isValid()) { throw new InputValidationException($result); } } diff --git a/tests/RequestInputParametersResolverTest.php b/tests/RequestInputParametersResolverTest.php index f8e934b..ecd06c0 100644 --- a/tests/RequestInputParametersResolverTest.php +++ b/tests/RequestInputParametersResolverTest.php @@ -5,6 +5,7 @@ namespace Yiisoft\Input\Http\Tests; use Closure; +use LogicException; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; @@ -98,8 +99,11 @@ public function testWithNonValidatingHydrator(): void $input = $result['input']; $this->assertInstanceOf(PersonInput::class, $input); - $this->assertNull($input->getValidationResult()); $this->assertSame('Bo', $input->name); + + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Validation result is not set.'); + $input->getValidationResult(); } public function testDoNotThrowExceptionForNonValidatedInput(): void @@ -141,7 +145,7 @@ public function testDoNotThrowExceptionForValidInput(): void $this->assertSame('Bo', $result['input']->name); } - public function testDoNotThrowExceptionForValidatedInputWithoutValidation(): void + public function testValidatedInputWithoutValidation(): void { $request = $this->createMock(ServerRequestInterface::class); $request->method('getQueryParams')->willReturn(['name' => 'Bo']); @@ -153,9 +157,9 @@ public function testDoNotThrowExceptionForValidatedInputWithoutValidation(): voi ); $parameters = TestHelper::getParameters(static fn(PersonInput $input) => null); - $result = $resolver->resolve($parameters, $request); - - $this->assertSame('Bo', $result['input']->name); + $this->expectException(LogicException::class); + $this->expectExceptionMessage('Validation result is not set.'); + $resolver->resolve($parameters, $request); } public function testThrowExceptionForInvalidInput(): void