Completed
Pull Request — master (#179)
by Freek
01:35
created

FakeAggregateRoot   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 47
rs 10
c 0
b 0
f 0

6 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 4 1
A __call() 0 6 1
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use PHPUnit\Framework\Assert;
6
7
class FakeAggregateRoot
8
{
9
    /** @var \Spatie\EventProjector\AggregateRoot */
10
    private $aggregateRoot;
11
12
    public function __construct(AggregateRoot $aggregateRoot)
13
    {
14
        $this->aggregateRoot = $aggregateRoot;
15
    }
16
17
    public function given(array $events)
18
    {
19
        foreach($events as $event) {
20
            $this->aggregateRoot->recordThat($event);
21
        }
22
23
        $this->aggregateRoot->persist();
24
25
        return $this;
26
    }
27
28
    public function when($callable)
29
    {
30
        $callable($this->aggregateRoot);
31
32
        return $this;
33
    }
34
35
    public function assertNothingRecorded()
36
    {
37
        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...
38
39
        return $this;
40
    }
41
42
    public function assertRecorded(array $expectedEvents): void
43
    {
44
        Assert::assertEquals($expectedEvents, $this->aggregateRoot->getRecordedEvents());
45
    }
46
47
    public function __call($name, $arguments)
48
    {
49
        $this->aggregateRoot->$name(...$arguments);
50
51
        return $this;
52
    }
53
}
54
55