Ejemplo n.º 1
0
 /**
  * @return MigrationManager
  */
 protected function createManager()
 {
     require_once __DIR__ . '/../../../Fixtures/BlogEventSourced.php';
     $aggregates = [PostEventSourced::class, \BlogEventSourced::class];
     $store = new InMemoryMigrationStore();
     $store->persist(new Migration($aggregates, Version::fromString('0.2.0'), new \DateTime()));
     $store->persist(new Migration($aggregates, Version::fromString('0.1.0'), new \DateTime()));
     $manager = new MigrationManager($store, __DIR__ . '/../../../Fixtures/Migrations');
     $manager->registerMigrationsFromDirectory();
     return $manager;
 }
Ejemplo n.º 2
0
 /**
  * Test migrate method.
  */
 public function testMigrate()
 {
     $this->migrationsDirectory = __DIR__ . '/../../Fixtures/Migrations';
     require_once __DIR__ . '/../../Fixtures/BlogEventSourced.php';
     // simulate an application and aggregate version state
     $currentApplicationVersion = Version::fromString('0.1.0');
     $aggregateVersion = Version::fromString('0.1.0');
     VersionManager::setCurrentApplicationVersion($currentApplicationVersion);
     VersionManager::persistVersionOfClass(PostEventSourced::class, $aggregateVersion);
     VersionManager::persistVersionOfClass(\BlogEventSourced::class, $aggregateVersion);
     // creating migration store
     $aggregates = [PostEventSourced::class, \BlogEventSourced::class];
     $migratorStore = new InMemoryMigrationStore();
     $migratorStore->persist(new Migration($aggregates, $aggregateVersion, new \DateTime()));
     // creating event store
     $eventStore = new InMemoryEventStore();
     // add the event store flow
     $postId1 = PostId::fromNative(md5(rand()));
     $postEventStream1 = new EventStream($this->streamName(PostEventSourced::class), $postId1, [new PostWasCreated($postId1, 'Best restaurants in barcelona', 'empty'), new PostTitleWasChanged($postId1, 'Best cuban restaurants in barcelona')]);
     $postId2 = PostId::fromNative(md5(rand()));
     $postEventStream2 = new EventStream($this->streamName(PostEventSourced::class), $postId2, [new PostWasCreated($postId2, 'Best things to do with children in barcelona', 'empty'), new PostTitleWasChanged($postId2, 'Things to do with children in barcelona this weekend')]);
     $eventStore->persist($postEventStream1, $aggregateVersion, $currentApplicationVersion);
     $eventStore->persist($postEventStream2, $aggregateVersion, $currentApplicationVersion);
     // fake BlogEventSourced event stream
     $postId3 = PostId::fromNative(md5(rand()));
     $postEventStream2 = new EventStream($this->streamName(\BlogEventSourced::class), $postId3, []);
     $eventStore->persist($postEventStream2, $aggregateVersion, $currentApplicationVersion);
     // creating snapshot store
     $snapshotStore = new InMemorySnapshotStore();
     // creating the migrator
     $migrator = new MigratorWithSnapshot($this->getClassMetadataFactory(), $migratorStore, $eventStore, $snapshotStore, $this->migrationsDirectory);
     $emptyMigrator = new MigratorWithSnapshot($this->getClassMetadataFactory(), $migratorStore, $eventStore, $snapshotStore, __DIR__ . '/../../Fixtures/EmptyMigrations');
     $this->given($result = $emptyMigrator->migrate())->then()->boolean($result)->isFalse();
     $this->given($status = $migrator->status())->and($nextVersion = $status->nextAvailableVersion())->then()->boolean($migratorStore->hasMigration($nextVersion))->isFalse()->variable($snapshotStore->load('post_event_sourced', $postId1, $aggregateVersion, $currentApplicationVersion))->isNull()->and()->when($result = $migrator->migrate())->and(VersionManager::setCurrentApplicationVersion($migratorStore->getLast()->version()))->then()->boolean($migratorStore->hasMigration($nextVersion))->isTrue()->boolean($result)->isTrue()->variable($snapshotStore->load('post_event_sourced', $postId1, $aggregateVersion, $nextVersion))->isNotNull()->variable($snapshotStore->load('post_event_sourced', $postId2, $aggregateVersion, $nextVersion))->isNotNull()->and()->exception(function () use($migrator) {
         // because the V1_0_0\BlogEventSourcedMigration class return an invalid stream
         $migrator->migrate();
     })->isInstanceOf(\RuntimeException::class);
 }
Ejemplo n.º 3
0
 /**
  * Test MigrationsStatus method.
  */
 public function testMigrationsStatus()
 {
     $this->migrationsDirectory = __DIR__ . '/../EmptyMigrations';
     $this->given($service = $this->createService())->and($command = new MigrationsStatusCommand())->and($command->setIo($this->getIO()))->when($service->migrationsStatus($command))->then()->string($this->output->fetch())->contains(' Current Version      <c2>0</c2>')->contains(' Latest Version       <c2>none</c2>')->contains(' Next Version         <c2>none</c2>')->contains(' Executed Migrations  <c2>0</c2>')->contains(' Available Migrations <c2>0</c2>')->contains(' New Migrations       <c2>0</c2>');
     $this->migrationsDirectory = __DIR__ . '/../../../Fixtures/Event';
     $this->given($service = $this->createService())->and($command = new MigrationsStatusCommand())->and($command->setIo($this->getIO()))->when($service->migrationsStatus($command))->then()->string($this->output->fetch())->contains('Invalid migration directory');
     $this->migrationsDirectory = __DIR__ . '/../../../Fixtures/Migrations';
     $this->given($service = $this->createService())->and($command = new MigrationsStatusCommand())->and($command->setIo($this->getIO()))->when($service->migrationsStatus($command))->then()->string($this->output->fetch())->contains(' Current Version      <c2>0</c2>')->contains(' Latest Version       <c2>1.1.0</c2>')->contains(' Next Version         <c2>0.1.0</c2>')->contains(' Executed Migrations  <c2>0</c2>')->contains(' Available Migrations <c2>4</c2>')->contains(' New Migrations       <c2>4</c2>');
     require_once __DIR__ . '/../../../Fixtures/BlogEventSourced.php';
     $aggregates = [PostEventSourced::class, \BlogEventSourced::class];
     $migratorStore = new InMemoryMigrationStore();
     $migratorStore->persist(new Migration($aggregates, Version::fromString('0.1.0'), \DateTime::createFromFormat('Y-m-d H:i:s', '2016-08-26 14:12:00')));
     $migratorStore->persist(new Migration($aggregates, Version::fromString('0.2.0'), \DateTime::createFromFormat('Y-m-d H:i:s', '2016-09-01 18:30:00')));
     $migrator = new Migrator($this->getClassMetadataFactory(), $migratorStore, new InMemoryEventStore(), $this->migrationsDirectory);
     $service = new MigrationsService($migrator);
     $this->given($command = new MigrationsStatusCommand())->and($command->setIo($this->getIO()))->when($service->migrationsStatus($command))->then()->string($this->output->fetch())->contains(' Current Version      <c2>0.2.0 (2016-09-01 18:30:00)</c2>')->contains(' Latest Version       <c2>1.1.0</c2>')->contains(' Next Version         <c2>1.0.0</c2>')->contains(' Executed Migrations  <c2>2</c2>')->contains(' Available Migrations <c2>4</c2>')->contains(' New Migrations       <c2>2</c2>');
 }