/** * @param Events $events */ public function play(Events $events) { /* @var $event \Apha\Message\Event */ foreach ($events->getIterator() as $event) { $this->eventBus->publish($event); } }
/** * @test */ public function playPublishesEachEvent() { $eventBus = $this->getMockBuilder(EventBus::class)->getMockForAbstractClass(); $events = new Events([new EventPlayerTest_Event(), new EventPlayerTest_Event()]); $eventBus->expects(self::exactly($events->size()))->method('publish'); $eventPlayer = new EventPlayer($eventBus); $eventPlayer->play($events); }
/** * @param Identity $aggregateId * @param string $aggregateType * @param Events $events * @param int $expectedPlayHead * @throws ConcurrencyException */ public function save(Identity $aggregateId, string $aggregateType, Events $events, int $expectedPlayHead) { if (!$this->isValidPlayHead($aggregateId, $expectedPlayHead)) { throw new ConcurrencyException($expectedPlayHead, $this->current[$aggregateId->getValue()]); } $playHead = $expectedPlayHead; foreach ($events->getIterator() as $event) { /* @var $event Event */ $playHead++; $event->setVersion($playHead); $this->saveEvent($aggregateId, $aggregateType, $event); $this->eventBus->publish($event); } }
/** * @param Event $event * @param bool $isNew * @throws UnsupportedEventException */ private function internalApplyChange(Event $event, bool $isNew) { $this->apply($event); if ($isNew) { $this->changes->add($event); } }
/** * @test */ public function saveEventsForAggregateAppendsEventsToStorageForExistingAggregate() { $eventBus = $this->getEventBus(); $storage = $this->getEventStorage(); $serializer = $this->getSerializer(); $eventMap = $this->getEventMap(); $aggregateId = Identity::createNew(); $events = new Events([new EventStoreTest_Event1(), new EventStoreTest_Event2()]); $descriptors = [EventDescriptor::reconstructFromArray(['identity' => $aggregateId->getValue(), 'type' => 'aggregateType', 'event' => EventStoreTest_Event1::getName(), 'payload' => '{}', 'playHead' => 1, 'recorded' => date('r')])]; $storage->expects(self::once())->method('find')->with($aggregateId->getValue())->willReturn($descriptors); $storage->expects(self::exactly($events->size()))->method('append'); $serializer->expects(self::exactly($events->size()))->method('serialize')->willReturn('{}'); $eventStore = new EventStore($eventBus, $storage, $serializer, $eventMap); $eventStore->save($aggregateId, 'type', $events, 1); }