Skip to content

Commit

Permalink
test attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Dec 19, 2023
1 parent 484b7d0 commit 569ed03
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/Traits/ActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -87,6 +94,15 @@ public static function runMethod(): string
return 'run';
}

protected function getInvokeErrorMessage(Throwable $e): string

Check warning on line 97 in src/Traits/ActionTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 test on ubuntu-latest

Escaped Mutant for Mutator "ProtectedVisibility": --- Original +++ New @@ @@ { return 'run'; } - protected function getInvokeErrorMessage(Throwable $e) : string + private function getInvokeErrorMessage(Throwable $e) : string { return (string) message('`%method%` → %message%', method: static::runMethodFQN(), message: $e->getMessage()); }

Check warning on line 97 in src/Traits/ActionTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Escaped Mutant for Mutator "ProtectedVisibility": --- Original +++ New @@ @@ { return 'run'; } - protected function getInvokeErrorMessage(Throwable $e) : string + private function getInvokeErrorMessage(Throwable $e) : string { return (string) message('`%method%` → %message%', method: static::runMethodFQN(), message: $e->getMessage()); }

Check warning on line 97 in src/Traits/ActionTrait.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 test on ubuntu-latest

Escaped Mutant for Mutator "ProtectedVisibility": --- Original +++ New @@ @@ { return 'run'; } - protected function getInvokeErrorMessage(Throwable $e) : string + private function getInvokeErrorMessage(Throwable $e) : string { return (string) message('`%method%` → %message%', method: static::runMethodFQN(), message: $e->getMessage()); }
{
return (string) message(
'`%method%` → %message%',
method: static::runMethodFQN(),
message: $e->getMessage(),
);
}

/**
* @return array<object> [$method, $return]
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
<<<PLAIN
`Chevere\Tests\src\ActionTestAttributes::run` → [value]: Argument value provided `ac` doesn't match the regex `/^ab$/`
PLAIN
);
$action->__invoke(value: 'ac');
}
}
37 changes: 37 additions & 0 deletions tests/src/ActionTestAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* 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;
}
}

0 comments on commit 569ed03

Please sign in to comment.