Skip to content

Commit cb11366

Browse files
committed
feat: improve test fakes by adding array access and traversable
1 parent 0001e37 commit cb11366

10 files changed

+169
-8
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ trim_trailing_whitespace = true
88
insert_final_newline = true
99
indent_size = 4
1010

11-
[*.{md,yml,yaml}]
11+
[*.{yml,yaml}]
1212
indent_size = 2

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file. This projec
55

66
## Unreleased
77

8+
### Added
9+
10+
- The following fake classes now all implement array access to access captured items by index, plus can now be iterated
11+
over:
12+
- `Testing\FakeDomainEventDispatcher`
13+
- `Testing\FakeExceptionReporter`
14+
- `Testing\FakeOutboundEventPublisher`
15+
- `Testing\FakeQueue`
16+
817
## [5.0.0-rc.3] - 2025-10-14
918

1019
### Added
@@ -567,4 +576,4 @@ Initial release.
567576

568577
[3.0.0-rc.1]: https://github.com/cloudcreativity/ddd-modules/compare/v2.0.0...v3.0.0-rc.1
569578

570-
[2.0.0]: https://github.com/cloudcreativity/ddd-modules/compare/v2.0.0-rc.3...v2.0.0
579+
[2.0.0]: https://github.com/cloudcreativity/ddd-modules/compare/v2.0.0-rc.3...v2.0.0

src/Testing/FakeDomainEventDispatcher.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@
1212

1313
namespace CloudCreativity\Modules\Testing;
1414

15+
use ArrayAccess;
1516
use CloudCreativity\Modules\Contracts\Domain\Events\DomainEvent;
1617
use CloudCreativity\Modules\Contracts\Domain\Events\DomainEventDispatcher;
1718
use Countable;
19+
use Generator;
20+
use IteratorAggregate;
1821
use LogicException;
1922

