From 569ed0360a6313f011150140ef5c7dee84368cc5 Mon Sep 17 00:00:00 2001 From: Rodolfo Berrios <20590102+rodber@users.noreply.github.com> Date: Tue, 19 Dec 2023 10:38:06 -0300 Subject: [PATCH] test attributes --- src/Traits/ActionTrait.php | 36 +++++++++++++++++++++-------- tests/ActionTest.php | 14 +++++++++++ tests/src/ActionTestAttributes.php | 37 ++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 tests/src/ActionTestAttributes.php diff --git a/src/Traits/ActionTrait.php b/src/Traits/ActionTrait.php index 7b525b0..41ae3c7 100644 --- a/src/Traits/ActionTrait.php +++ b/src/Traits/ActionTrait.php @@ -26,7 +26,6 @@ use TypeError; use function Chevere\Action\getParameters; use function Chevere\Message\message; -use function Chevere\Parameter\arguments; use function Chevere\Parameter\mixed; use function Chevere\Parameter\reflectionToReturnParameter; @@ -44,19 +43,27 @@ final public function __invoke(mixed ...$argument): CastInterface static::assert(); // @infection-ignore-all $this->assertRuntime(); - $arguments = arguments($this->parameters(), $argument)->toArray(); - $run = $this->run(...$arguments); try { - static::return()->__invoke($run); + $arguments = $this->parameters()->__invoke(...$argument); } catch (Throwable $e) { - $message = (string) message( - '`%method%` → %message%', - method: static::runMethodFQN(), - message: $e->getMessage(), - ); + throw new ($e::class)($this->getInvokeErrorMessage($e)); + } + $run = $this->run(...$arguments->toArray()); + $reflection = new ReflectionMethod(static::class, static::runMethod()); + $attribute = $reflection->getAttributes(ReturnAttr::class)[0] ?? null; + if ($attribute === null) { + $return = static::return(); + } else { + $attribute = $attribute->newInstance(); + /** @var ReturnAttr $attribute */ + $return = $attribute->parameter(); + } - throw new ($e::class)($message); + try { + $return->__invoke($run); + } catch (Throwable $e) { + throw new ($e::class)($this->getInvokeErrorMessage($e)); } return new Cast($run); @@ -87,6 +94,15 @@ public static function runMethod(): string return 'run'; } + protected function getInvokeErrorMessage(Throwable $e): string + { + return (string) message( + '`%method%` → %message%', + method: static::runMethodFQN(), + message: $e->getMessage(), + ); + } + /** * @return array [$method, $return] */ diff --git a/tests/ActionTest.php b/tests/ActionTest.php index e743fd6..1ecb5f2 100644 --- a/tests/ActionTest.php +++ b/tests/ActionTest.php @@ -16,6 +16,7 @@ use ArgumentCountError; use Chevere\Tests\src\ActionTestAction; use Chevere\Tests\src\ActionTestArrayAccessReturnType; +use Chevere\Tests\src\ActionTestAttributes; use Chevere\Tests\src\ActionTestController; use Chevere\Tests\src\ActionTestGenericResponse; use Chevere\Tests\src\ActionTestGenericResponseError; @@ -151,4 +152,17 @@ public function testParametersNullAssign(): void $action->__invoke(); $reflection->getValue($action); } + + public function testAttributeValidation(): void + { + $action = new ActionTestAttributes(); + $this->assertSame(1, $action->__invoke(value: 'ab')->int()); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + <<__invoke(value: 'ac'); + } } diff --git a/tests/src/ActionTestAttributes.php b/tests/src/ActionTestAttributes.php new file mode 100644 index 0000000..87d7f39 --- /dev/null +++ b/tests/src/ActionTestAttributes.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Chevere\Tests\src; + +use Chevere\Action\Action; +use Chevere\Parameter\Attributes\IntAttr; +use Chevere\Parameter\Attributes\ReturnAttr; +use Chevere\Parameter\Attributes\StringAttr; +use Chevere\Parameter\Interfaces\ParameterInterface; +use function Chevere\Parameter\string; + +final class ActionTestAttributes extends Action +{ + public static function return(): ParameterInterface + { + return string(); + } + + #[ReturnAttr(new IntAttr(min: 1))] + protected function run( + #[StringAttr('/^ab$/')] + string $value + ): int { + return 1; + } +}