Completed
Pull Request — master (#366)
by Beñat
04:43 queued 26s
created

InMemoryEventStoreSpec   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 32.89 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 6
dl 25
loc 76
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A it_get_stream_of_name_given() 12 12 1
A it_does_not_get_any_aggregate() 0 7 1
A it_gets_events_since_given_date() 0 21 1
A it_gets_empty_events_when_since_is_higher_than_persisted_events_occurred_on() 0 16 1
A it_appends_to() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Spec\Kreta\SharedKernel\Infrastructure\Persistence\InMemory\EventStore;
16
17
use Kreta\SharedKernel\Domain\Model\AggregateDoesNotExistException;
18
use Kreta\SharedKernel\Domain\Model\DomainEventCollection;
19
use Kreta\SharedKernel\Domain\Model\Identity\Id;
20
use Kreta\SharedKernel\Event\EventStore;
21
use Kreta\SharedKernel\Event\Stream;
22
use Kreta\SharedKernel\Event\StreamName;
23
use Kreta\SharedKernel\Infrastructure\Persistence\InMemory\EventStore\InMemoryEventStore;
24
use Kreta\SharedKernel\Tests\Double\Domain\Model\DomainEventStub;
25
use PhpSpec\ObjectBehavior;
26
27
class InMemoryEventStoreSpec extends ObjectBehavior
28
{
29 View Code Duplication
    function it_appends_to(Stream $stream, StreamName $streamName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $this->shouldHaveType(InMemoryEventStore::class);
32
        $this->shouldImplement(EventStore::class);
33
34
        $eventCollection = new DomainEventCollection([
35
            new DomainEventStub('foo', 'bar'),
36
        ]);
37
        $stream->events()->shouldBeCalled()->willReturn($eventCollection);
38
        $stream->name()->shouldBeCalled()->willReturn($streamName);
39
        $streamName->name()->shouldBeCalled()->willReturn('dummy');
40
        $this->append($stream);
41
    }
42
43 View Code Duplication
    function it_get_stream_of_name_given(Stream $stream, StreamName $streamName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45
        $eventCollection = new DomainEventCollection([
46
            new DomainEventStub('foo', 'bar'),
47
        ]);
48
        $stream->events()->shouldBeCalled()->willReturn($eventCollection);
49
        $stream->name()->shouldBeCalled()->willReturn($streamName);
50
        $streamName->name()->shouldBeCalled()->willReturn('dummy');
51
        $this->append($stream);
52
53
        $this->streamOfName($streamName)->shouldReturnAnInstanceOf(Stream::class);
54
    }
55
56
    function it_does_not_get_any_aggregate(StreamName $streamName, Id $aggregateId)
57
    {
58
        $streamName->aggregateId()->shouldBeCalled()->willReturn($aggregateId);
59
        $aggregateId->id()->willReturn('id');
60
61
        $this->shouldThrow(AggregateDoesNotExistException::class)->duringStreamOfName($streamName);
62
    }
63
64
    function it_gets_events_since_given_date(\DateTimeImmutable $since, Stream $stream, StreamName $streamName)
65
    {
66
        $domainEvent = new DomainEventStub('foo', 'bar');
67
        $eventCollection = new DomainEventCollection([$domainEvent]);
68
        $stream->events()->shouldBeCalled()->willReturn($eventCollection);
69
        $stream->name()->shouldBeCalled()->willReturn($streamName);
70
        $streamName->name()->shouldBeCalled()->willReturn('dummy');
71
        $this->append($stream);
72
73
        $this->eventsSince($since)->shouldReturn([
74
            [
75
                'stream_name' => 'dummy',
76
                'type'        => DomainEventStub::class,
77
                'content'     => [
78
                    'bar'        => 'bar',
79
                    'foo'        => 'foo',
80
                    'occurredOn' => $domainEvent->occurredOn()->getTimestamp(),
81
                ],
82
            ],
83
        ]);
84
    }
85
86
    function it_gets_empty_events_when_since_is_higher_than_persisted_events_occurred_on(
87
        \DateTimeImmutable $since,
88
        Stream $stream,
89
        StreamName $streamName
90
    ) {
91
        $domainEvent = new DomainEventStub('foo', 'bar');
92
        $eventCollection = new DomainEventCollection([$domainEvent]);
93
        $stream->events()->shouldBeCalled()->willReturn($eventCollection);
94
        $stream->name()->shouldBeCalled()->willReturn($streamName);
95
        $streamName->name()->shouldBeCalled()->willReturn('dummy');
96
        $this->append($stream);
97
98
        $since->getTimestamp()->willReturn($domainEvent->occurredOn()->getTimestamp() + 10);
99
100
        $this->eventsSince($since)->shouldReturn([null]);
101
    }
102
}
103