Completed
Push — master ( 00f04e...1d93d6 )
by Freek
03:46
created

FakeAggregateRoot   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 76
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\EventSourcing;
4
5
use Illuminate\Support\Arr;
6
use PHPUnit\Framework\Assert;
7
8
class FakeAggregateRoot
9
{
10
    private AggregateRoot $aggregateRoot;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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