This repository was archived by the owner on Sep 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEventStoreIntegrationTest.php
More file actions
170 lines (143 loc) · 5.99 KB
/
Copy pathEventStoreIntegrationTest.php
File metadata and controls
170 lines (143 loc) · 5.99 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace HelloFresh\Tests;
use HelloFresh\Engine\CommandBus\Handler\InMemoryLocator;
use HelloFresh\Engine\CommandBus\SimpleCommandBus;
use HelloFresh\Engine\Domain\AggregateId;
use HelloFresh\Engine\Domain\StreamName;
use HelloFresh\Engine\EventBus\SimpleEventBus;
use HelloFresh\Engine\EventSourcing\AggregateRepository;
use HelloFresh\Engine\EventStore\Adapter\DbalAdapter;
use HelloFresh\Engine\EventStore\Adapter\InMemoryAdapter;
use HelloFresh\Engine\EventStore\Adapter\MongoAdapter;
use HelloFresh\Engine\EventStore\Adapter\MongoDbAdapter;
use HelloFresh\Engine\EventStore\Adapter\RedisAdapter;
use HelloFresh\Engine\EventStore\Adapter\Schema\DbalSchema;
use HelloFresh\Engine\EventStore\EventStore;
use HelloFresh\Engine\EventStore\Snapshot\Adapter\DbalSnapshotAdapter;
use HelloFresh\Engine\EventStore\Snapshot\Adapter\InMemorySnapshotAdapter;
use HelloFresh\Engine\EventStore\Snapshot\Adapter\RedisSnapshotAdapter;
use HelloFresh\Engine\EventStore\Snapshot\Adapter\Schema\SnapshotSchema;
use HelloFresh\Engine\EventStore\Snapshot\SnapshotStore;
use HelloFresh\Engine\EventStore\Snapshot\Snapshotter;
use HelloFresh\Engine\EventStore\Snapshot\Strategy\CountSnapshotStrategy;
use HelloFresh\Engine\Serializer\Adapter\JmsSerializerAdapter;
use HelloFresh\Engine\Serializer\Type\VectorHandler;
use HelloFresh\Engine\Serializer\Type\UuidSerializerHandler;
use HelloFresh\Tests\Engine\Mock\AggregateRoot;
use HelloFresh\Tests\Engine\Mock\AssignNameCommand;
use HelloFresh\Tests\Engine\Mock\AssignNameHandler;
use JMS\Serializer\Handler\HandlerRegistry;
use JMS\Serializer\SerializerBuilder;
use MongoDB\Client as MongoClient;
use Predis\Client as RedisClient;
/**
* @group integration
*/
class EventStoreIntegrationTest extends \PHPUnit_Framework_TestCase
{
private $connection;
protected function setUp()
{
$connection = $this->getDoctrineConnection();
DbalSchema::createSchema($connection);
SnapshotSchema::createSchema($connection);
}
protected function tearDown()
{
DbalSchema::dropSchema($this->connection);
SnapshotSchema::dropSchema($this->connection);
}
/**
* @test
* @dataProvider eventStoreProvider
* @param $eventStoreAdapter
* @param $snapshotAdapter
*/
public function isShouldStoreEvents($eventStoreAdapter, $snapshotAdapter)
{
$locator = new InMemoryLocator();
$commandBus = new SimpleCommandBus($locator);
$eventBus = new SimpleEventBus();
$eventStore = new EventStore($eventStoreAdapter);
$snapshotStore = new SnapshotStore($snapshotAdapter);
$snapshotter = new Snapshotter($snapshotStore, new CountSnapshotStrategy($eventStore, 5));
$aggregateRepo = new AggregateRepository($eventStore, $eventBus, $snapshotter);
$locator->addHandler(AssignNameCommand::class, new AssignNameHandler($aggregateRepo));
$aggregateRoot = AggregateRoot::create(AggregateId::generate(), 'test1');
$aggregateRepo->save($aggregateRoot);
$command = new AssignNameCommand($aggregateRoot->getAggregateRootId(), 'test2');
$commandBus->execute($command);
$commandBus->execute($command);
$commandBus->execute($command);
$commandBus->execute($command);
$this->assertEquals(6,
$eventStore->countEventsFor(new StreamName('event_stream'), $aggregateRoot->getAggregateRootId()));
}
public function eventStoreProvider()
{
//Setup serializer
$serializer = $this->configureSerializer();
$redis = $this->configureRedis();
if (version_compare(PHP_VERSION, '7.0', '>=')) {
$mongodb = $this->configureMongoDB();
$mongodbAdapter = new MongoDbAdapter($mongodb, $serializer, 'chassis');
} else {
$mongodb = $this->configureMongo();
$mongodbAdapter = new MongoAdapter($mongodb, $serializer, 'chassis');
}
return [
[new InMemoryAdapter(), new InMemorySnapshotAdapter()],
[new RedisAdapter($redis, $serializer), new RedisSnapshotAdapter($redis, $serializer)],
[$mongodbAdapter, new RedisSnapshotAdapter($redis, $serializer)],
[
new DbalAdapter($this->getDoctrineConnection(), $serializer, DbalSchema::TABLE_NAME),
new DbalSnapshotAdapter($this->getDoctrineConnection(), $serializer, SnapshotSchema::TABLE_NAME)
]
];
}
private function configureSerializer()
{
$jmsSerializer = SerializerBuilder::create()
->setMetadataDirs(['' => realpath(__DIR__ . '/Mock/Config')])
->configureHandlers(function (HandlerRegistry $registry) {
$registry->registerSubscribingHandler(new VectorHandler());
$registry->registerSubscribingHandler(new UuidSerializerHandler());
})
->addDefaultHandlers()
->build();
return new JmsSerializerAdapter($jmsSerializer);
}
private function configureMongoDB()
{
$host = getenv('MONGO_HOST');
$port = getenv('MONGO_PORT') ?: "27017";
return new MongoClient("mongodb://$host:$port");
}
private function configureMongo()
{
$host = getenv('MONGO_HOST');
$port = getenv('MONGO_PORT') ?: "27017";
return new \MongoClient("mongodb://$host:$port");
}
private function configureRedis()
{
$host = getenv('REDIS_HOST');
$port = getenv('REDIS_PORT') ?: "6379";
return new RedisClient("tcp://$host:$port");
}
private function getDoctrineConnection()
{
if ($this->connection) {
return $this->connection;
}
$connectionParams = [
'dbname' => getenv('DB_NAME'),
'user' => getenv('DB_USER'),
'password' => getenv('DB_PASSWORD'),
'host' => getenv('DB_HOST'),
'driver' => 'pdo_pgsql',
];
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
return $this->connection;
}
}