42 lines
1.4 KiB
PHP
42 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\Infra\Adapters\Services;
|
|
|
|
use App\Models\Tag;
|
|
use Infrastructure\Adapters\Services\LaravelTransactionManager;
|
|
|
|
describe('LaravelTransactionManager', function (): void {
|
|
it('commits database writes and returns callback result when callback succeeds', function (): void {
|
|
// GIVEN
|
|
$manager = new LaravelTransactionManager();
|
|
|
|
// WHEN
|
|
$result = $manager->execute(static function (): string {
|
|
Tag::factory()->createOne(['label' => 'Tx Committed Tag']);
|
|
|
|
return 'success';
|
|
});
|
|
|
|
// THEN
|
|
expect($result)->toBe('success')
|
|
->and(Tag::query()->where('label', 'Tx Committed Tag')->exists())->toBeTrue();
|
|
});
|
|
|
|
it('rolls back database writes when callback throws an exception', function (): void {
|
|
// GIVEN
|
|
$manager = new LaravelTransactionManager();
|
|
|
|
// WHEN
|
|
$action = static fn (): mixed => $manager->execute(static function (): never {
|
|
Tag::factory()->createOne(['label' => 'Tx Rolled Back Tag']);
|
|
|
|
throw new \RuntimeException('Transaction failure');
|
|
});
|
|
|
|
// THEN
|
|
expect($action)->toThrow(\RuntimeException::class, 'Transaction failure')
|
|
->and(Tag::query()->where('label', 'Tx Rolled Back Tag')->exists())->toBeFalse();
|
|
});
|
|
});
|