Completed
Push — master ( 514e5e...49f70a )
by Freek
14s queued 10s
created

FakeAggregateRoot   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 62
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A given() 0 10 2
A when() 0 6 1
A assertNothingRecorded() 0 6 1
A assertRecorded() 0 6 1
A assertNotRecorded() 0 12 2
A __call() 0 6 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
    public function given(array $events)
19
    {
20
        foreach ($events as $event) {
21
            $this->aggregateRoot->recordThat($event);
22
        }
23
24
        $this->aggregateRoot->persist();
25
26
        return $this;
27
    }
28
29
    public function when($callable)
30
    {
31
        $callable($this->aggregateRoot);
32
33
        return $this;
34
    }
35
36
    public function assertNothingRecorded()
37
    {
38
        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...
39
40
        return $this;
41
    }
42
43
    public function assertRecorded(array $expectedEvents)
44
    {
45
        Assert::assertEquals($expectedEvents, $this->aggregateRoot->getRecordedEvents());
46
47
        return $this;
48
    }
49
50
    public function assertNotRecorded($unexpectedEventClasses): void
51
    {
52
        $actualEventClasses = array_map(function (ShouldBeStored $event) {
53
            return get_class($event);
54
        }, $this->aggregateRoot->getRecordedEvents());
55
56
        $unexpectedEventClasses = Arr::wrap($unexpectedEventClasses);
57
58
        foreach ($unexpectedEventClasses as $nonExceptedEventClass) {
59
            Assert::assertNotContains($nonExceptedEventClass, $actualEventClasses, "Did not expect to record {$nonExceptedEventClass}, but it was recorded.");
60
        }
61
    }
62
63
    public function __call($name, $arguments)
64
    {
65
        $this->aggregateRoot->$name(...$arguments);
66
67
        return $this;
68
    }
69
}
70