forked from EventSaucePHP/EventSauce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventSourcedAggregateRootRepositoryTest.php
More file actions
82 lines (71 loc) · 3.21 KB
/
EventSourcedAggregateRootRepositoryTest.php
File metadata and controls
82 lines (71 loc) · 3.21 KB
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
<?php
declare(strict_types=1);
namespace EventSauce\EventSourcing;
use EventSauce\EventSourcing\TestUtilities\TestingAggregates\DummyAggregate;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
use function iterator_to_array;
class EventSourcedAggregateRootRepositoryTest extends TestCase
{
/**
* @test
*/
public function aggregate_versions_are_incremented_per_event(): void
{
$messageRepository = new InMemoryMessageRepository();
$repository = new EventSourcedAggregateRootRepository(DummyAggregate::class, $messageRepository);
/** @var DummyAggregate $aggregate */
$aggregateRootId = DummyAggregateRootId::generate();
$aggregate = $repository->retrieve($aggregateRootId);
$aggregate->increment();
$aggregate->increment();
$aggregate->increment();
$repository->persist($aggregate);
/** @var Message[] $messages */
$messages = iterator_to_array($messageRepository->retrieveAll($aggregateRootId));
self::assertEquals(1, $messages[0]->aggregateVersion());
self::assertEquals(2, $messages[1]->aggregateVersion());
self::assertEquals(3, $messages[2]->aggregateVersion());
}
/**
* @test
*/
public function aggregate_types_are_added_to_messages(): void
{
$messageRepository = new InMemoryMessageRepository();
$repository = new EventSourcedAggregateRootRepository(DummyAggregate::class, $messageRepository);
/** @var DummyAggregate $aggregate */
$aggregateRootId = DummyAggregateRootId::generate();
$aggregate = $repository->retrieve($aggregateRootId);
$aggregate->increment();
$aggregate->increment();
$aggregate->increment();
$repository->persist($aggregate);
$expectedAggregateRootType = 'event_sauce.event_sourcing.test_utilities.testing_aggregates.dummy_aggregate';
/** @var Message[] $messages */
$messages = iterator_to_array($messageRepository->retrieveAll($aggregateRootId));
self::assertEquals($expectedAggregateRootType, $messages[0]->aggregateRootType());
self::assertEquals($expectedAggregateRootType, $messages[1]->aggregateRootType());
self::assertEquals($expectedAggregateRootType, $messages[2]->aggregateRootType());
}
/**
* @test
*/
public function event_ids_are_added_to_messages(): void
{
$messageRepository = new InMemoryMessageRepository();
$repository = new EventSourcedAggregateRootRepository(DummyAggregate::class, $messageRepository);
/** @var DummyAggregate $aggregate */
$aggregateRootId = DummyAggregateRootId::generate();
$aggregate = $repository->retrieve($aggregateRootId);
$aggregate->increment();
$aggregate->increment();
$aggregate->increment();
$repository->persist($aggregate);
/** @var Message[] $messages */
$messages = iterator_to_array($messageRepository->retrieveAll($aggregateRootId));
self::assertTrue(Uuid::isValid($messages[0]->header(Header::EVENT_ID)));
self::assertTrue(Uuid::isValid($messages[1]->header(Header::EVENT_ID)));
self::assertTrue(Uuid::isValid($messages[2]->header(Header::EVENT_ID)));
}
}