-
Notifications
You must be signed in to change notification settings - Fork 42
/
AggregateRootTest.php
124 lines (88 loc) · 3.85 KB
/
AggregateRootTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* This file is part of prooph/event-sourcing.
* (c) 2014-2021 Alexander Miertsch <kontakt@codeliner.ws>
* (c) 2015-2021 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ProophTest\EventSourcing;
use ArrayIterator;
use PHPUnit\Framework\TestCase;
use Prooph\EventSourcing\EventStoreIntegration\AggregateRootDecorator;
use ProophTest\EventSourcing\Mock\BrokenUser;
use ProophTest\EventSourcing\Mock\User;
use ProophTest\EventSourcing\Mock\UserCreated;
use RuntimeException;
class AggregateRootTest extends TestCase
{
/**
* @test
*/
public function it_applies_event_by_calling_appropriate_event_handler(): void
{
$decorator = AggregateRootDecorator::newInstance();
$user = User::nameNew('John');
$recordedEvents = $decorator->extractRecordedEvents($user);
//Recording and applying events are two different steps
//In between would be the process of persisting recorded events to an event stream
//Only if this was successful the events can be applied to the aggregate root
//We skip the persistence process here and apply the events directly
$decorator->replayStreamEvents($user, new ArrayIterator($recordedEvents));
$this->assertEquals('John', $user->name());
$user->changeName('Max');
$additionalRecordedEvents = $decorator->extractRecordedEvents($user);
$decorator->replayStreamEvents($user, new ArrayIterator($additionalRecordedEvents));
$this->assertEquals('Max', $user->name());
$recordedEvents = \array_merge($recordedEvents, $additionalRecordedEvents);
$this->assertEquals(2, \count($recordedEvents));
$userCreatedEvent = $recordedEvents[0];
$this->assertEquals('John', $userCreatedEvent->name());
$this->assertEquals(1, $userCreatedEvent->version());
$userNameChangedEvent = $recordedEvents[1];
$this->assertEquals('Max', $userNameChangedEvent->newUsername());
$this->assertEquals(2, $userNameChangedEvent->version());
}
/**
* @test
*/
public function it_throws_exception_when_no_handler_on_aggregate(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Unknown event "' . UserCreated::class . '" applied to user aggregate');
$brokenUser = BrokenUser::nameNew('John');
AggregateRootDecorator::newInstance()->applyStreamEvents(
$brokenUser,
new ArrayIterator($brokenUser->accessRecordedEvents())
);
}
/**
* @test
*/
public function it_reconstructs_itself_from_history(): void
{
$user = User::nameNew('John');
$recordedEvents = $user->accessRecordedEvents();
AggregateRootDecorator::newInstance()->replayStreamEvents($user, new ArrayIterator($recordedEvents));
$user->changeName('Max');
$additionalRecordedEvents = $user->accessRecordedEvents();
AggregateRootDecorator::newInstance()->replayStreamEvents($user, new ArrayIterator($additionalRecordedEvents));
$historyEvents = new ArrayIterator(\array_merge($recordedEvents, $additionalRecordedEvents));
$sameUser = User::fromHistory($historyEvents);
$this->assertEquals($user->id(), $sameUser->id());
$this->assertEquals($user->name(), $sameUser->name());
}
/**
* @test
*/
public function it_clears_pending_events_after_returning_them(): void
{
$user = User::nameNew('John');
$recordedEvens = $user->accessRecordedEvents();
$this->assertEquals(1, \count($recordedEvens));
$recordedEvens = $user->accessRecordedEvents();
$this->assertEquals(0, \count($recordedEvens));
}
}