/** * @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())); }
/** * @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); }