Skip to content

Commit

Permalink
checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Dec 25, 2023
1 parent 6ac4c0d commit 7d503fa
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/ReflectionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,14 @@ public function __construct(
) {
if (! class_exists($action)) {
throw new LogicException(
(string) message(
'Action does not exist',
)
(string) message("Action doesn't exists")
);
}
$interfaces = class_implements($action) ?: [];
if (! in_array(ActionInterface::class, $interfaces, true)) {
throw new LogicException(
(string) message(
'Action does not implement `%interface%`',
"Action doesn't implement `%interface%`",
interface: ActionInterface::class,
)
);
Expand All @@ -60,7 +58,7 @@ interface: ActionInterface::class,
if (! method_exists($action, $action::mainMethod())) {
throw new LogicException(
(string) message(
'Action does not define a `%main%` method',
"Action doesn't define a `%main%` method",
main: $action::mainMethod(),
)
);
Expand Down
1 change: 1 addition & 0 deletions src/Traits/ActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ private function getExceptionArguments(Throwable $e): array
message: $e->getMessage(),
);

// @infection-ignore-all
return [
$message,
$e,
Expand Down
19 changes: 18 additions & 1 deletion tests/ActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Chevere\Action\Exceptions\ActionException;
use Chevere\Tests\src\ActionTestAction;
use Chevere\Tests\src\ActionTestArrayAccessReturnType;
use Chevere\Tests\src\ActionTestAssertStatic;
use Chevere\Tests\src\ActionTestAttributes;
use Chevere\Tests\src\ActionTestController;
use Chevere\Tests\src\ActionTestGenericResponse;
Expand All @@ -40,7 +41,7 @@ public function testMissingMainMethod(): void
$this->expectException(ActionException::class);
$this->expectExceptionMessage(
<<<PLAIN
`Chevere\Tests\src\ActionTestMissingRun` LogicException → Action does not define a `main` method
`Chevere\Tests\src\ActionTestMissingRun` LogicException → Action doesn't define a `main` method
PLAIN
);
$action->__invoke();
Expand Down Expand Up @@ -175,4 +176,20 @@ public function testAttributeValidation(): void
);
$action->__invoke(value: 'ac');
}

public function testAssert(): void
{
$action = new ActionTestAssertStatic();
$this->assertFalse($action::isAsserted());
$action::assert();
$this->assertTrue($action::isAsserted());
}

public function testAssertInvoke(): void
{
$action = new ActionTestAssertStatic();
$this->assertFalse($action::isAsserted());
$action->__invoke();
$this->assertTrue($action::isAsserted());
}
}
67 changes: 67 additions & 0 deletions tests/ReflectionActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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;

use Chevere\Action\Interfaces\ActionInterface;
use Chevere\Action\ReflectionAction;
use Chevere\Tests\src\ActionTestMissingRun;
use Chevere\Tests\src\ActionTestPrivateScope;
use LogicException;
use PHPUnit\Framework\TestCase;

final class ReflectionActionTest extends TestCase
{
public function testActionNotExists(): void
{
$action = 'wea';
$this->expectException(LogicException::class);
$this->expectExceptionMessage(
"Action doesn't exists"
);
new ReflectionAction($action);
}

public function testActionNotImplements(): void
{
$action = __CLASS__;
$interface = ActionInterface::class;
$this->expectException(LogicException::class);
$this->expectExceptionMessage(
"Action doesn't implement `{$interface}`"
);
new ReflectionAction($action);
}

public function testActionNoMainMethod(): void
{
$action = ActionTestMissingRun::class;
$main = ActionTestMissingRun::mainMethod();
$this->expectException(LogicException::class);
$this->expectExceptionMessage(
"Action doesn't define a `{$main}` method"
);
new ReflectionAction($action);
}

public function testActionPrivateScope(): void
{
$action = ActionTestPrivateScope::class;
$main = ActionTestPrivateScope::mainMethod();
$this->expectException(LogicException::class);
$this->expectExceptionMessage(
"Action `{$main}` method can't be private"
);
new ReflectionAction($action);
}
}
41 changes: 41 additions & 0 deletions tests/src/ActionTestAssertStatic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\Action\Interfaces\ReflectionActionInterface;

final class ActionTestAssertStatic extends Action
{
protected static bool $isAsserted = false;

public function __construct()
{
self::$isAsserted = false;
}

public function main(): void
{
}

public static function isAsserted(): bool
{
return self::$isAsserted;
}

protected static function assertStatic(ReflectionActionInterface $reflection): void
{
self::$isAsserted = true;
}
}

0 comments on commit 7d503fa

Please sign in to comment.