FakeAggregateRoot::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Illuminate\Support\Arr;
6
use PHPUnit\Framework\Assert;
7
8
class FakeAggregateRoot
9
{
10
    /** @var \Spatie\EventProjector\AggregateRoot */
11
    private $aggregateRoot;
12
13
    public function __construct(AggregateRoot $aggregateRoot)
14
    {
15
        $this->aggregateRoot = $aggregateRoot;
16
    }
17
18
    /**
19
     * @param \Spatie\EventProjector\ShouldBeStored|\Spatie\EventProjector\ShouldBeStored[] $events
20
     *
21
     * @return $this
22
     */
23
    public function given($events)
24
    {
25
        $events = Arr::wrap($events);
26
27
        foreach ($events as $event) {
28
            $this->aggregateRoot->recordThat($event);
29
        }
30
31
        $this->aggregateRoot->persist();
32
33
        return $this;
34
    }
35
36
    public function when($callable)
37
    {
38
        $callable($this->aggregateRoot);
39
40
        return $this;
41
    }
42
43
    public function assertNothingRecorded()
44
    {
45
        Assert::assertCount(0, $this->aggregateRoot->getRecordedEvents());
0 ignored issues
show
Documentation introduced by
$this->aggregateRoot->getRecordedEvents() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param \Spatie\EventProjector\ShouldBeStored|\Spatie\EventProjector\ShouldBeStored[] $expectedEvents
52
     *
53
     * @return $this
54
     */
55
    public function assertRecorded($expectedEvents)
56
    {
57
        $expectedEvents = Arr::wrap($expectedEvents);
58
59
        Assert::assertEquals($expectedEvents, $this->aggregateRoot->getRecordedEvents());
60
61
        return $this;
62
    }
63
64
    public function assertNotRecorded($unexpectedEventClasses): void
65
    {
66
        $actualEventClasses = array_map(function (ShouldBeStored $event) {
67
            return get_class($event);
68
        }, $this->aggregateRoot->getRecordedEvents());
69
70
        $unexpectedEventClasses = Arr::wrap($unexpectedEventClasses);
71
72
        foreach ($unexpectedEventClasses as $nonExceptedEventClass) {
73
            Assert::assertNotContains($nonExceptedEventClass, $actualEventClasses, "Did not expect to record {$nonExceptedEventClass}, but it was recorded.");
74
        }
75
    }
76
77
    public function __call($name, $arguments)
78
    {
79
        $this->aggregateRoot->$name(...$arguments);
80
81
        return $this;
82
    }
83
}
84