/** * {@inheritDoc} */ private function loadFromSnapshotStore(AggregateIdInterface $aggregateId, StreamName $streamName = null) { $snapshot = $this->snapshotter->get($aggregateId); if (null === $snapshot) { return null; } $streamName = $this->determineStreamName($streamName); $aggregateRoot = $snapshot->getAggregate(); $stream = $this->eventStore->fromVersion($streamName, $aggregateId, $snapshot->getVersion() + 1); if (!$stream->getIterator()->valid()) { return $aggregateRoot; } $aggregateRoot->replay($stream); return $aggregateRoot; }
/** * @param AggregateRoot $aggregateRoot * @test * @dataProvider aggregateRootProvider */ public function itShouldLoadFromSnapshot(AggregateRoot $aggregateRoot) { $stream = $aggregateRoot->getEventStream(); $this->eventStore = $this->prophesize(EventStoreInterface::class); $this->eventBus = $this->prophesize(EventBusInterface::class); $version = 100; $snapshot = $this->prophesize(Snapshot::class); $snapshot->getVersion()->shouldBeCalled()->willReturn($version); $snapshot->getAggregate()->shouldBeCalled()->willReturn($aggregateRoot); $snapshotStore = $this->prophesize(SnapshotStoreInterface::class); $snapshotStore->byId($aggregateRoot->getAggregateRootId())->shouldBeCalled()->willReturn($snapshot); $strategy = $this->prophesize(SnapshotStrategyInterface::class); $snapshotter = new Snapshotter($snapshotStore->reveal(), $strategy->reveal()); $this->eventStore->fromVersion(new StreamName('event_stream'), $aggregateRoot->getAggregateRootId(), $version + 1)->shouldBeCalled()->willReturn($stream); $repo = new AggregateRepository($this->eventStore->reveal(), $this->eventBus->reveal(), $snapshotter); $repo->load($aggregateRoot->getAggregateRootId(), AggregateRoot::class); }