-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
AntiCorruptionLayerTestCase.php
116 lines (95 loc) · 3.29 KB
/
AntiCorruptionLayerTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace EventSauce\EventSourcing\TestUtilities;
use Closure;
use EventSauce\EventSourcing\AntiCorruptionLayer\AntiCorruptionMessageConsumer;
use EventSauce\EventSourcing\AntiCorruptionLayer\AntiCorruptionMessageDispatcher;
use EventSauce\EventSourcing\AntiCorruptionLayer\PassthroughMessageTranslator;
use EventSauce\EventSourcing\CollectingMessageConsumer;
use EventSauce\EventSourcing\Message;
use EventSauce\EventSourcing\MessageConsumer;
use EventSauce\EventSourcing\MessageDispatcher;
use EventSauce\EventSourcing\SynchronousMessageDispatcher;
use PHPUnit\Framework\TestCase;
use function array_map;
abstract class AntiCorruptionLayerTestCase extends TestCase
{
/**
* @var Message[]
*/
private array $messagesToDispatch;
private Closure $dispatcher;
private Closure $consumer;
public function setUp(): void
{
parent::setUp();
$this->dispatcher = fn(MessageDispatcher $dispatcher) => $this->antiCorruptionDispatcher($dispatcher);
$this->consumer = fn(MessageConsumer $consumer) => $this->antiCorruptionConsumer($consumer);
}
protected function antiCorruptionDispatcher(MessageDispatcher $dispatcher): AntiCorruptionMessageDispatcher
{
return new AntiCorruptionMessageDispatcher(
$dispatcher,
new PassthroughMessageTranslator()
);
}
protected function antiCorruptionConsumer(MessageConsumer $consumer): AntiCorruptionMessageConsumer
{
return new AntiCorruptionMessageConsumer(
$consumer,
new PassthroughMessageTranslator()
);
}
protected function givenMessages(Message ...$messages): self
{
$this->messagesToDispatch = $messages;
return $this;
}
protected function givenEvents(object ...$events): self
{
return $this->givenMessages(
...array_map(
fn(object $object) => new Message($object),
$events
)
);
}
protected function expectNoMessages(): void
{
$this->expectMessages();
}
protected function expectNoEvents(): void
{
$this->expectEvents();
}
protected function expectMessages(Message ...$messages): void
{
$consumer = new CollectingMessageConsumer();
$aclConsumer = ($this->consumer)($consumer);
$dispatcher = new SynchronousMessageDispatcher($aclConsumer);
$aclDispatcher = ($this->dispatcher)($dispatcher);
$aclDispatcher->dispatch(...$this->messagesToDispatch);
$messagesDispatched = $consumer->collectedMessages();
$this->assertCount(count($messages), $messagesDispatched);
foreach ($messages as $index => $message) {
$this->assertEquals($message, $messagesDispatched[$index] ?? null);
}
}
protected function expectEvents(object ...$objects): void
{
$messages = array_map(
fn(object $object) => new Message($object),
$objects
);
$this->expectMessages(...$messages);
}
protected function dispatchedThrough(Closure $dispatcher): self
{
$this->dispatcher = $dispatcher;
return $this;
}
protected function consumedThrough(Closure $consumer): self
{
$this->consumer = $consumer;
return $this;
}
}