/**
* @test
*/
public function it_uses_snapshot_store_and_applies_pending_events()
{
$this->prepareSnapshotStoreAggregateRepository();
$this->eventStore->beginTransaction();
$user = User::create('John Doe', '*****@*****.**');
$this->repository->addAggregateRoot($user);
$this->eventStore->commit();
$snapshot = new Snapshot(AggregateType::fromAggregateRootClass('ProophTest\\EventStore\\Mock\\User'), $user->getId()->toString(), $user, 1, new \DateTimeImmutable('now', new \DateTimeZone('UTC')));
$this->snapshotStore->save($snapshot);
$this->eventStore->beginTransaction();
$fetchedUser = $this->repository->getAggregateRoot($user->getId()->toString());
$fetchedUser->changeName('Max Mustermann');
$this->eventStore->commit();
$loadedEvents = [];
$this->eventStore->getActionEventEmitter()->attachListener('loadEventsByMetadataFrom.post', function (ActionEvent $event) use(&$loadedEvents) {
foreach ($event->getParam('streamEvents', []) as $streamEvent) {
$loadedEvents[] = $streamEvent;
}
$event->getParam('streamEvents')->rewind();
});
$this->repository->getAggregateRoot($user->getId()->toString());
$this->assertCount(1, $loadedEvents);
$this->assertInstanceOf(UsernameChanged::class, $loadedEvents[0]);
$this->assertEquals(2, $this->repository->extractAggregateVersion($fetchedUser));
}