diff --git a/CHANGELOG.md b/CHANGELOG.md index 4183d1fe..c5535ec8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,9 +9,17 @@ All notable changes to this project will be documented in this file. This projec - `FakeUnitOfWork` now has an `executions` property, counting the number of times its `execute()` method has been called. +- New `AfterCommitFailure` interface, implemented by the `AfterCommitCallbacksFailed` exception. Calling code can catch + this interface to know that the unit of work committed, but one or more after commit callbacks failed. Its + `exceptions()` method returns the exceptions thrown by the after commit callbacks. ### Changed +- `UnitOfWorkManager` now throws an `AfterCommitCallbacksFailed` exception if any after commit callback fails, + including when only one fails. Previously a single failure was rethrown as-is, which meant calling code could not + distinguish it from a unit of work that failed and did not commit. The original exception is available via the + `exceptions()` method, and is the previous exception. + - `FakeUnitOfWork` now records which execution each entry in its `sequence` belongs to, in the format `execute:/:`, e.g. `execute:2/commit:1`. Previously entries were in the format `:`, e.g. `commit:1`. As the unit of work manager handles re-attempts itself by executing the unit of diff --git a/docs/guide/application/units-of-work.md b/docs/guide/application/units-of-work.md index 425425d3..5bb54932 100644 --- a/docs/guide/application/units-of-work.md +++ b/docs/guide/application/units-of-work.md @@ -296,9 +296,30 @@ $manager->afterCommit(function (): void { ``` As the transaction has already been committed, a failing after commit callback does not prevent the remaining after -commit callbacks from being executed. Once they have all been executed, if exactly one of them failed, its exception is -thrown. If more than one failed, an `AfterCommitCallbacksFailed` exception is thrown instead. Its `exceptions()` method -returns all the exceptions in the order they were thrown, and its previous exception is the first one. +commit callbacks from being executed. Once they have all been executed, if any of them failed, an +`AfterCommitCallbacksFailed` exception is thrown - even if only one of them failed. Its `exceptions()` method returns +all the exceptions in the order they were thrown, and its previous exception is the first one. + +This exception implements the `AfterCommitFailure` interface. Catch this interface if your code needs to distinguish a +unit of work that failed and was not committed, from one that was committed but where some of the after commit work +failed: + +```php +use CloudCreativity\Modules\Contracts\Application\UnitOfWork\AfterCommitFailure; + +try { + $manager->execute(function (): void { + // some work. + }); +} catch (AfterCommitFailure $ex) { + // the unit of work committed, but some after commit work failed. + foreach ($ex->exceptions() as $failure) { + // ... + } +} +``` + +Any other exception thrown by `execute()` means the unit of work did not commit. #### After Commit Queue @@ -332,6 +353,9 @@ $manager->execute(function () use ($manager): void { As the new unit of work's after commit callbacks are queued, any failure in them is thrown by the outermost call to `execute()` along with any other after commit failures. It is not thrown by the call that started the new unit of work. +Likewise, if the new unit of work fails and is not committed, its exception is thrown to the after commit callback that +started it. If not caught, it becomes one of the after commit failures of the outermost unit of work - which did +commit. :::warning While the queue is FIFO, your after commit callbacks should **never** rely on the order they are executed in, or on diff --git a/src/Application/UnitOfWork/AfterCommitCallbacksFailed.php b/src/Application/UnitOfWork/AfterCommitCallbacksFailed.php index ece89150..3dc09832 100644 --- a/src/Application/UnitOfWork/AfterCommitCallbacksFailed.php +++ b/src/Application/UnitOfWork/AfterCommitCallbacksFailed.php @@ -12,31 +12,28 @@ namespace CloudCreativity\Modules\Application\UnitOfWork; -use CloudCreativity\Modules\Contracts\Application\ApplicationException; +use CloudCreativity\Modules\Contracts\Application\UnitOfWork\AfterCommitFailure; use RuntimeException; use Throwable; -final class AfterCommitCallbacksFailed extends RuntimeException implements ApplicationException +final class AfterCommitCallbacksFailed extends RuntimeException implements AfterCommitFailure { /** * @param non-empty-list $exceptions */ public function __construct(private readonly array $exceptions) { - assert(count($exceptions) > 1, 'Expecting more than one exception.'); + assert(count($exceptions) > 0, 'Expecting at least one exception.'); + + $count = count($exceptions); parent::__construct( - sprintf('%d after commit callbacks failed.', count($exceptions)), + $count === 1 ? '1 after commit callback failed.' : sprintf('%d after commit callbacks failed.', $count), 0, - $exceptions[0], + array_first($exceptions), ); } - /** - * Get the exceptions thrown by the after commit callbacks, in the order they were thrown. - * - * @return non-empty-list - */ public function exceptions(): array { return $this->exceptions; diff --git a/src/Application/UnitOfWork/UnitOfWorkManager.php b/src/Application/UnitOfWork/UnitOfWorkManager.php index d740f62f..0b80c5d5 100644 --- a/src/Application/UnitOfWork/UnitOfWorkManager.php +++ b/src/Application/UnitOfWork/UnitOfWorkManager.php @@ -29,6 +29,9 @@ * callback, or that starts (and commits) a new unit of work, adds to the end of that queue. The queue is drained by * the outermost call to `execute()`, so a unit of work started by an after commit callback returns before its own * after commit callbacks are executed. + * + * If any after commit callbacks fail, the outermost call to `execute()` throws an `AfterCommitFailure` once the + * queue has been drained. This indicates that the unit of work did commit, even though it threw. */ final class UnitOfWorkManager implements IUnitOfWorkManager { @@ -78,7 +81,8 @@ public function __construct( * A unit of work cannot be started while another is executing. It can however be started by an after commit * callback, as the previous unit of work has committed at that point. In this scenario, the after commit * callbacks of the new unit of work are added to the end of the queue that is already being drained, rather - * than being executed before this method returns. + * than being executed before this method returns. Any failures of those callbacks are therefore thrown by the + * outermost call to this method. */ public function execute(Closure $callback, int $attempts = 1): mixed { @@ -208,9 +212,11 @@ private function executeBeforeCommit(): void * Callbacks are executed in FIFO order, including any that are added to the queue while it is being drained. * * As the unit of work has been committed, a failing callback must not prevent the remaining callbacks from being - * executed. If exactly one callback fails, its exception is rethrown. If multiple fail, they are wrapped in a - * single exception. This includes failures of after commit callbacks belonging to a unit of work started while - * draining. + * executed. Once the queue is drained, any failures are wrapped in a single `AfterCommitCallbacksFailed` + * exception - even if only one callback failed. This allows calling code to know that the unit of work did + * commit. This includes failures of after commit callbacks belonging to a unit of work started while draining. + * + * @throws AfterCommitCallbacksFailed */ private function drain(): void { @@ -227,11 +233,7 @@ private function drain(): void $this->draining = false; - if (count($exceptions) === 1) { - throw $exceptions[0]; - } - - if (count($exceptions) > 1) { + if ($exceptions !== []) { throw new AfterCommitCallbacksFailed($exceptions); } } diff --git a/src/Contracts/Application/UnitOfWork/AfterCommitFailure.php b/src/Contracts/Application/UnitOfWork/AfterCommitFailure.php new file mode 100644 index 00000000..0addd8a2 --- /dev/null +++ b/src/Contracts/Application/UnitOfWork/AfterCommitFailure.php @@ -0,0 +1,32 @@ + + */ + public function exceptions(): array; +} diff --git a/src/Contracts/Application/UnitOfWork/UnitOfWorkManager.php b/src/Contracts/Application/UnitOfWork/UnitOfWorkManager.php index 029e837d..cb4c7d3c 100644 --- a/src/Contracts/Application/UnitOfWork/UnitOfWorkManager.php +++ b/src/Contracts/Application/UnitOfWork/UnitOfWorkManager.php @@ -13,16 +13,23 @@ namespace CloudCreativity\Modules\Contracts\Application\UnitOfWork; use Closure; +use Throwable; interface UnitOfWorkManager { /** * Execute the provided closure within a unit of work. * + * If the closure (or committing the unit of work) fails, the exception is thrown and the unit of work will not + * have committed. If the unit of work commits but one or more after commit callbacks fail, an + * `AfterCommitFailure` exception is thrown instead. This allows calling code to distinguish the two scenarios. + * * @template TReturn * @param Closure(): TReturn $callback * @param int<1, max> $attempts * @return TReturn + * @throws AfterCommitFailure if the commit succeeded but an exception was thrown afterwards. + * @throws Throwable if an exception occurs during the unit of work. */ public function execute(Closure $callback, int $attempts = 1): mixed; diff --git a/tests/Integration/Testing/FakeUnitOfWorkManagerTest.php b/tests/Integration/Testing/FakeUnitOfWorkManagerTest.php index 440afada..2ad42611 100644 --- a/tests/Integration/Testing/FakeUnitOfWorkManagerTest.php +++ b/tests/Integration/Testing/FakeUnitOfWorkManagerTest.php @@ -16,6 +16,7 @@ use CloudCreativity\Modules\Application\ApplicationException; use CloudCreativity\Modules\Application\UnitOfWork\AfterCommitCallbacksFailed; use CloudCreativity\Modules\Application\UnitOfWork\UnitOfWorkManager; +use CloudCreativity\Modules\Contracts\Application\UnitOfWork\AfterCommitFailure; use CloudCreativity\Modules\Testing\FakeExceptionReporter; use CloudCreativity\Modules\Testing\FakeUnitOfWork; use LogicException; @@ -162,14 +163,15 @@ public function it_cannot_queue_a_before_commit_callback_from_an_after_commit_ca $this->manager->execute(function (): void { $this->manager->afterCommit(fn () => $this->manager->beforeCommit($this->record('before'))); }); - } catch (ApplicationException $ex) { + } catch (AfterCommitCallbacksFailed $ex) { $actual = $ex; } - $this->assertInstanceOf(ApplicationException::class, $actual); + $previous = $this->assertSoleAfterCommitFailure($actual); + $this->assertInstanceOf(ApplicationException::class, $previous); $this->assertSame( 'Cannot queue a before commit callback when not executing a unit of work.', - $actual->getMessage(), + $previous->getMessage(), ); $this->assertSame([], $this->log); $this->assertSame(['execute:1/attempt:1', 'execute:1/commit:1'], $this->unitOfWork->sequence); @@ -327,11 +329,11 @@ public function it_does_not_retry_when_an_after_commit_callback_fails(): void }); $this->manager->afterCommit($this->record('after2')); }, 3); - } catch (LogicException $e) { + } catch (AfterCommitCallbacksFailed $e) { $actual = $e; } - $this->assertSame($ex, $actual); + $this->assertSame($ex, $this->assertSoleAfterCommitFailure($actual)); $this->assertSame(['after1', 'after2'], $this->log); $this->assertSame(['execute:1/attempt:1', 'execute:1/commit:1'], $this->unitOfWork->sequence); $this->assertSame(1, $this->unitOfWork->attempts); @@ -409,11 +411,11 @@ public function it_executes_remaining_callbacks_when_a_queued_callback_fails(): $this->manager->afterCommit($this->record('after4')); }); }); - } catch (LogicException $e) { + } catch (AfterCommitCallbacksFailed $e) { $actual = $e; } - $this->assertSame($ex, $actual); + $this->assertSame($ex, $this->assertSoleAfterCommitFailure($actual)); $this->assertSame(['after1', 'after2', 'after3', 'after4'], $this->log); $this->assertSame(['execute:1/attempt:1', 'execute:1/commit:1'], $this->unitOfWork->sequence); $this->assertSame([], $this->unitOfWork->exceptions->reported); @@ -701,11 +703,11 @@ public function it_executes_remaining_after_commit_callbacks_when_a_nested_unit_ }); $this->manager->afterCommit($this->record('outer:after')); }); - } catch (LogicException $e) { + } catch (AfterCommitCallbacksFailed $e) { $actual = $e; } - $this->assertSame($ex, $actual); + $this->assertSame($ex, $this->assertSoleAfterCommitFailure($actual)); $this->assertSame(['outer:after'], $this->log); $this->assertSame([ 'execute:1/attempt:1', @@ -741,11 +743,11 @@ public function it_executes_remaining_after_commit_callbacks_when_a_nested_after }); $this->manager->afterCommit($this->record('outer:after')); }); - } catch (LogicException $e) { + } catch (AfterCommitCallbacksFailed $e) { $actual = $e; } - $this->assertSame($ex, $actual); + $this->assertSame($ex, $this->assertSoleAfterCommitFailure($actual)); $this->assertSame(['nested:result', 'outer:after', 'nested:after'], $this->log); $this->assertSame([ 'execute:1/attempt:1', @@ -770,14 +772,15 @@ public function it_cannot_start_a_unit_of_work_within_a_nested_unit_of_work(): v }); $this->manager->afterCommit($this->record('outer:after')); }); - } catch (ApplicationException $ex) { + } catch (AfterCommitCallbacksFailed $ex) { $actual = $ex; } - $this->assertInstanceOf(ApplicationException::class, $actual); + $previous = $this->assertSoleAfterCommitFailure($actual); + $this->assertInstanceOf(ApplicationException::class, $previous); $this->assertSame( 'Not expecting unit of work manager to start a unit of work within an existing one.', - $actual->getMessage(), + $previous->getMessage(), ); $this->assertSame(['outer:after'], $this->log); $this->assertSame([ @@ -786,7 +789,7 @@ public function it_cannot_start_a_unit_of_work_within_a_nested_unit_of_work(): v 'execute:2/attempt:1', 'execute:2/rollback:1', ], $this->unitOfWork->sequence); - $this->assertSame([$actual], $this->unitOfWork->exceptions->reported); + $this->assertSame([$previous], $this->unitOfWork->exceptions->reported); } #[Test] @@ -801,14 +804,15 @@ public function it_cannot_queue_a_before_commit_callback_from_an_after_commit_ca $this->manager->beforeCommit($this->record('before')); }); }); - } catch (ApplicationException $ex) { + } catch (AfterCommitCallbacksFailed $ex) { $actual = $ex; } - $this->assertInstanceOf(ApplicationException::class, $actual); + $previous = $this->assertSoleAfterCommitFailure($actual); + $this->assertInstanceOf(ApplicationException::class, $previous); $this->assertSame( 'Cannot queue a before commit callback when not executing a unit of work.', - $actual->getMessage(), + $previous->getMessage(), ); $this->assertSame([], $this->log); $this->assertSame([ @@ -862,7 +866,7 @@ public function it_is_reset_after_a_nested_unit_of_work_fails(): void $this->manager->execute(fn () => throw new LogicException('Boom!')); }); }); - } catch (LogicException) { + } catch (AfterCommitCallbacksFailed) { // expected } @@ -909,6 +913,23 @@ public function it_cannot_queue_a_before_commit_callback_once_the_unit_of_work_h $this->manager->beforeCommit($this->record('before')); } + /** + * Assert that the after commit callbacks failed with exactly one exception, and return that exception. + */ + private function assertSoleAfterCommitFailure(?AfterCommitCallbacksFailed $actual): Throwable + { + $this->assertInstanceOf(AfterCommitCallbacksFailed::class, $actual); + $this->assertInstanceOf(AfterCommitFailure::class, $actual); + $this->assertSame('1 after commit callback failed.', $actual->getMessage()); + + $exceptions = $actual->exceptions(); + + $this->assertCount(1, $exceptions); + $this->assertSame($exceptions[0], $actual->getPrevious()); + + return $exceptions[0]; + } + /** * Create a callback that records the provided entry in the log. * diff --git a/tests/Unit/Application/UnitOfWork/AfterCommitCallbacksFailedTest.php b/tests/Unit/Application/UnitOfWork/AfterCommitCallbacksFailedTest.php index 33eed4a4..d2835a92 100644 --- a/tests/Unit/Application/UnitOfWork/AfterCommitCallbacksFailedTest.php +++ b/tests/Unit/Application/UnitOfWork/AfterCommitCallbacksFailedTest.php @@ -14,6 +14,7 @@ use CloudCreativity\Modules\Application\UnitOfWork\AfterCommitCallbacksFailed; use CloudCreativity\Modules\Contracts\Application\ApplicationException; +use CloudCreativity\Modules\Contracts\Application\UnitOfWork\AfterCommitFailure; use LogicException; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; @@ -32,9 +33,23 @@ public function it_wraps_multiple_exceptions(): void $actual = new AfterCommitCallbacksFailed($exceptions); + $this->assertInstanceOf(AfterCommitFailure::class, $actual); $this->assertInstanceOf(ApplicationException::class, $actual); $this->assertSame('3 after commit callbacks failed.', $actual->getMessage()); $this->assertSame($exceptions, $actual->exceptions()); $this->assertSame($ex1, $actual->getPrevious()); } + + #[Test] + public function it_wraps_a_single_exception(): void + { + $ex = new LogicException('Boom!'); + + $actual = new AfterCommitCallbacksFailed([$ex]); + + $this->assertInstanceOf(AfterCommitFailure::class, $actual); + $this->assertSame('1 after commit callback failed.', $actual->getMessage()); + $this->assertSame([$ex], $actual->exceptions()); + $this->assertSame($ex, $actual->getPrevious()); + } } diff --git a/tests/Unit/Application/UnitOfWork/UnitOfWorkManagerTest.php b/tests/Unit/Application/UnitOfWork/UnitOfWorkManagerTest.php index 8f1f3177..42f9f1d2 100644 --- a/tests/Unit/Application/UnitOfWork/UnitOfWorkManagerTest.php +++ b/tests/Unit/Application/UnitOfWork/UnitOfWorkManagerTest.php @@ -17,6 +17,7 @@ use CloudCreativity\Modules\Application\UnitOfWork\AfterCommitCallbacksFailed; use CloudCreativity\Modules\Application\UnitOfWork\UnitOfWorkManager; use CloudCreativity\Modules\Contracts\Application\Ports\UnitOfWork; +use CloudCreativity\Modules\Contracts\Application\UnitOfWork\AfterCommitFailure; use CloudCreativity\Modules\Testing\FakeExceptionReporter; use LogicException; use PHPUnit\Framework\Attributes\Test; @@ -449,7 +450,8 @@ function () use (&$sequence): void { * * The unit of work has already been committed at this point, so the callback must not be * retried. The remaining after commit callbacks must still be executed, and the exception - * from the after commit callback must be thrown as-is. + * from the after commit callback must be wrapped, so that calling code knows the unit of + * work committed. */ #[Test] public function it_does_not_retry_when_after_commit_callback_fails(): void @@ -479,11 +481,11 @@ public function it_does_not_retry_when_after_commit_callback_fails(): void $sequence[] = 'after2'; }); }, 3); - } catch (LogicException $e) { + } catch (AfterCommitCallbacksFailed $e) { $actual = $e; } - $this->assertSame($ex, $actual); + $this->assertSame($ex, $this->assertSoleAfterCommitFailure($actual)); $this->assertSame(['begin', 'callback', 'committed', 'after1', 'after2'], $sequence); $this->assertSame([], $this->reporter->reported); } @@ -569,11 +571,11 @@ public function it_does_not_retry_after_commit_following_a_failed_attempt(): voi throw $first; } }, 3); - } catch (LogicException $e) { + } catch (AfterCommitCallbacksFailed $e) { $actual = $e; } - $this->assertSame($ex, $actual); + $this->assertSame($ex, $this->assertSoleAfterCommitFailure($actual)); $this->assertSame(['begin', 'rollback', 'begin', 'committed', 'after1'], $sequence); $this->assertSame([$first], $this->reporter->reported); } @@ -590,7 +592,7 @@ public function it_flushes_callbacks_when_after_commit_callback_fails(): void $this->manager->execute(function () { $this->manager->afterCommit(fn () => throw new LogicException('Boom!')); }, 2); - } catch (LogicException) { + } catch (AfterCommitCallbacksFailed) { // expected } @@ -647,19 +649,29 @@ public function it_cannot_start_transaction_within_an_existing_one(): void #[Test] public function it_fails_if_after_commit_callback_registers_before_commit_callback(): void { + $actual = null; + $this->unitOfWork ->expects($this->once()) ->method('execute') ->willReturnCallback(fn (Closure $callback) => $callback()); - $this->expectException(ApplicationException::class); - $this->expectExceptionMessage('Cannot queue a before commit callback when not executing a unit of work.'); - - $this->manager->execute(function () { - $this->manager->afterCommit(function () { - $this->manager->beforeCommit(fn () => null); + try { + $this->manager->execute(function () { + $this->manager->afterCommit(function () { + $this->manager->beforeCommit(fn () => null); + }); }); - }); + } catch (AfterCommitCallbacksFailed $ex) { + $actual = $ex; + } + + $previous = $this->assertSoleAfterCommitFailure($actual); + $this->assertInstanceOf(ApplicationException::class, $previous); + $this->assertSame( + 'Cannot queue a before commit callback when not executing a unit of work.', + $previous->getMessage(), + ); } #[Test] @@ -765,11 +777,11 @@ public function it_executes_remaining_callbacks_when_a_queued_callback_fails(): }); }); }, 3); - } catch (LogicException $e) { + } catch (AfterCommitCallbacksFailed $e) { $actual = $e; } - $this->assertSame($ex, $actual); + $this->assertSame($ex, $this->assertSoleAfterCommitFailure($actual)); $this->assertSame(['begin', 'commit', 'after1', 'after2', 'after3', 'after4'], $sequence); $this->assertSame([], $this->reporter->reported); } @@ -1082,11 +1094,11 @@ public function it_executes_remaining_after_commit_callbacks_when_a_nested_unit_ $sequence[] = 'after2'; }); }); - } catch (LogicException $e) { + } catch (AfterCommitCallbacksFailed $e) { $actual = $e; } - $this->assertSame($ex, $actual); + $this->assertSame($ex, $this->assertSoleAfterCommitFailure($actual)); $this->assertSame(['begin', 'commit', 'after1', 'begin', 'nested', 'rollback', 'after2'], $sequence); $this->assertSame([], $this->reporter->reported); } @@ -1122,11 +1134,11 @@ public function it_executes_remaining_after_commit_callbacks_when_a_nested_after $sequence[] = 'outer:after'; }); }); - } catch (LogicException $e) { + } catch (AfterCommitCallbacksFailed $e) { $actual = $e; } - $this->assertSame($ex, $actual); + $this->assertSame($ex, $this->assertSoleAfterCommitFailure($actual)); $this->assertSame([ 'begin', 'commit', @@ -1145,37 +1157,54 @@ public function it_executes_remaining_after_commit_callbacks_when_a_nested_after public function it_cannot_start_a_unit_of_work_within_a_nested_unit_of_work(): void { $sequence = []; + $actual = null; $this->willExecuteUnitOfWork(2, $sequence); - $this->expectException(ApplicationException::class); - $this->expectExceptionMessage( - 'Not expecting unit of work manager to start a unit of work within an existing one.', - ); - - $this->manager->execute(function (): void { - $this->manager->afterCommit(function (): void { - $this->manager->execute(function (): void { - $this->manager->execute(fn () => null); + try { + $this->manager->execute(function (): void { + $this->manager->afterCommit(function (): void { + $this->manager->execute(function (): void { + $this->manager->execute(fn () => null); + }); }); }); - }); + } catch (AfterCommitCallbacksFailed $ex) { + $actual = $ex; + } + + $previous = $this->assertSoleAfterCommitFailure($actual); + $this->assertInstanceOf(ApplicationException::class, $previous); + $this->assertSame( + 'Not expecting unit of work manager to start a unit of work within an existing one.', + $previous->getMessage(), + ); + $this->assertSame(['begin', 'commit', 'begin', 'rollback'], $sequence); } #[Test] public function it_fails_if_after_commit_callback_registers_before_commit_callback_after_a_nested_unit_of_work(): void { $sequence = []; + $actual = null; $this->willExecuteUnitOfWork(2, $sequence); - $this->expectException(ApplicationException::class); - $this->expectExceptionMessage('Cannot queue a before commit callback when not executing a unit of work.'); - - $this->manager->execute(function (): void { - $this->manager->afterCommit(function (): void { - $this->manager->execute(fn () => null); - $this->manager->beforeCommit(fn () => null); + try { + $this->manager->execute(function (): void { + $this->manager->afterCommit(function (): void { + $this->manager->execute(fn () => null); + $this->manager->beforeCommit(fn () => null); + }); }); - }); + } catch (AfterCommitCallbacksFailed $ex) { + $actual = $ex; + } + + $previous = $this->assertSoleAfterCommitFailure($actual); + $this->assertInstanceOf(ApplicationException::class, $previous); + $this->assertSame( + 'Cannot queue a before commit callback when not executing a unit of work.', + $previous->getMessage(), + ); } // ------------------------------------------------------------------------- @@ -1248,7 +1277,7 @@ public function it_cannot_queue_after_commit_callback_once_after_commit_callback $this->manager->execute(function (): void { $this->manager->afterCommit(fn () => throw new LogicException('Boom!')); }); - } catch (LogicException) { + } catch (AfterCommitCallbacksFailed) { // expected } @@ -1274,6 +1303,23 @@ public function it_cannot_queue_before_commit_callback_once_the_unit_of_work_has $this->manager->beforeCommit(fn () => null); } + /** + * Assert that the after commit callbacks failed with exactly one exception, and return that exception. + */ + private function assertSoleAfterCommitFailure(?AfterCommitCallbacksFailed $actual): Throwable + { + $this->assertInstanceOf(AfterCommitCallbacksFailed::class, $actual); + $this->assertInstanceOf(AfterCommitFailure::class, $actual); + $this->assertSame('1 after commit callback failed.', $actual->getMessage()); + + $exceptions = $actual->exceptions(); + + $this->assertCount(1, $exceptions); + $this->assertSame($exceptions[0], $actual->getPrevious()); + + return $exceptions[0]; + } + /** * Expect the unit of work to be executed the given number of times, recording each execution in the sequence. *