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) |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.