From 0e16dee7a68f574a0f843a8f8bd50e63485d62b4 Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 14 Nov 2026 13:45:31 +0000 Subject: [PATCH 1/9] fix: throw when binding a string enqueuer without a PSR container Refs #40 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 +++++ src/Infrastructure/Queue/EnqueuerContainer.php | 6 ++++++ .../Infrastructure/Queue/EnqueuerContainerTest.php | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff8e1f7a..7ff888ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. This projec ## Unreleased +### Fixed + +- `EnqueuerContainer::bind()` now throws an `InfrastructureException` when given a string binding without a PSR + container, instead of failing later when the enqueuer is resolved. + ## [6.1.0] - 2026-11-14 ### Added diff --git a/src/Infrastructure/Queue/EnqueuerContainer.php b/src/Infrastructure/Queue/EnqueuerContainer.php index c27901b8..45624d7a 100644 --- a/src/Infrastructure/Queue/EnqueuerContainer.php +++ b/src/Infrastructure/Queue/EnqueuerContainer.php @@ -47,6 +47,12 @@ public function __construct( */ public function bind(array|string $queueableName, Closure|string $binding): void { + if (is_string($binding) && $this->container === null) { + throw new InfrastructureException( + 'Cannot bind enqueuer as a string without a PSR container.', + ); + } + if (is_string($queueableName)) { $queueableName = [$queueableName]; } diff --git a/tests/Unit/Infrastructure/Queue/EnqueuerContainerTest.php b/tests/Unit/Infrastructure/Queue/EnqueuerContainerTest.php index 8eaaca10..54cbcbca 100644 --- a/tests/Unit/Infrastructure/Queue/EnqueuerContainerTest.php +++ b/tests/Unit/Infrastructure/Queue/EnqueuerContainerTest.php @@ -222,4 +222,15 @@ public function it_throws_when_setting_a_string_default_enqueuer_without_a_psr_c $container->withDefault(TestEnqueuer::class); } + + #[Test] + public function it_throws_when_binding_a_string_enqueuer_without_a_psr_container(): void + { + $container = new EnqueuerContainer(); + + $this->expectException(InfrastructureException::class); + $this->expectExceptionMessage('Cannot bind enqueuer as a string without a PSR container.'); + + $container->bind(TestQueueableCommand::class, TestEnqueuer::class); + } } From 891c4b475fbee6c98f2fe52e16424e2220f2abce Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 14 Nov 2026 13:45:39 +0000 Subject: [PATCH 2/9] fix: sort keyed set of errors consistently when putting and merging Refs #40 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + src/Toolkit/Result/KeyedSetOfErrors.php | 11 +++++++--- .../Toolkit/Result/KeyedSetOfErrorsTest.php | 20 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ff888ac..d91650fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file. This projec - `EnqueuerContainer::bind()` now throws an `InfrastructureException` when given a string binding without a PSR container, instead of failing later when the enqueuer is resolved. +- `KeyedSetOfErrors::put()` and `merge()` now sort keys case-insensitively, matching the constructor. ## [6.1.0] - 2026-11-14 diff --git a/src/Toolkit/Result/KeyedSetOfErrors.php b/src/Toolkit/Result/KeyedSetOfErrors.php index 29d40b9e..1c08ad91 100644 --- a/src/Toolkit/Result/KeyedSetOfErrors.php +++ b/src/Toolkit/Result/KeyedSetOfErrors.php @@ -49,7 +49,7 @@ public function __construct(IError ...$errors) $this->stack[$key] = $this->get($key)->push($error); } - ksort($this->stack, SORT_STRING | SORT_FLAG_CASE); + $this->sort(); } /** @@ -63,7 +63,7 @@ public function put(IError $error): self $copy = clone $this; $copy->stack[$key] = $errors->push($error); - ksort($copy->stack); + $copy->sort(); return $copy; } @@ -76,7 +76,7 @@ public function merge(IListOfErrors|self $other): self $copy->stack[$key] = $copy->get($key)->merge($errors); } - ksort($copy->stack); + $copy->sort(); return $copy; } @@ -126,6 +126,11 @@ public function count(): int return $count; } + private function sort(): void + { + ksort($this->stack, SORT_STRING | SORT_FLAG_CASE); + } + private function keyFor(IError $error): string { $key = $error->key(); diff --git a/tests/Unit/Toolkit/Result/KeyedSetOfErrorsTest.php b/tests/Unit/Toolkit/Result/KeyedSetOfErrorsTest.php index cb20302c..5914e2a1 100644 --- a/tests/Unit/Toolkit/Result/KeyedSetOfErrorsTest.php +++ b/tests/Unit/Toolkit/Result/KeyedSetOfErrorsTest.php @@ -256,6 +256,26 @@ public function it_sorts_keys_case_insensitively(): void $this->assertSame(['_base', 'Bar', 'baz', 'foo'], $actual); } + #[Test] + public function it_sorts_keys_case_insensitively_when_putting_an_error(): void + { + $errors = new KeyedSetOfErrors(new Error(message: 'Message A', key: 'bar')); + + $actual = $errors->put(new Error(message: 'Message B', key: 'Foo'))->keys(); + + $this->assertSame(['bar', 'Foo'], $actual); + } + + #[Test] + public function it_sorts_keys_case_insensitively_when_merging(): void + { + $errors = new KeyedSetOfErrors(new Error(message: 'Message A', key: 'bar')); + + $actual = $errors->merge(new KeyedSetOfErrors(new Error(message: 'Message B', key: 'Foo')))->keys(); + + $this->assertSame(['bar', 'Foo'], $actual); + } + #[Test] public function it_returns_an_empty_list_for_a_key_that_does_not_exist(): void { From 5b34d354ae04371d93a3e1422e88c7b4ea2e47ab Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 14 Nov 2026 13:46:13 +0000 Subject: [PATCH 3/9] fix: preserve numeric string keys when merging result meta Refs #40 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + src/Toolkit/Result/Meta.php | 2 +- tests/Unit/Toolkit/Result/MetaTest.php | 11 +++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d91650fb..0d4828e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. This projec - `EnqueuerContainer::bind()` now throws an `InfrastructureException` when given a string binding without a PSR container, instead of failing later when the enqueuer is resolved. - `KeyedSetOfErrors::put()` and `merge()` now sort keys case-insensitively, matching the constructor. +- `Meta::merge()` no longer renumbers numeric string keys. ## [6.1.0] - 2026-11-14 diff --git a/src/Toolkit/Result/Meta.php b/src/Toolkit/Result/Meta.php index f4c3380f..a3043ef2 100644 --- a/src/Toolkit/Result/Meta.php +++ b/src/Toolkit/Result/Meta.php @@ -110,7 +110,7 @@ public function merge(array|self $values): self } $copy = clone $this; - $copy->stack = array_merge($this->stack, $values); + $copy->stack = array_replace($this->stack, $values); return $copy; } diff --git a/tests/Unit/Toolkit/Result/MetaTest.php b/tests/Unit/Toolkit/Result/MetaTest.php index 35973a14..0d879400 100644 --- a/tests/Unit/Toolkit/Result/MetaTest.php +++ b/tests/Unit/Toolkit/Result/MetaTest.php @@ -185,6 +185,17 @@ public function it_merges_an_array(): void $this->assertSame(['foo' => null, 'baz' => 'bat', 'foobar' => 'bazbat'], $actual->all()); } + #[Test] + public function it_preserves_numeric_string_keys_when_merging(): void + { + $original = new Meta(['x' => 'y'])->put('1', 'a'); + $values = new Meta(['x' => 'z'])->put('2', 'b'); + + $actual = $original->merge($values); + + $this->assertSame(['x' => 'z', 1 => 'a', 2 => 'b'], $actual->all()); + } + #[Test] public function it_merges_another_meta(): void { From e850ba4f122e81ced59cd124d2a0bf81f17676f9 Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 14 Nov 2026 13:46:23 +0000 Subject: [PATCH 4/9] fix: allow an error message and key of '0' Refs #40 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + src/Toolkit/Result/Error.php | 4 ++-- tests/Unit/Toolkit/Result/ErrorTest.php | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d4828e5..a54fb57f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. This projec container, instead of failing later when the enqueuer is resolved. - `KeyedSetOfErrors::put()` and `merge()` now sort keys case-insensitively, matching the constructor. - `Meta::merge()` no longer renumbers numeric string keys. +- `Error` now accepts a message of `'0'` and no longer converts a key of `'0'` to `null`. ## [6.1.0] - 2026-11-14 diff --git a/src/Toolkit/Result/Error.php b/src/Toolkit/Result/Error.php index 0512dee5..d332f2ba 100644 --- a/src/Toolkit/Result/Error.php +++ b/src/Toolkit/Result/Error.php @@ -25,8 +25,8 @@ public function __construct( private string $message = '', string|UnitEnum|null $key = null, ) { - Contracts::assert(!empty($message) || $code !== null, 'Error must have a message or a code.'); - $this->key = $key ?: null; + Contracts::assert($message !== '' || $code !== null, 'Error must have a message or a code.'); + $this->key = $key === '' ? null : $key; } public function key(): string|UnitEnum|null diff --git a/tests/Unit/Toolkit/Result/ErrorTest.php b/tests/Unit/Toolkit/Result/ErrorTest.php index f371961c..cfc616e0 100644 --- a/tests/Unit/Toolkit/Result/ErrorTest.php +++ b/tests/Unit/Toolkit/Result/ErrorTest.php @@ -98,6 +98,22 @@ public function it_converts_an_empty_key_to_null(): void $this->assertNull($error->key()); } + #[Test] + public function it_can_have_a_message_of_zero(): void + { + $error = new Error(message: '0'); + + $this->assertSame('0', $error->message()); + } + + #[Test] + public function it_can_have_a_key_of_zero(): void + { + $error = new Error(message: 'Hello World', key: '0'); + + $this->assertSame('0', $error->key()); + } + #[Test] public function it_is_not_a_code_when_it_has_no_code(): void { From 2752d893fd9b21869815eb10fea04ffb1e1b8dfe Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 14 Nov 2026 13:46:29 +0000 Subject: [PATCH 5/9] fix: add message to uuid version contract exceptions Refs #40 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + src/Toolkit/Identifiers/UuidV4.php | 2 +- src/Toolkit/Identifiers/UuidV7.php | 2 +- tests/Unit/Toolkit/Identifiers/UuidV4Test.php | 1 + tests/Unit/Toolkit/Identifiers/UuidV7Test.php | 1 + 5 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a54fb57f..a0de34e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. This projec - `KeyedSetOfErrors::put()` and `merge()` now sort keys case-insensitively, matching the constructor. - `Meta::merge()` no longer renumbers numeric string keys. - `Error` now accepts a message of `'0'` and no longer converts a key of `'0'` to `null`. +- The `ContractException` thrown by `UuidV4` and `UuidV7` when given a lazy UUID of the wrong version now has a message. ## [6.1.0] - 2026-11-14 diff --git a/src/Toolkit/Identifiers/UuidV4.php b/src/Toolkit/Identifiers/UuidV4.php index 28398c32..149d3f48 100644 --- a/src/Toolkit/Identifiers/UuidV4.php +++ b/src/Toolkit/Identifiers/UuidV4.php @@ -56,7 +56,7 @@ public static function tryFrom(IBaseUuid|Identifier|string|null $value): ?self public function __construct(public readonly BaseUuidV4|LazyUuidFromString $value) { if ($this->value instanceof LazyUuidFromString) { - Contracts::assert($this->value->getVersion() === 4); + Contracts::assert($this->value->getVersion() === 4, 'Expecting a version 4 UUID.'); } } } diff --git a/src/Toolkit/Identifiers/UuidV7.php b/src/Toolkit/Identifiers/UuidV7.php index ce03fada..eecae3d6 100644 --- a/src/Toolkit/Identifiers/UuidV7.php +++ b/src/Toolkit/Identifiers/UuidV7.php @@ -57,7 +57,7 @@ public static function tryFrom(IBaseUuid|Identifier|string|null $value): ?self public function __construct(public readonly BaseUuidV7|LazyUuidFromString $value) { if ($this->value instanceof LazyUuidFromString) { - Contracts::assert($this->value->getVersion() === 7); + Contracts::assert($this->value->getVersion() === 7, 'Expecting a version 7 UUID.'); } } } diff --git a/tests/Unit/Toolkit/Identifiers/UuidV4Test.php b/tests/Unit/Toolkit/Identifiers/UuidV4Test.php index 30b0fe02..d7055dae 100644 --- a/tests/Unit/Toolkit/Identifiers/UuidV4Test.php +++ b/tests/Unit/Toolkit/Identifiers/UuidV4Test.php @@ -67,6 +67,7 @@ public function it_rejects_a_lazy_uuid_of_another_version(): void $base = new LazyUuidFromString(RamseyUuid::uuid7()->toString()); $this->expectException(ContractException::class); + $this->expectExceptionMessage('Expecting a version 4 UUID.'); new UuidV4($base); } diff --git a/tests/Unit/Toolkit/Identifiers/UuidV7Test.php b/tests/Unit/Toolkit/Identifiers/UuidV7Test.php index 14fc69dc..9419be93 100644 --- a/tests/Unit/Toolkit/Identifiers/UuidV7Test.php +++ b/tests/Unit/Toolkit/Identifiers/UuidV7Test.php @@ -68,6 +68,7 @@ public function it_rejects_a_lazy_uuid_of_another_version(): void $base = new LazyUuidFromString(RamseyUuid::uuid4()->toString()); $this->expectException(ContractException::class); + $this->expectExceptionMessage('Expecting a version 7 UUID.'); new UuidV7($base); } From 7619bca237c722dc382e55fc269e58d57a767633 Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 14 Nov 2026 13:46:41 +0000 Subject: [PATCH 6/9] fix: rethrow in fake exception reporter once expected count is exceeded Refs #40 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 ++ src/Testing/FakeExceptionReporter.php | 2 +- tests/Unit/Testing/FakeExceptionReporterTest.php | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0de34e3..78e58969 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ All notable changes to this project will be documented in this file. This projec - `Meta::merge()` no longer renumbers numeric string keys. - `Error` now accepts a message of `'0'` and no longer converts a key of `'0'` to `null`. - The `ContractException` thrown by `UuidV4` and `UuidV7` when given a lazy UUID of the wrong version now has a message. +- `FakeExceptionReporter` now rethrows when more exceptions have already been reported than expected, e.g. when + `none()` is called after an exception was reported. ## [6.1.0] - 2026-11-14 diff --git a/src/Testing/FakeExceptionReporter.php b/src/Testing/FakeExceptionReporter.php index c04c82f5..fa2bb504 100644 --- a/src/Testing/FakeExceptionReporter.php +++ b/src/Testing/FakeExceptionReporter.php @@ -35,7 +35,7 @@ final class FakeExceptionReporter implements ExceptionReporter, Countable, Array public function report(Throwable $ex): void { - if ($this->expected === count($this->reported)) { + if ($this->expected !== null && count($this->reported) >= $this->expected) { throw $ex; } diff --git a/tests/Unit/Testing/FakeExceptionReporterTest.php b/tests/Unit/Testing/FakeExceptionReporterTest.php index 5d3f2cd6..76e9568a 100644 --- a/tests/Unit/Testing/FakeExceptionReporterTest.php +++ b/tests/Unit/Testing/FakeExceptionReporterTest.php @@ -109,6 +109,21 @@ public function it_rethrows_once_the_expected_count_is_reached(): void $this->reporter->report($expected); } + #[Test] + public function it_rethrows_when_the_expected_count_has_been_exceeded(): void + { + $expected = new RuntimeException('Boom! That was a surprise.'); + $this->reporter->reported = [ + new LogicException('Whoops 0!'), + new LogicException('Whoops 1!'), + ]; + $this->reporter->expect(1); + + $this->expectExceptionObject($expected); + + $this->reporter->report($expected); + } + // ------------------------------------------------------------------------- // count() and getIterator() // ------------------------------------------------------------------------- From db6410b81222a1ef954c5f80a55ca614afeee948 Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 14 Nov 2026 13:46:51 +0000 Subject: [PATCH 7/9] fix: keep a message of '0' in fake logger messages Refs #40 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + src/Testing/FakeLogger.php | 2 +- tests/Unit/Testing/FakeLoggerTest.php | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78e58969..752ca207 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file. This projec - The `ContractException` thrown by `UuidV4` and `UuidV7` when given a lazy UUID of the wrong version now has a message. - `FakeExceptionReporter` now rethrows when more exceptions have already been reported than expected, e.g. when `none()` is called after an exception was reported. +- `FakeLogger::messages()` no longer drops a message of `'0'`. ## [6.1.0] - 2026-11-14 diff --git a/src/Testing/FakeLogger.php b/src/Testing/FakeLogger.php index f46615b8..087e6861 100644 --- a/src/Testing/FakeLogger.php +++ b/src/Testing/FakeLogger.php @@ -41,7 +41,7 @@ public function messages(): array fn (array $entry): string => implode(': ', array_filter([ is_scalar($entry['level']) ? strtoupper((string) $entry['level']) : null, $entry['message'], - ])), + ], static fn (?string $value): bool => $value !== null)), $this->log, ); } diff --git a/tests/Unit/Testing/FakeLoggerTest.php b/tests/Unit/Testing/FakeLoggerTest.php index d059e6ce..486ee247 100644 --- a/tests/Unit/Testing/FakeLoggerTest.php +++ b/tests/Unit/Testing/FakeLoggerTest.php @@ -124,4 +124,16 @@ public function it_returns_messages_without_a_prefix_when_the_level_is_not_scala $this->assertSame(['Some message'], $actual); } + + #[Test] + public function it_returns_a_message_of_zero(): void + { + $this->logger->log = [ + ['level' => LogLevel::INFO, 'message' => '0', 'context' => []], + ]; + + $actual = $this->logger->messages(); + + $this->assertSame(['INFO: 0'], $actual); + } } From a54d2c270494a168bf54ee47928fc1991babd3df Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 14 Nov 2026 13:47:08 +0000 Subject: [PATCH 8/9] fix: implement queue middleware interface in log pushed to queue middleware Refs #40 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 ++ .../Queue/Middleware/LogPushedToQueue.php | 8 +++---- .../Queue/Middleware/LogPushedToQueueTest.php | 22 +++++++++++++------ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 752ca207..6619b086 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ All notable changes to this project will be documented in this file. This projec - `FakeExceptionReporter` now rethrows when more exceptions have already been reported than expected, e.g. when `none()` is called after an exception was reported. - `FakeLogger::messages()` no longer drops a message of `'0'`. +- `LogPushedToQueue` now implements the `QueueMiddleware` interface, instead of `CommandMiddleware`. Its `__invoke()` + method now has a `void` return type, matching the queue middleware contract. ## [6.1.0] - 2026-11-14 diff --git a/src/Infrastructure/Queue/Middleware/LogPushedToQueue.php b/src/Infrastructure/Queue/Middleware/LogPushedToQueue.php index 691baee9..7637243d 100644 --- a/src/Infrastructure/Queue/Middleware/LogPushedToQueue.php +++ b/src/Infrastructure/Queue/Middleware/LogPushedToQueue.php @@ -14,13 +14,13 @@ use Closure; use CloudCreativity\Modules\Bus\SanitizedMessage; -use CloudCreativity\Modules\Contracts\Bus\Middleware\CommandMiddleware; +use CloudCreativity\Modules\Contracts\Infrastructure\Queue\QueueMiddleware; use CloudCreativity\Modules\Contracts\Messaging\Command; use CloudCreativity\Modules\Toolkit\ModuleBasename; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; -final readonly class LogPushedToQueue implements CommandMiddleware +final readonly class LogPushedToQueue implements QueueMiddleware { public function __construct( private LoggerInterface $log, @@ -29,7 +29,7 @@ public function __construct( ) { } - public function __invoke(Command $command, Closure $next): null + public function __invoke(Command $command, Closure $next): void { $name = ModuleBasename::tryFrom($command)?->toString() ?? $command::class; @@ -42,7 +42,5 @@ public function __invoke(Command $command, Closure $next): null $next($command); $this->log->log($this->queuedLevel, "Queued command {$name}.", $context); - - return null; } } diff --git a/tests/Unit/Infrastructure/Queue/Middleware/LogPushedToQueueTest.php b/tests/Unit/Infrastructure/Queue/Middleware/LogPushedToQueueTest.php index 91a86887..fa9e54da 100644 --- a/tests/Unit/Infrastructure/Queue/Middleware/LogPushedToQueueTest.php +++ b/tests/Unit/Infrastructure/Queue/Middleware/LogPushedToQueueTest.php @@ -12,11 +12,11 @@ namespace CloudCreativity\Modules\Tests\Unit\Infrastructure\Queue\Middleware; +use CloudCreativity\Modules\Contracts\Infrastructure\Queue\QueueMiddleware; use CloudCreativity\Modules\Contracts\Messaging\Command; use CloudCreativity\Modules\Infrastructure\Queue\Middleware\LogPushedToQueue; use CloudCreativity\Modules\Testing\FakeLogger; use CloudCreativity\Modules\Tests\Unit\Infrastructure\Queue\TestQueueableCommand; -use CloudCreativity\Modules\Toolkit\Result\Result; use LogicException; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; @@ -42,15 +42,22 @@ protected function tearDown(): void parent::tearDown(); } + #[Test] + public function it_is_queue_middleware(): void + { + $middleware = new LogPushedToQueue($this->logger); + + $this->assertInstanceOf(QueueMiddleware::class, $middleware); + } + #[Test] public function it_logs_with_the_default_levels(): void { $passed = null; $middleware = new LogPushedToQueue($this->logger); - $middleware($this->command, function (Command $command) use (&$passed): Result { + $middleware($this->command, function (Command $command) use (&$passed): void { $passed = $command; - return Result::ok(); }); $context = ['command' => ['name' => 'bar']]; @@ -67,7 +74,8 @@ public function it_logs_with_custom_levels(): void { $middleware = new LogPushedToQueue($this->logger, LogLevel::NOTICE, LogLevel::WARNING); - $middleware($this->command, fn (): Result => Result::ok()); + $middleware($this->command, static function (): void { + }); $this->assertSame([ 'NOTICE: Queuing command Tests:TestQueueableCommand.', @@ -82,7 +90,8 @@ public function it_uses_the_fully_qualified_class_name_when_there_is_no_module_b $name = $command::class; $middleware = new LogPushedToQueue($this->logger); - $middleware($command, fn (): Result => Result::ok()); + $middleware($command, static function (): void { + }); $this->assertSame([ "DEBUG: Queuing command {$name}.", @@ -96,9 +105,8 @@ public function it_logs_before_the_next_closure_is_invoked(): void $messages = null; $middleware = new LogPushedToQueue($this->logger); - $middleware($this->command, function () use (&$messages): Result { + $middleware($this->command, function () use (&$messages): void { $messages = $this->logger->messages(); - return Result::ok(); }); $this->assertSame(['DEBUG: Queuing command Tests:TestQueueableCommand.'], $messages); From fcd3d8c6ea70340c0f135df8e12c67432f10882f Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 14 Nov 2026 13:47:31 +0000 Subject: [PATCH 9/9] fix: allow fake container built-in services to be overridden Refs #40 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 ++ src/Testing/FakeContainer.php | 26 +++++++++----------- tests/Unit/Testing/FakeContainerTest.php | 30 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6619b086..c7996a28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ All notable changes to this project will be documented in this file. This projec - `FakeLogger::messages()` no longer drops a message of `'0'`. - `LogPushedToQueue` now implements the `QueueMiddleware` interface, instead of `CommandMiddleware`. Its `__invoke()` method now has a `void` return type, matching the queue middleware contract. +- `FakeContainer` now allows the built-in exception reporter, logger and unit of work services to be overridden via + `bind()` or `instance()`. Its `has()` method also no longer resolves the binding to check if it exists. ## [6.1.0] - 2026-11-14 diff --git a/src/Testing/FakeContainer.php b/src/Testing/FakeContainer.php index 5cf545e7..8f8c43ff 100644 --- a/src/Testing/FakeContainer.php +++ b/src/Testing/FakeContainer.php @@ -50,29 +50,25 @@ public function instance(string $id, mixed $instance): void public function get(string $id) { - $stub = match ($id) { + if (isset($this->bindings[$id])) { + return $this->bindings[$id]() ?? $this->abort($id); + } + + return match ($id) { ExceptionReporter::class => $this->reporter, LoggerInterface::class => $this->logger, UnitOfWork::class => $this->unitOfWork, - default => null, + default => $this->abort($id), }; - - if ($stub === null) { - $binding = $this->bindings[$id] ?? null; - $stub = $binding ? $binding() : null; - } - - return $stub ?? $this->abort($id); } public function has(string $id): bool { - try { - $this->get($id); - return true; - } catch (NotFoundExceptionInterface) { - return false; - } + return isset($this->bindings[$id]) || in_array($id, [ + ExceptionReporter::class, + LoggerInterface::class, + UnitOfWork::class, + ], true); } protected function abort(string $id): never diff --git a/tests/Unit/Testing/FakeContainerTest.php b/tests/Unit/Testing/FakeContainerTest.php index 6b6c8ddb..cb1730b3 100644 --- a/tests/Unit/Testing/FakeContainerTest.php +++ b/tests/Unit/Testing/FakeContainerTest.php @@ -81,6 +81,21 @@ public function it_resolves_the_unit_of_work(): void $this->assertSame($this->container->unitOfWork, $actual); } + /** + * @param class-string $id + */ + #[DataProvider('builtInServiceProvider')] + #[Test] + public function it_resolves_a_binding_that_overrides_a_built_in_service(string $id): void + { + $instance = new stdClass(); + $this->container->instance($id, $instance); + + $actual = $this->container->get($id); + + $this->assertSame($instance, $actual); + } + #[Test] public function it_resolves_a_binding_each_time_it_is_requested(): void { @@ -201,4 +216,19 @@ public function it_does_not_have_an_unknown_service(): void $this->assertFalse($actual); } + + #[Test] + public function it_does_not_resolve_a_binding_when_checking_if_it_exists(): void + { + $resolved = false; + $this->container->bind('foo', function () use (&$resolved): string { + $resolved = true; + return 'bar'; + }); + + $actual = $this->container->has('foo'); + + $this->assertTrue($actual); + $this->assertFalse($resolved); + } }