Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 23 additions & 4 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>`, `non-empty-string`,
`Result<T>`, 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).
Expand Down
32 changes: 31 additions & 1 deletion docs/guide/application/units-of-work.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
);
);
}
}
```
Expand Down Expand Up @@ -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.
28 changes: 28 additions & 0 deletions docs/guide/upgrade.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
15 changes: 15 additions & 0 deletions src/Contracts/Application/Ports/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Testing/FakeUnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -57,6 +74,8 @@ public function execute(Closure $callback, int $attempts = 1): mixed
if ($i === $attempts) {
throw $ex;
}
} finally {
$this->active = false;
}
}

Expand Down
84 changes: 84 additions & 0 deletions tests/Unit/Testing/FakeUnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Loading