1 | <?php |
||
8 | class FakeAggregateRoot |
||
9 | { |
||
10 | private AggregateRoot $aggregateRoot; |
||
|
|||
11 | |||
12 | public function __construct(AggregateRoot $aggregateRoot) |
||
13 | { |
||
14 | $this->aggregateRoot = $aggregateRoot; |
||
15 | } |
||
16 | |||
17 | /** |
||
18 | * @param \Spatie\EventSourcing\ShouldBeStored|\Spatie\EventSourcing\ShouldBeStored[] $events |
||
19 | * |
||
20 | * @return $this |
||
21 | */ |
||
22 | public function given($events) |
||
23 | { |
||
24 | $events = Arr::wrap($events); |
||
25 | |||
26 | foreach ($events as $event) { |
||
27 | $this->aggregateRoot->recordThat($event); |
||
28 | } |
||
29 | |||
30 | $this->aggregateRoot->persist(); |
||
31 | |||
32 | return $this; |
||
33 | } |
||
34 | |||
35 | public function when($callable) |
||
36 | { |
||
37 | $callable($this->aggregateRoot); |
||
38 | |||
39 | return $this; |
||
40 | } |
||
41 | |||
42 | public function assertNothingRecorded() |
||
43 | { |
||
44 | Assert::assertCount(0, $this->aggregateRoot->getRecordedEvents()); |
||
45 | |||
46 | return $this; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param \Spatie\EventSourcing\ShouldBeStored|\Spatie\EventSourcing\ShouldBeStored[] $expectedEvents |
||
51 | * |
||
52 | * @return $this |
||
53 | */ |
||
54 | public function assertRecorded($expectedEvents) |
||
55 | { |
||
56 | $expectedEvents = Arr::wrap($expectedEvents); |
||
57 | |||
58 | Assert::assertEquals($expectedEvents, $this->aggregateRoot->getRecordedEvents()); |
||
59 | |||
60 | return $this; |
||
61 | } |
||
62 | |||
63 | public function assertNotRecorded($unexpectedEventClasses): void |
||
64 | { |
||
65 | $actualEventClasses = array_map(fn(ShouldBeStored $event) => get_class($event), $this->aggregateRoot->getRecordedEvents()); |
||
66 | |||
67 | $unexpectedEventClasses = Arr::wrap($unexpectedEventClasses); |
||
68 | |||
69 | foreach ($unexpectedEventClasses as $nonExceptedEventClass) { |
||
70 | Assert::assertNotContains($nonExceptedEventClass, $actualEventClasses, "Did not expect to record {$nonExceptedEventClass}, but it was recorded."); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | public function __call($name, $arguments) |
||
75 | { |
||
76 | $this->aggregateRoot->$name(...$arguments); |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | } |
||
81 |