Beispiel #1
0
 /**
  * @return void
  */
 public function run()
 {
     // A logger
     $logger = new Logger('default');
     // A serializer
     $serializer = new JsonSerializer();
     // An event storage
     $eventStorage = new MemoryEventStorage();
     // A state storage
     $stateStorage = new MemoryStateStorage();
     // An event bus with an event handler bound to the DemonstratedEvent
     $eventBus = new SimpleEventBus([CreatedEvent::class => [new DemonstratedEventHandler($stateStorage)], DemonstratedEvent::class => [new DemonstratedEventHandler($stateStorage)]]);
     $eventBus->setLogger(new EventLogger($logger));
     // The event store
     $eventStore = new EventStore($eventBus, $eventStorage, $serializer, new EventClassMap([CreatedEvent::class, DemonstratedEvent::class]));
     $identity = Identity::createNew();
     // Build a series of events for an aggregate to mock initial state.
     $this->prepareSomeEvents($identity, $eventStorage, $serializer);
     // Use the EventPlayer to replay the events for an Aggregate.
     $eventPlayer = new AggregateEventPlayer($eventBus, $eventStore);
     $eventPlayer->replayEventsByAggregateId($identity);
     // Inspect the current aggregate state
     $logger->debug('Current aggregate state', ['aggregate' => (new ArrayConverter())->objectToArray($stateStorage->find($identity->getValue()))]);
 }
 /**
  * @test
  */
 public function playToVersionPublishesEventsUpToAVersion()
 {
     $serializer = $this->getSerializer();
     $eventClassMap = $this->getEventClassMap();
     $eventStorage = $this->getEventStorage();
     $eventBus = $this->getEventBus();
     $eventStore = $this->getEventStore($eventBus, $eventStorage, $serializer, $eventClassMap);
     $aggregateId = Identity::createNew();
     $eventVersion1 = new AggregateEventPlayerTest_Event();
     $eventVersion1->setVersion(1);
     $eventVersion2 = new AggregateEventPlayerTest_Event();
     $eventVersion2->setVersion(2);
     $events = new Events([$eventVersion1, $eventVersion2]);
     $eventStore->expects(self::any())->method('getEventsForAggregate')->with($aggregateId)->willReturn($events);
     $eventBus->expects(self::once())->method('publish');
     $eventPlayer = new AggregateEventPlayer($eventBus, $eventStore);
     $eventPlayer->playToVersion($aggregateId, 1);
 }