Files
michaelschiemer/tests/Framework/Attributes/Execution/CallbackExecutorTest.php
2025-11-24 21:28:25 +01:00

99 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Framework\Attributes\Execution;
use App\Framework\Attributes\Execution\AttributeExecutionContext;
use App\Framework\Attributes\Execution\CallbackExecutor;
use App\Framework\Attributes\Execution\CallbackMetadata;
use App\Framework\Attributes\Execution\CallbackType;
use App\Framework\Attributes\Execution\Handlers\PermissionGuard;
use App\Framework\Attributes\Execution\Policies\Policies;
use App\Framework\Attributes\Execution\Policies\UserPolicies;
use App\Framework\DI\Container;
use App\Framework\DI\DefaultContainer;
use App\Framework\DI\MethodInvoker;
use App\Framework\Reflection\SimpleReflectionService;
use PHPUnit\Framework\TestCase;
final class CallbackExecutorTest extends TestCase
{
private Container $container;
private CallbackExecutor $executor;
protected function setUp(): void
{
$this->container = new DefaultContainer();
$this->container->instance(MethodInvoker::class, new MethodInvoker(
$this->container,
new SimpleReflectionService()
));
$this->executor = new CallbackExecutor($this->container);
}
public function testExecuteHandler(): void
{
$metadata = CallbackMetadata::fromHandler(PermissionGuard::class, [['edit_post']]);
$context = AttributeExecutionContext::forClass(
$this->container,
\App\Framework\Core\ValueObjects\ClassName::create('TestClass')
);
$result = $this->executor->execute($metadata, $context);
// PermissionGuard gibt true zurück (Placeholder-Implementierung)
$this->assertIsBool($result);
}
public function testExecuteStaticMethod(): void
{
$metadata = CallbackMetadata::fromCallable([UserPolicies::class, 'isAdmin']);
$context = AttributeExecutionContext::forClass(
$this->container,
\App\Framework\Core\ValueObjects\ClassName::create('TestClass')
);
$result = $this->executor->execute($metadata, $context);
// UserPolicies::isAdmin gibt bool zurück
$this->assertIsBool($result);
}
public function testExecuteFactory(): void
{
$metadata = CallbackMetadata::fromFactory(
Policies::class,
'requirePermission',
['edit_post']
);
$context = AttributeExecutionContext::forClass(
$this->container,
\App\Framework\Core\ValueObjects\ClassName::create('TestClass')
);
$closure = $this->executor->execute($metadata, $context);
$this->assertInstanceOf(\Closure::class, $closure);
// Führe Closure aus
$result = $closure($context);
$this->assertIsBool($result);
}
public function testExecuteThrowsForClosure(): void
{
$metadata = new CallbackMetadata(CallbackType::CLOSURE, '');
$context = AttributeExecutionContext::forClass(
$this->container,
\App\Framework\Core\ValueObjects\ClassName::create('TestClass')
);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Closure execution not supported via metadata');
$this->executor->execute($metadata, $context);
}
}