ldc-cocotte/tests/Integration/WithApp/App/Services/ActionExecutorServiceTest.php
Jérémy Munsch 98e580d327
refactor(tags): implement CRUD actions for tags
- Added CreateTag, UpdateTag, and DeleteTag actions.
- Introduced CreateTagInput and UpdateTagInput for input validation.
- Implemented ActionExecutor for executing actions with logging.
- Added tests for all new actions and input validation.
2026-10-27 09:58:30 +01:00

86 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Contracts\Action;
use App\Contracts\ActionInput;
use App\Services\ActionExecutorService;
use Psr\Log\AbstractLogger;
describe('ActionExecutorService', function (): void {
it('executes action in transaction and logs success', function (): void {
// GIVEN
$logger = new class () extends AbstractLogger
{
/** @var array<int, string> */
public array $logs = [];
#[\Override]
public function log($level, string | \Stringable $message, array $context = []): void
{
$this->logs[] = (string) $message;
}
};
$action = new class () implements Action
{
#[\Override]
public function __invoke(ActionInput $input): string
{
return 'done';
}
};
$input = new class () implements ActionInput
{
};
$executor = new ActionExecutorService($logger);
// WHEN
$result = $executor->execute($action, $input);
// THEN
expect($result)->toBe('done')
->and($logger->logs)->toHaveCount(2)
->and($logger->logs[0])->toContain('Executing')
->and($logger->logs[1])->toContain('Succeeded');
});
it('logs error and rethrows when action fails', function (): void {
// GIVEN
$logger = new class () extends AbstractLogger
{
/** @var array<int, string> */
public array $logs = [];
#[\Override]
public function log($level, string | \Stringable $message, array $context = []): void
{
$this->logs[] = (string) $message;
}
};
$action = new class () implements Action
{
#[\Override]
public function __invoke(ActionInput $input): never
{
throw new \RuntimeException('Database failure');
}
};
$input = new class () implements ActionInput
{
};
$executor = new ActionExecutorService($logger);
// WHEN
expect(fn () => $executor->execute($action, $input))
// THEN
->toThrow(\RuntimeException::class, 'Database failure');
expect($logger->logs)->toHaveCount(2)
->and($logger->logs[0])->toContain('Executing')
->and($logger->logs[1])->toContain('Failed');
});
});