1 | <?php |
||
15 | abstract class AggregateRootTestCase extends TestCase |
||
16 | { |
||
17 | /** |
||
18 | * @var InMemoryMessageRepository |
||
19 | */ |
||
20 | private $messageRepository; |
||
21 | |||
22 | /** |
||
23 | * @var AggregateRootRepository |
||
24 | */ |
||
25 | protected $repository; |
||
26 | |||
27 | /** |
||
28 | * @var Exception|null |
||
29 | */ |
||
30 | private $caughtException; |
||
31 | |||
32 | /** |
||
33 | * @var Event[] |
||
34 | */ |
||
35 | private $expectedEvents = []; |
||
36 | |||
37 | /** |
||
38 | * @var Exception|null |
||
39 | */ |
||
40 | private $theExpectedException; |
||
41 | |||
42 | /** |
||
43 | * @var TestClock |
||
44 | */ |
||
45 | private $clock; |
||
46 | |||
47 | /** |
||
48 | * @var bool |
||
49 | */ |
||
50 | private $assertedScenario = false; |
||
51 | |||
52 | /** |
||
53 | * @var AggregateRootId |
||
54 | */ |
||
55 | protected $aggregateRootId; |
||
56 | |||
57 | /** |
||
58 | * @before |
||
59 | */ |
||
60 | 7 | protected function setUpEventStore() |
|
61 | { |
||
62 | 7 | $className = $this->aggregateRootClassName(); |
|
63 | 7 | $this->clock = new TestClock(); |
|
64 | 7 | $this->aggregateRootId = $this->aggregateRootId(); |
|
65 | 7 | $this->messageRepository = new InMemoryMessageRepository($this->messageDispatcher()); |
|
66 | 7 | $this->repository = new AggregateRootRepository($className, $this->messageRepository, new DelegatingMessageDecorator()); |
|
67 | 7 | $this->expectedEvents = []; |
|
68 | 7 | $this->assertedScenario = false; |
|
69 | 7 | $this->theExpectedException = null; |
|
70 | 7 | $this->caughtException = null; |
|
71 | 7 | } |
|
72 | |||
73 | /** |
||
74 | * @after |
||
75 | */ |
||
76 | 7 | protected function assertScenario() |
|
77 | { |
||
78 | // @codeCoverageIgnoreStart |
||
79 | if ($this->assertedScenario) { |
||
80 | return; |
||
81 | } |
||
82 | // @codeCoverageIgnoreEnd |
||
83 | |||
84 | try { |
||
85 | 7 | $this->assertExpectedException($this->theExpectedException, $this->caughtException); |
|
86 | 5 | $this->assertLastCommitEqualsEvents(... $this->expectedEvents); |
|
87 | 5 | $this->messageRepository->purgeLastCommit(); |
|
88 | 5 | } finally { |
|
89 | 7 | $this->assertedScenario = true; |
|
90 | 7 | $this->theExpectedException = null; |
|
91 | 7 | $this->caughtException = null; |
|
92 | } |
||
93 | 5 | } |
|
94 | |||
95 | abstract protected function aggregateRootId(): AggregateRootId; |
||
96 | |||
97 | abstract protected function aggregateRootClassName(): string; |
||
98 | |||
99 | /** |
||
100 | * @return $this |
||
101 | */ |
||
102 | 1 | protected function given(Event ... $events) |
|
109 | |||
110 | 1 | public function on(AggregateRootId $id) |
|
114 | |||
115 | /** |
||
116 | * @return $this |
||
117 | */ |
||
118 | 7 | protected function when(... $arguments) |
|
132 | |||
133 | /** |
||
134 | * @return $this |
||
135 | */ |
||
136 | 3 | protected function then(Event ... $events) |
|
142 | |||
143 | /** |
||
144 | * @return $this |
||
145 | */ |
||
146 | 2 | public function thenWeAreSorry(Exception $expectedException) |
|
152 | |||
153 | /** |
||
154 | * @return $this |
||
155 | */ |
||
156 | 1 | protected function thenNothingShouldHaveHappened() |
|
162 | |||
163 | 5 | protected function assertLastCommitEqualsEvents(Event ... $events) |
|
167 | |||
168 | 7 | private function assertExpectedException(Exception $expectedException = null, Exception $caughtException = null) |
|
180 | |||
181 | 3 | protected function pointInTime(): PointInTime |
|
185 | |||
186 | 7 | protected function clock(): Clock |
|
190 | |||
191 | 7 | protected function messageDispatcher(): MessageDispatcher |
|
195 | |||
196 | /** |
||
197 | * @return Consumer[] |
||
198 | */ |
||
199 | 7 | protected function consumers(): array |
|
203 | } |