Completed
Pull Request — master (#66)
by Frank
07:06 queued 04:59
created

AggregateRootTestCase::retrieveAggregateRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing;
6
7
use EventSauce\EventSourcing\TestUtilities\ConsumerThatSerializesMessages;
8
use EventSauce\EventSourcing\Time\Clock;
9
use EventSauce\EventSourcing\Time\TestClock;
10
use Exception;
11
use LogicException;
12
use PHPUnit\Framework\TestCase;
13
use function get_class;
14
use function method_exists;
15
use function sprintf;
16
17
/**
18
 * @method handle(...$arguments)
19
 */
20
abstract class AggregateRootTestCase extends TestCase
21
{
22
    /**
23
     * @var InMemoryMessageRepository
24
     */
25
    protected $messageRepository;
26
27
    /**
28
     * @var AggregateRootRepository
29
     */
30
    protected $repository;
31
32
    /**
33
     * @var Exception|null
34
     */
35
    private $caughtException;
36
37
    /**
38
     * @var object[]
39
     */
40
    private $expectedEvents = [];
41
42
    /**
43
     * @var Exception|null
44
     */
45
    private $theExpectedException;
46
47
    /**
48
     * @var TestClock
49
     */
50
    private $clock;
51
52
    /**
53
     * @var bool
54
     */
55
    private $assertedScenario = false;
56
57
    /**
58
     * @var AggregateRootId
59
     */
60
    protected $aggregateRootId;
61
62
    /**
63
     * @before
64
     */
65 11
    protected function setUpEventSauce(): void
66
    {
67 11
        $className = $this->aggregateRootClassName();
68 11
        $this->clock = new TestClock();
69 11
        $this->aggregateRootId = $this->newAggregateRootId();
70 11
        $this->messageRepository = new InMemoryMessageRepository();
71 11
        $dispatcher = $this->messageDispatcher();
72 11
        $decorator = $this->messageDecorator();
73 11
        $this->repository = $this->aggregateRootRepository(
74 11
            $className,
75 11
            $this->messageRepository,
76
            $dispatcher,
77
            $decorator
78
        );
79 11
        $this->expectedEvents = [];
80 11
        $this->assertedScenario = false;
81 11
        $this->theExpectedException = null;
82 11
        $this->caughtException = null;
83 11
    }
84
85
    protected function retrieveAggregateRoot(AggregateRootId $id): object
86
    {
87
        return $this->repository->retrieve($id);
88 11
    }
89
90
    protected function persistAggregateRoot(AggregateRoot $aggregateRoot): void
91
    {
92
        $this->repository->persist($aggregateRoot);
93
    }
94
95
    /**
96
     * @after
97 11
     */
98 8
    protected function assertScenario(): void
99 8
    {
100 8
        // @codeCoverageIgnoreStart
101 11
        if ($this->assertedScenario) {
102 11
            return;
103 11
        }
104
        // @codeCoverageIgnoreEnd
105 8
106
        try {
107 10
            $this->assertExpectedException($this->theExpectedException, $this->caughtException);
108
            $this->assertLastCommitEqualsEvents(...$this->expectedEvents);
109 10
            $this->messageRepository->purgeLastCommit();
110
        } finally {
111
            $this->assertedScenario = true;
112
            $this->theExpectedException = null;
113
            $this->caughtException = null;
114
        }
115
    }
116
117
    protected function aggregateRootId(): AggregateRootId
118
    {
119 2
        return $this->aggregateRootId;
120
    }
121 2
122 2
    abstract protected function newAggregateRootId(): AggregateRootId;
123
124 2
    abstract protected function aggregateRootClassName(): string;
125
126
    /**
127 1
     * @return $this
128
     */
129 1
    protected function given(object ...$events)
130
    {
131
        $this->repository->persistEvents($this->aggregateRootId(), count($events), ...$events);
132
        $this->messageRepository->purgeLastCommit();
133
134
        return $this;
135 10
    }
136
137
    public function on(AggregateRootId $id)
138 10
    {
139 1
        return new EventStager($id, $this->messageRepository, $this->repository, $this);
140
    }
141
142 9
    /**
143 4
     * @return $this
144 4
     */
145
    protected function when(...$arguments)
146
    {
147 10
        try {
148
            if ( ! method_exists($this, 'handle')) {
149
                throw new LogicException(sprintf('Class %s is missing a ::handle method.', get_class($this)));
150
            }
151
152
            $this->handle(...$arguments);
153 5
        } catch (Exception $exception) {
154
            $this->caughtException = $exception;
155 5
        }
156
157 5
        return $this;
158
    }
159
160
    /**
161
     * @return $this
162
     */
163 2
    protected function then(object ...$events)
164
    {
165 2
        $this->expectedEvents = $events;
166
167 2
        return $this;
168
    }
169
170
    /**
171
     * @return $this
172
     */
173 1
    public function expectToFail(Exception $expectedException)
174
    {
175 1
        $this->theExpectedException = $expectedException;
176
177 1
        return $this;
178
    }
179
180 8
    /**
181
     * @return $this
182 8
     */
183 8
    protected function thenNothingShouldHaveHappened()
184
    {
185 11
        $this->expectedEvents = [];
186
187 11
        return $this;
188 7
    }
189
190
    protected function assertLastCommitEqualsEvents(object ...$events): void
191
    {
192 4
        self::assertEquals($events, $this->messageRepository->lastCommit(), 'Events are not equal.');
193 4
    }
194 4
195
    private function assertExpectedException(Exception $expectedException = null, Exception $caughtException = null): void
196 3
    {
197
        if ($expectedException == $caughtException) {
198
            return;
199 1
        }
200 1
201
        if (
202 1
            null !== $caughtException && (
203
            null === $expectedException ||
204 1
            get_class($expectedException) !== get_class($caughtException))
205
        ) {
206
            throw $caughtException;
207 9
        }
208
209 9
        self::assertEquals([$expectedException], [$caughtException], '>> Exceptions are not equal.');
210
    }
211
212 11
    protected function pointInTime(): PointInTime
213
    {
214 11
        return $this->clock->pointInTime();
215 11
    }
216 11
217
    protected function clock(): Clock
218
    {
219
        return $this->clock;
220
    }
221
222
    protected function messageDispatcher(): MessageDispatcher
223 11
    {
224
        return new SynchronousMessageDispatcher(
225 11
            new ConsumerThatSerializesMessages(),
226
            ...$this->consumers()
227
        );
228 11
    }
229
230 11
    /**
231
     * @return Consumer[]
232
     */
233 11
    protected function consumers(): array
234
    {
235
        return [];
236
    }
237
238
    private function messageDecorator(): MessageDecorator
239 11
    {
240 11
        return new MessageDecoratorChain(new DefaultHeadersDecorator());
241
    }
242
243
    protected function aggregateRootRepository(
244
        string $className,
245
        MessageRepository $repository,
246
        MessageDispatcher $dispatcher,
247
        MessageDecorator $decorator
248
    ): AggregateRootRepository {
249
        return new ConstructingAggregateRootRepository(
250
            $className,
251
            $repository,
252
            $dispatcher,
253
            $decorator
254
        );
255
    }
256
}
257