diff --git a/.github/workflows/mutation.yml b/.github/workflows/mutation.yml index c7fccb9..a0eceb0 100644 --- a/.github/workflows/mutation.yml +++ b/.github/workflows/mutation.yml @@ -28,6 +28,6 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['8.3'] + ['8.4'] secrets: STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} diff --git a/composer.json b/composer.json index 8325685..d389032 100644 --- a/composer.json +++ b/composer.json @@ -46,6 +46,7 @@ }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.93", + "httpsoft/http-message": "^1.1", "maglnet/composer-require-checker": "^4.7.1", "phpunit/phpunit": "^10.5.45", "rector/rector": "^2.0.11", @@ -85,6 +86,7 @@ "scripts": { "test": "phpunit --testdox --no-interaction", "test-watch": "phpunit-watcher watch", - "cs-fix": "php-cs-fixer fix" + "cs-fix": "php-cs-fixer fix", + "infection": "infection --threads=max" } } diff --git a/tests/Attribute/Parameter/BodyTest.php b/tests/Attribute/Parameter/BodyTest.php index eee0ed4..0a2874e 100644 --- a/tests/Attribute/Parameter/BodyTest.php +++ b/tests/Attribute/Parameter/BodyTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Input\Http\Tests\Attribute\Parameter; +use HttpSoft\Message\ServerRequest; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; use Yiisoft\Hydrator\AttributeHandling\Exception\UnexpectedAttributeException; @@ -78,6 +79,23 @@ public function testNonExistPath(): void $this->assertSame('', $input->a); } + public function testNonExistPathReturnsFailResult(): void + { + $request = new ServerRequest(parsedBody: ['a' => 'one']); + + $requestProvider = new RequestProvider(); + $requestProvider->set($request); + + $resolver = new BodyResolver($requestProvider); + + $attribute = new Body('non-existing-key'); + $context = TestHelper::createParameterAttributeResolveContext(); + + $result = $resolver->getParameterValue($attribute, $context); + + $this->assertFalse($result->isResolved()); + } + public function testUnexpectedAttributeException(): void { $resolver = new BodyResolver($this->createMock(RequestProviderInterface::class)); diff --git a/tests/Attribute/Parameter/RequestTest.php b/tests/Attribute/Parameter/RequestTest.php index 5cfcf35..19ab8c4 100644 --- a/tests/Attribute/Parameter/RequestTest.php +++ b/tests/Attribute/Parameter/RequestTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Input\Http\Tests\Attribute\Parameter; +use HttpSoft\Message\ServerRequest; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; use Yiisoft\Hydrator\AttributeHandling\Exception\UnexpectedAttributeException; @@ -78,6 +79,23 @@ public function testNonExistPath(): void $this->assertSame('', $input->a); } + public function testNonExistPathReturnsFailResult(): void + { + $request = (new ServerRequest())->withAttribute('a', 'one'); + + $requestProvider = new RequestProvider(); + $requestProvider->set($request); + + $resolver = new RequestResolver($requestProvider); + + $attribute = new Request('non-existing-key'); + $context = TestHelper::createParameterAttributeResolveContext(); + + $result = $resolver->getParameterValue($attribute, $context); + + $this->assertFalse($result->isResolved()); + } + public function testUnexpectedAttributeException(): void { $resolver = new RequestResolver($this->createMock(RequestProviderInterface::class)); diff --git a/tests/HydratorAttributeParametersResolver/CustomHydrator/CustomHydratorTest.php b/tests/HydratorAttributeParametersResolver/CustomHydrator/CustomHydratorTest.php new file mode 100644 index 0000000..fb74a7f --- /dev/null +++ b/tests/HydratorAttributeParametersResolver/CustomHydrator/CustomHydratorTest.php @@ -0,0 +1,55 @@ + 'Vasya']); + $requestProvider = new RequestProvider(); + $requestProvider->set($request); + + $resolver = new HydratorAttributeParametersResolver( + TestHelper::createParameterAttributesHandler([ + BodyResolver::class => new BodyResolver($requestProvider), + ]), + typeCaster: new CallableTypeCaster( + static fn(mixed $value, TypeCastContext $context) => $context->getHydrator()->create(User::class, [ + 'name' => $value, + ]), + ), + hydrator: new Hydrator( + new CallableTypeCaster( + static fn(mixed $value) => 'The ' . $value, + ), + ), + ); + + $parameters = TestHelper::getParameters( + static fn( + #[Body] + User $user, + ) => null, + ); + + $result = $resolver->resolve($parameters, $request); + + $this->assertSame(['user'], array_keys($result)); + $this->assertInstanceOf(User::class, $result['user']); + $this->assertSame('The Vasya', $result['user']->name); + } +} diff --git a/tests/HydratorAttributeParametersResolver/CustomHydrator/User.php b/tests/HydratorAttributeParametersResolver/CustomHydrator/User.php new file mode 100644 index 0000000..b9895f6 --- /dev/null +++ b/tests/HydratorAttributeParametersResolver/CustomHydrator/User.php @@ -0,0 +1,12 @@ +callable)($value)); + return Result::success(($this->callable)($value, $context)); } }