From da3b6b983b18befefe01f2612414362d405ac015 Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 14 Nov 2026 14:14:13 +0000 Subject: [PATCH 1/2] feat!: add active and inactive methods to unit of work port Adds active() and inactive() to the UnitOfWork driven port, so callers can determine whether a unit of work is currently in progress. The FakeUnitOfWork implements both and now throws if execute() is called while it is already active. BREAKING CHANGE: implementations of the UnitOfWork interface must add the active() and inactive() methods. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 ++ docs/guide/application/units-of-work.md | 32 ++++++- docs/guide/upgrade.md | 28 +++++++ .../Application/Ports/UnitOfWork.php | 15 ++++ src/Testing/FakeUnitOfWork.php | 19 +++++ tests/Unit/Testing/FakeUnitOfWorkTest.php | 84 +++++++++++++++++++ 6 files changed, 185 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a01614cc..a05a9e5d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/). +## Unreleased Next Major + +### Added + +- **BREAKING** The `UnitOfWork` port now has `active()` and `inactive()` methods, to determine whether a unit of work + is currently in progress. Implementations of this interface must add these methods. The `FakeUnitOfWork` implements + them, and now throws if `execute()` is called while it is already active. + ## Unreleased ## [6.1.1] - 2026-11-14 diff --git a/docs/guide/application/units-of-work.md b/docs/guide/application/units-of-work.md index b0a98f0b..65ecbe68 100644 --- a/docs/guide/application/units-of-work.md +++ b/docs/guide/application/units-of-work.md @@ -168,6 +168,21 @@ use Closure; interface UnitOfWork { + /** + * Is a unit of work currently active? + * + * Returns true while a callback passed to `execute()` is running, i.e. there is an open transaction that will be + * committed or rolled back once the callback completes. + */ + public function active(): bool; + + /** + * Is there no unit of work currently active? + * + * Returns the inverse of `active()`. + */ + public function inactive(): bool; + /** * Execute the callback in a transaction. * @@ -196,12 +211,22 @@ final readonly class IlluminateUnitOfWork implements UnitOfWork { } + public function active(): bool + { + return $this->db->transactionLevel() > 0; + } + + public function inactive(): bool + { + return !$this->active(); + } + public function execute(Closure $callback, int $attempts = 1): mixed { return $this->db->transaction( static fn () => $callback(), $attempts, - ); + ); } } ``` @@ -479,3 +504,8 @@ $this->assertSame( $unitOfWork->sequence, ); ``` + +The fake also implements the `active()` and `inactive()` methods. It is active while the callback passed to +`execute()` is running, including on each retry, and inactive before and after. To match the unit of work manager, +which does not support nested units of work, the fake throws a `RuntimeException` if `execute()` is called while it is +already active. diff --git a/docs/guide/upgrade.md b/docs/guide/upgrade.md index 4a931d55..e20df9b8 100644 --- a/docs/guide/upgrade.md +++ b/docs/guide/upgrade.md @@ -1,5 +1,33 @@ # Upgrade Guide +## 6.x to 7.x + +Upgrade using Composer: + +```bash +composer require cloudcreativity/ddd-modules:^7.0 +``` + +### Unit of Work + +The `UnitOfWork` driven port now has `active()` and `inactive()` methods. These indicate whether a unit of work is +currently in progress - i.e. whether the callback passed to `execute()` is running. You will need to add these methods +to your unit of work adapter. For example, a Laravel adapter could implement them as follows: + +```php +public function active(): bool +{ + return $this->db->transactionLevel() > 0; +} + +public function inactive(): bool +{ + return !$this->active(); +} +``` + +If you use the `FakeUnitOfWork` in your tests, it now throws if `execute()` is called while it is already active. + ## 5.x to 6.x Upgrade using Composer: diff --git a/src/Contracts/Application/Ports/UnitOfWork.php b/src/Contracts/Application/Ports/UnitOfWork.php index df30afe4..efd804d7 100644 --- a/src/Contracts/Application/Ports/UnitOfWork.php +++ b/src/Contracts/Application/Ports/UnitOfWork.php @@ -16,6 +16,21 @@ interface UnitOfWork { + /** + * Is a unit of work currently active? + * + * Returns true while a callback passed to `execute()` is running, i.e. there is an open transaction that will be + * committed or rolled back once the callback completes. + */ + public function active(): bool; + + /** + * Is there no unit of work currently active? + * + * Returns the inverse of `active()`. + */ + public function inactive(): bool; + /** * Execute the callback in a transaction. * diff --git a/src/Testing/FakeUnitOfWork.php b/src/Testing/FakeUnitOfWork.php index b4ac9a4e..d14af7ed 100644 --- a/src/Testing/FakeUnitOfWork.php +++ b/src/Testing/FakeUnitOfWork.php @@ -31,17 +31,34 @@ final class FakeUnitOfWork implements UnitOfWork */ public array $sequence = []; + private bool $active = false; + public function __construct(public FakeExceptionReporter $exceptions = new FakeExceptionReporter()) { } + public function active(): bool + { + return $this->active; + } + + public function inactive(): bool + { + return !$this->active; + } + public function execute(Closure $callback, int $attempts = 1): mixed { if ($attempts < 1) { throw new InvalidArgumentException('The number of attempts must be at least 1.'); } + if ($this->active) { + throw new RuntimeException('Fake unit of work is already active.'); + } + for ($i = 1; $i <= $attempts; $i++) { + $this->active = true; try { $this->sequence[] = 'attempt:' . $i; $this->attempts++; @@ -57,6 +74,8 @@ public function execute(Closure $callback, int $attempts = 1): mixed if ($i === $attempts) { throw $ex; } + } finally { + $this->active = false; } } diff --git a/tests/Unit/Testing/FakeUnitOfWorkTest.php b/tests/Unit/Testing/FakeUnitOfWorkTest.php index f6f1636e..ef3fc5de 100644 --- a/tests/Unit/Testing/FakeUnitOfWorkTest.php +++ b/tests/Unit/Testing/FakeUnitOfWorkTest.php @@ -43,10 +43,94 @@ public function it_exposes_the_provided_exception_reporter(): void $this->assertSame($reporter, $unitOfWork->exceptions); } + // ------------------------------------------------------------------------- + // active() and inactive() + // ------------------------------------------------------------------------- + + #[Test] + public function it_is_inactive_by_default(): void + { + $unitOfWork = new FakeUnitOfWork(); + + $active = $unitOfWork->active(); + $inactive = $unitOfWork->inactive(); + + $this->assertFalse($active); + $this->assertTrue($inactive); + } + + #[Test] + public function it_is_active_while_executing_the_callback(): void + { + $unitOfWork = new FakeUnitOfWork(); + + $flags = $unitOfWork->execute(fn () => [$unitOfWork->active(), $unitOfWork->inactive()]); + + $this->assertSame([true, false], $flags); + } + + #[Test] + public function it_is_inactive_after_committing(): void + { + $unitOfWork = new FakeUnitOfWork(); + + $unitOfWork->execute(fn () => 'result'); + + $this->assertFalse($unitOfWork->active()); + $this->assertTrue($unitOfWork->inactive()); + } + + #[Test] + public function it_is_inactive_after_rolling_back(): void + { + $unitOfWork = new FakeUnitOfWork(); + + try { + $unitOfWork->execute(fn () => throw new RuntimeException('An error occurred.')); + } catch (RuntimeException) { + // expected + } + + $this->assertFalse($unitOfWork->active()); + $this->assertTrue($unitOfWork->inactive()); + } + + #[Test] + public function it_is_active_on_every_attempt_when_retrying(): void + { + $unitOfWork = new FakeUnitOfWork(); + $flags = []; + $exceptions = [new RuntimeException('An error occurred.')]; + + $unitOfWork->execute(function () use ($unitOfWork, &$flags, &$exceptions): string { + $flags[] = $unitOfWork->active(); + + if ($ex = array_shift($exceptions)) { + throw $ex; + } + + return 'result'; + }, 2); + + $this->assertSame([true, true], $flags); + $this->assertFalse($unitOfWork->active()); + } + // ------------------------------------------------------------------------- // execute() // ------------------------------------------------------------------------- + #[Test] + public function it_throws_when_executing_within_an_active_unit_of_work(): void + { + $unitOfWork = new FakeUnitOfWork(); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Fake unit of work is already active.'); + + $unitOfWork->execute(fn () => $unitOfWork->execute(fn () => 'result')); + } + #[Test] public function it_is_successful_on_first_attempt(): void { From cd9bdf08799bd6c6cea4801551cc12860b69fc4b Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 14 Nov 2026 14:14:13 +0000 Subject: [PATCH 2/2] docs: add breaking change and changelog guidance to claude.md Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index e556ca51..1d35e3cf 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -69,14 +69,33 @@ then the handler's own middleware, then the handler. Commands and queries return - PHPStan runs at level 10 including tests — use precise generics/phpdoc types (`class-string`, `non-empty-string`, `Result`, etc.). - Tests mirror `src/` under `tests/Unit` and `tests/Integration`; file names end in `Test.php`. -- Record user-facing changes under `## Unreleased` in `CHANGELOG.md` (keepachangelog format; mark breaking changes - with **BREAKING**). Deprecations are kept for BC and removed in the next major — update `docs/guide/upgrade.md` for - breaking changes and the relevant `docs/guide/**` page for new features. +- Record user-facing changes in `CHANGELOG.md` (keepachangelog format, grouped under `### Added`, `### Changed`, + `### Fixed`, `### Deprecated`, `### Removed`). Which heading depends on the branch — see Breaking Changes below. + Update the relevant `docs/guide/**` page for new features. +- Deprecations are kept for BC and removed in the next major. + +## Breaking Changes + +A breaking change is anything that can break a consumer's code on upgrade. In this library that includes: adding a +method to an interface (consumers implement the `Contracts` interfaces, e.g. the driven ports), changing a public or +protected method signature, removing or renaming a class/method/constant, changing the constructor of a class +consumers instantiate or extend, and changing documented behaviour. + +- Breaking changes **must** target the `next` branch, never `develop`: branch from `next`, rebase onto `next`, and open + the PR against `next`. If you are on `develop` and the change is + breaking, stop and tell the user rather than making it there. Where possible, offer a non-breaking alternative for + `develop` (e.g. a new interface, or deprecating the old API and keeping it working). +- Use `!` in the commit type (e.g. `feat!: ...`). +- On `next`, record changes under `## Unreleased Next Major` in `CHANGELOG.md`, which sits **above** `## Unreleased`. + Create that heading if it does not exist. Prefix each breaking entry with **BREAKING**. Leave `## Unreleased` alone — + it holds changes on `develop` that are merged into `next` and released in the next 6.x version. +- On `develop`, record changes under `## Unreleased` only. `develop` never has an `Unreleased Next Major` section. +- Document how to upgrade in `docs/guide/upgrade.md`, under the section for the next major (e.g. `## 6.x to 7.x`). ## Git Workflow This repo uses git-flow. `develop` is the 6.x line and the default target for branches and PRs. `next` is the 7.x line -(breaking changes go here). `main` holds tagged releases, and the docs deploy from `main`. Never branch from or target +(breaking changes go here — see Breaking Changes above). `main` holds tagged releases, and the docs deploy from `main`. Never branch from or target `main` directly: releases are cut by maintainers through `release/x.y.z` branches. Branch prefixes: `feature/`, `fix/`, `refactor/`, `build/`, `wip/` (never merged).