Completed
Pull Request — master (#33)
by Frank
06:20 queued 03:42
created

AggregateRootTestCase::given()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 10
    protected function setUpEventSauce()
66
    {
67 10
        $aggregateRootFactory = $this->aggregateRootFactory();
68 10
        $this->clock = new TestClock();
69 10
        $this->aggregateRootId = $this->newAggregateRootId();
70 10
        $this->messageRepository = new InMemoryMessageRepository();
71 10
        $dispatcher = $this->messageDispatcher();
72 10
        $decorator = $this->messageDecorator();
73 10
        $this->repository = new AggregateRootRepository(
74 10
            $aggregateRootFactory,
75 10
            $this->messageRepository,
76 10
            $dispatcher,
77 10
            $decorator
78
        );
79 10
        $this->expectedEvents = [];
80 10
        $this->assertedScenario = false;
81 10
        $this->theExpectedException = null;
82 10
        $this->caughtException = null;
83 10
    }
84
85
    /**
86
     * @after
87
     */
88 10
    protected function assertScenario()
89
    {
90
        // @codeCoverageIgnoreStart
91
        if ($this->assertedScenario) {
92
            return;
93
        }
94
        // @codeCoverageIgnoreEnd
95
96
        try {
97 10
            $this->assertExpectedException($this->theExpectedException, $this->caughtException);
98 7
            $this->assertLastCommitEqualsEvents(...$this->expectedEvents);
99 7
            $this->messageRepository->purgeLastCommit();
100 7
        } finally {
101 10
            $this->assertedScenario = true;
102 10
            $this->theExpectedException = null;
103 10
            $this->caughtException = null;
104
        }
105 7
    }
106
107 9
    protected function aggregateRootId(): AggregateRootId
108
    {
109 9
        return $this->aggregateRootId;
110
    }
111
112
    abstract protected function newAggregateRootId(): AggregateRootId;
113
114
    abstract protected function aggregateRootFactory(): AggregateRootFactory;
115
116 10
    protected function reconstitutableAggregateRootFactory(string $className): AggregateRootFactory
117
    {
118 10
        return new ReconstitutableAggregateRootFactory($className);
119
    }
120
121
    /**
122
     * @return $this
123
     */
124 2
    protected function given(object ...$events)
125
    {
126 2
        $this->repository->persistEvents($this->aggregateRootId(), 0, ...$events);
127 2
        $this->messageRepository->purgeLastCommit();
128
129 2
        return $this;
130
    }
131
132 1
    public function on(AggregateRootId $id)
133
    {
134 1
        return new EventStager($id, $this->messageRepository, $this->repository, $this);
135
    }
136
137
    /**
138
     * @return $this
139
     */
140 9
    protected function when(...$arguments)
141
    {
142
        try {
143 9
            if ( ! method_exists($this, 'handle')) {
144 1
                throw new LogicException(sprintf('Class %s is missing a ::handle method.', get_class($this)));
145
            }
146
147 8
            $this->handle(...$arguments);
148 4
        } catch (Exception $exception) {
149 4
            $this->caughtException = $exception;
150
        }
151
152 9
        return $this;
153
    }
154
155
    /**
156
     * @return $this
157
     */
158 4
    protected function then(object ...$events)
159
    {
160 4
        $this->expectedEvents = $events;
161
162 4
        return $this;
163
    }
164
165
    /**
166
     * @return $this
167
     */
168 2
    public function expectToFail(Exception $expectedException)
169
    {
170 2
        $this->theExpectedException = $expectedException;
171
172 2
        return $this;
173
    }
174
175
    /**
176
     * @return $this
177
     */
178 1
    protected function thenNothingShouldHaveHappened()
179
    {
180 1
        $this->expectedEvents = [];
181
182 1
        return $this;
183
    }
184
185 7
    protected function assertLastCommitEqualsEvents(object ...$events)
186
    {
187 7
        self::assertEquals($events, $this->messageRepository->lastCommit(), 'Events are not equal.');
188 7
    }
189
190 10
    private function assertExpectedException(Exception $expectedException = null, Exception $caughtException = null)
191
    {
192 10
        if ($expectedException == $caughtException) {
193 6
            return;
194
        }
195
196 4
        if ( ! $expectedException instanceof Exception || ($caughtException instanceof Exception && (get_class($expectedException) !== get_class($caughtException)))) {
197 3
            throw $caughtException;
198
        }
199
200 1
        self::assertEquals([$expectedException], [$caughtException], '>> Exceptions are not equal.');
201 1
    }
202
203 1
    protected function pointInTime(): PointInTime
204
    {
205 1
        return $this->clock->pointInTime();
206
    }
207
208 8
    protected function clock(): Clock
209
    {
210 8
        return $this->clock;
211
    }
212
213 10
    protected function messageDispatcher(): MessageDispatcher
214
    {
215 10
        return new SynchronousMessageDispatcher(
216 10
            new ConsumerThatSerializesMessages(),
217 10
            ...$this->consumers()
218
        );
219
    }
220
221
    /**
222
     * @return Consumer[]
223
     */
224 10
    protected function consumers(): array
225
    {
226 10
        return [];
227
    }
228
229 10
    private function messageDecorator(): MessageDecorator
230
    {
231 10
        return new MessageDecoratorChain(new DefaultHeadersDecorator());
232
    }
233
}
234