Ejemplo n.º 1
0
 /**
  * Test Generate method.
  */
 public function testGenerate()
 {
     require_once __DIR__ . '/../../../Fixtures/BlogEventSourced.php';
     $this->given($generator = $this->createGenerator())->and($aggregateClass = PostEventSourced::class)->and($version = VersionManager::versionOfClass($aggregateClass))->when($generator->generate($aggregateClass, $version))->then()->boolean(file_exists($this->getMigratorFileName($aggregateClass, $version)))->isTrue()->and()->exception(function () use($generator, $aggregateClass, $version) {
         $generator->generate($aggregateClass, $version);
     })->isInstanceOf(\RuntimeException::class);
     $this->given($generator = $this->createGenerator())->and($aggregateClass = \BlogEventSourced::class)->and($version = VersionManager::versionOfClass($aggregateClass))->when($generator->generate($aggregateClass, $version))->then()->boolean(file_exists($this->getMigratorFileName($aggregateClass, $version)))->isTrue()->boolean($generator->existsDirectory($version))->isTrue();
 }
 /**
  * Load a aggregate history from the storage.
  *
  * @param IdInterface $id
  *
  * @return EventStream
  */
 protected function loadHistory(IdInterface $id)
 {
     $applicationVersion = VersionManager::currentApplicationVersion();
     $aggregateVersion = VersionManager::versionOfClass($this->aggregateClassName, $applicationVersion);
     return $this->eventStore->load($this->streamName(), $id, $aggregateVersion, $applicationVersion);
 }
Ejemplo n.º 3
0
 /**
  * @return bool
  */
 public function migrate()
 {
     $nextMigration = $this->migrationManager()->nextMigrationToExecute();
     $currentApplicationVersion = VersionManager::currentApplicationVersion();
     if ($nextMigration !== null) {
         foreach ($nextMigration->aggregates() as $aggregateMigrationClass) {
             // -- start current application context --
             /** @var MigrationInterface $migrationClass */
             $migrationClass = new $aggregateMigrationClass();
             $aggregateClassName = $migrationClass->aggregateClassName();
             $currentAggregateVersion = VersionManager::versionOfClass($aggregateClassName, $currentApplicationVersion);
             // get all event streams for this aggregate class name
             $eventStreams = $this->eventStore->loadAll($this->streamName($aggregateClassName), $currentAggregateVersion, $currentApplicationVersion);
             // -- end current application context --
             // -- start new version context --
             $nextApplicationVersion = $nextMigration->version();
             // iterate for every aggregateRoot event stream
             foreach ($eventStreams as $aggregateRootEventStream) {
                 // migrate the current aggregate event stream
                 $newAggregateRootEventStream = $migrationClass->migrate($aggregateRootEventStream);
                 if ($newAggregateRootEventStream === null || $newAggregateRootEventStream !== null && !$newAggregateRootEventStream instanceof EventStream) {
                     throw new \RuntimeException(sprintf('Invalid migration class %s. The migration method should return the new EventStream', $aggregateMigrationClass));
                 }
                 // calculate the new version for every event
                 $pathVersion = 0;
                 foreach ($newAggregateRootEventStream->events() as $event) {
                     $event->setVersion(++$pathVersion);
                 }
                 // and the new version for this aggregateRoot
                 $newAggregateRootVersion = Version::fromString($nextMigration->version()->__toString());
                 $newAggregateRootVersion->setPatch($pathVersion);
                 // persist the new event stream for this aggregateRoot.
                 $this->migrateAggregateRoot($aggregateClassName, $newAggregateRootEventStream, $newAggregateRootVersion, $nextApplicationVersion);
             }
             // persist the new version of this aggregate class in the VersionManager
             VersionManager::persistVersionOfClass($aggregateClassName, $nextMigration->version(), $nextApplicationVersion);
             // -- end new version context --
         }
         // persist the new migration in the store
         $this->migrationManager->persistMigration($nextMigration);
         return true;
     }
     return false;
 }