20-
class FakeDomainEventDispatcher implements DomainEventDispatcher, Countable
23+
/**
24+
* @implements ArrayAccess<int, DomainEvent>
25+
* @implements IteratorAggregate<int, DomainEvent>
26+
*/
27+
class FakeDomainEventDispatcher implements DomainEventDispatcher, Countable, ArrayAccess, IteratorAggregate
2128
{
2229
/**
2330
* @var list<DomainEvent>
@@ -29,6 +36,34 @@ public function dispatch(DomainEvent $event): void
2936
$this->events[] = $event;
3037
}
3138

39+
public function offsetExists(mixed $offset): bool
40+
{
41+
return isset($this->events[$offset]);
42+
}
43+
44+
public function offsetGet(mixed $offset): DomainEvent
45+
{
46+
return $this->events[$offset];
47+
}
48+
49+
public function offsetSet(mixed $offset, mixed $value): void
50+
{
51+
throw new LogicException('Cannot set domain events.');
52+
}
53+
54+
public function offsetUnset(mixed $offset): void
55+
{
56+
throw new LogicException('Cannot unset domain events.');
57+
}
58+
59+
/**
60+
* @return Generator<int, DomainEvent>
61+
*/
62+
public function getIterator(): Generator
63+
{
64+
yield from $this->events;
65+
}
66+
3267
public function count(): int
3368
{
3469
return count($this->events);

src/Testing/FakeExceptionReporter.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@
1212

1313
namespace CloudCreativity\Modules\Testing;
1414

15+
use ArrayAccess;
1516
use CloudCreativity\Modules\Contracts\Application\Ports\Driven\ExceptionReporter;
1617
use Countable;
18+
use Generator;
19+
use IteratorAggregate;
1720
use LogicException;
1821
use Throwable;
1922

20-
final class FakeExceptionReporter implements ExceptionReporter, Countable
23+
/**
24+
* @implements ArrayAccess<int, Throwable>
25+
* @implements IteratorAggregate<int, Throwable>
26+
*/
27+
final class FakeExceptionReporter implements ExceptionReporter, Countable, ArrayAccess, IteratorAggregate
2128
{
2229
/**
2330
* @var list<Throwable>
@@ -35,6 +42,34 @@ public function report(Throwable $ex): void
3542
$this->reported[] = $ex;
3643
}
3744

45+
public function offsetExists(mixed $offset): bool
46+
{
47+
return isset($this->reported[$offset]);
48+
}
49+
50+
public function offsetGet(mixed $offset): Throwable
51+
{
52+
return $this->reported[$offset];
53+
}
54+
55+
public function offsetSet(mixed $offset, mixed $value): void
56+
{
57+
throw new LogicException('Cannot set reported exceptions.');
58+
}
59+
60+
public function offsetUnset(mixed $offset): void
61+
{
62+
throw new LogicException('Cannot unset reported exceptions.');
63+
}
64+
65+
/**
66+
* @return Generator<int, Throwable>
67+
*/
68+
public function getIterator(): Generator
69+
{
70+
yield from $this->reported;
71+
}
72+
3873
/**
3974
* Expect no exceptions to be reported.
4075
*

src/Testing/FakeOutboundEventPublisher.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@
1212

1313
namespace CloudCreativity\Modules\Testing;
1414

15+
use ArrayAccess;
1516
use CloudCreativity\Modules\Contracts\Application\Ports\Driven\OutboundEventPublisher;
1617
use CloudCreativity\Modules\Contracts\Toolkit\Messages\IntegrationEvent;
1718
use Countable;
19+
use Generator;
20+
use IteratorAggregate;
1821
use LogicException;
1922

20-
class FakeOutboundEventPublisher implements OutboundEventPublisher, Countable
23+
/**
24+
* @implements ArrayAccess<int, IntegrationEvent>
25+
* @implements IteratorAggregate<int, IntegrationEvent>
26+
*/
27+
class FakeOutboundEventPublisher implements OutboundEventPublisher, Countable, ArrayAccess, IteratorAggregate
2128
{
2229
/**
2330
* @var list<IntegrationEvent>
@@ -29,6 +36,34 @@ public function publish(IntegrationEvent $event): void
2936
$this->events[] = $event;
3037
}
3138

39+
public function offsetExists(mixed $offset): bool
40+
{
41+
return isset($this->events[$offset]);
42+
}
43+
44+
public function offsetGet(mixed $offset): IntegrationEvent
45+
{
46+
return $this->events[$offset];
47+
}
48+
49+
public function offsetSet(mixed $offset, mixed $value): void
50+
{
51+
throw new LogicException('Cannot set integration events.');
52+
}
53+
54+
public function offsetUnset(mixed $offset): void
55+
{
56+
throw new LogicException('Cannot unset integration events.');
57+
}
58+
59+
/**
60+
* @return Generator<int, IntegrationEvent>
61+
*/
62+
public function getIterator(): Generator
63+
{
64+
yield from $this->events;
65+
}
66+
3267
public function count(): int
3368
{
3469
return count($this->events);

src/Testing/FakeQueue.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@
1212

1313
namespace CloudCreativity\Modules\Testing;
1414

15+
use ArrayAccess;
1516
use CloudCreativity\Modules\Contracts\Application\Ports\Driven\Queue;
1617
use CloudCreativity\Modules\Contracts\Toolkit\Messages\Command;
1718
use Countable;
19+
use Generator;
20+
use IteratorAggregate;
1821
use LogicException;
1922

20-
class FakeQueue implements Queue, Countable
23+
/**
24+
* @implements ArrayAccess<int, Command>
25+
* @implements IteratorAggregate<int, Command>
26+
*/
27+
class FakeQueue implements Queue, Countable, ArrayAccess, IteratorAggregate
2128
{
2229
/**
2330
* @var list<Command>
@@ -29,6 +36,34 @@ public function push(Command $command): void
2936
$this->commands[] = $command;
3037
}
3138

39+
public function offsetExists(mixed $offset): bool
40+
{
41+
return isset($this->commands[$offset]);
42+
}
43+
44+
public function offsetGet(mixed $offset): Command
45+
{
46+
return $this->commands[$offset];
47+
}
48+
49+
public function offsetSet(mixed $offset, mixed $value): void
50+
{
51+
throw new LogicException('Cannot set commands.');
52+
}
53+
54+
public function offsetUnset(mixed $offset): void
55+
{
56+
throw new LogicException('Cannot unset commands.');
57+
}
58+
59+
/**
60+
* @return Generator<int, Command>
61+
*/
62+
public function getIterator(): Generator
63+
{
64+
yield from $this->commands;
65+
}
66+
3267
public function count(): int
3368
{
3469
return count($this->commands);

tests/Unit/Testing/FakeDomainEventDispatcherTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ public function testItPublishesEvents(): void
3232

3333
$this->assertInstanceOf(DomainEventDispatcher::class, $dispatcher);
3434
$this->assertCount(2, $dispatcher);
35+
$this->assertSame([$event1, $event2], $dispatcher->events);
36+
$this->assertSame([$event1, $event2], iterator_to_array($dispatcher));
3537
$this->assertSame($event1, $dispatcher->events[0]);
3638
$this->assertSame($event2, $dispatcher->events[1]);
39+
$this->assertSame($event1, $dispatcher[0]);
40+
$this->assertSame($event2, $dispatcher[1]);
3741
}
3842

3943
public function testItReturnsSoleEvent(): void

tests/Unit/Testing/FakeExceptionReporterTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public function testItReportsExceptions(): void
2929
$this->assertInstanceOf(ExceptionReporter::class, $reporter);
3030
$this->assertCount(2, $reporter);
3131
$this->assertSame([$ex1, $ex2], $reporter->reported);
32+
$this->assertSame([$ex1, $ex2], iterator_to_array($reporter));
33+
$this->assertSame($ex1, $reporter[0]);
34+
$this->assertSame($ex2, $reporter[1]);
3235
}
3336

3437
public function testItDoesNotExpectExceptions(): void

tests/Unit/Testing/FakeOutboundEventPublisherTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public function testItPublishesEvents(): void
3232

3333
$this->assertInstanceOf(OutboundEventPublisher::class, $publisher);
3434
$this->assertCount(2, $publisher);
35-
$this->assertSame($event1, $publisher->events[0]);
36-
$this->assertSame($event2, $publisher->events[1]);
35+
$this->assertSame([$event1, $event2], $publisher->events);
36+
$this->assertSame([$event1, $event2], iterator_to_array($publisher));
37+
$this->assertSame($event1, $publisher[0]);
38+
$this->assertSame($event2, $publisher[1]);
3739
}
3840

3941
public function testItReturnsSoleEvent(): void

tests/Unit/Testing/FakeQueueTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public function testItQueuesCommands(): void
2929
$this->assertInstanceOf(Queue::class, $queue);
3030
$this->assertCount(2, $queue);
3131
$this->assertSame([$command1, $command2], $queue->commands);
32+
$this->assertSame([$command1, $command2], iterator_to_array($queue));
33+
$this->assertSame($command1, $queue[0]);
34+
$this->assertSame($command2, $queue[1]);
3235
}
3336

3437
public function testItHasSoleCommand(): void

0 commit comments

Comments
 (0)