This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAsyncAggregateTest.php
83 lines (69 loc) · 2.43 KB
/
AsyncAggregateTest.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
<?php
require 'vendor/autoload.php';
require 'config/config.php';
use App\Domain\Accounting\Account;
use App\Infrastructure\Repository\AsyncAccountRepository;
use Amp\Loop;
use Prooph\EventStoreClient\EventStoreConnectionFactory;
use Prooph\EventStore\EndPoint;
use Psa\EventSourcing\EventStoreIntegration\AggregateReflectionTranslator;
use Psa\EventSourcing\EventStoreIntegration\EventReflectionTranslator;
use Psa\EventSourcing\SnapshotStore\InMemoryStore;
Amp\Loop::run(function() use ($config) {
/*******************************************************************************
* Setting up the event store
******************************************************************************/
$eventStore = EventStoreConnectionFactory::createFromEndPoint(
new EndPoint($config['eventstore']['host'], $config['eventstore']['port'])
);
$eventStore->onConnected(function (): void {
echo 'Connected' . PHP_EOL;
});
$eventStore->onClosed(function (): void {
echo 'Connection closed' . PHP_EOL;
});
yield $eventStore->connectAsync();
/*******************************************************************************
* Setting up the repository object
******************************************************************************/
$repository = new AsyncAccountRepository(
$eventStore,
new AggregateReflectionTranslator(),
new EventReflectionTranslator(),
new InMemoryStore()
);
/*******************************************************************************
* Create, modify and save the aggregate (with two events)
******************************************************************************/
$account = Account::create(
'Test',
'Test'
);
/**
* @var $result \Prooph\EventStore\WriteResult
*/
$repository->saveAggregate($account);
/*******************************************************************************
* Restore the aggregate
******************************************************************************/
$aggregateId = (string)$account->aggregateId();
$account = $repository->getAggregate($aggregateId);
for ($i = 1; $i <= 5; $i++) {
$account->update(
'Updated ' . $i,
'Updated ' . $i
);
}
$repository->saveAggregate($account);
$repository->createSnapshot($account);
for ($i = 5; $i <= 10; $i++) {
$account->update(
'Updated ' . $i,
'Updated ' . $i
);
}
$repository->saveAggregate($account);
$result = $repository->getAggregate($aggregateId);
var_dump($result);
Loop::stop();
});