Example #1
0
 /**
  * @test
  */
 public function it_takes_snapshots()
 {
     $user = User::create('Alex', '*****@*****.**');
     $repository = $this->prophesize(AggregateRepository::class);
     $repository->getAggregateRoot('some id')->willReturn($user);
     $repository->extractAggregateVersion($user)->willReturn(1);
     $snapshotStore = $this->prophesize(SnapshotStore::class);
     $snapshotStore->save($this->any());
     $snapshotter = new Snapshotter($snapshotStore->reveal(), ['ProophTest\\EventStore\\Mock\\User' => $repository->reveal()]);
     $snapshotter(TakeSnapshot::withData('ProophTest\\EventStore\\Mock\\User', 'some id'));
 }
 /**
  * @test
  */
 public function an_invokable_plugin_is_loaded_by_plugin_manager_and_attached_to_event_store_by_configuration()
 {
     $pluginManager = new ServiceManager(new Config(["invokables" => ["eventlogger" => EventLoggerPlugin::class]]));
     $eventStore = new EventStore(new InMemoryAdapter(), new ProophActionEventEmitter());
     $logger = $pluginManager->get('eventlogger');
     $logger->setUp($eventStore);
     $repository = new AggregateRepository($eventStore, AggregateType::fromAggregateRootClass('ProophTest\\EventStore\\Mock\\User'), new ConfigurableAggregateTranslator(), null, null, true);
     $eventStore->beginTransaction();
     $user = User::create("Alex", "*****@*****.**");
     $repository->addAggregateRoot($user);
     $eventStore->commit();
     $loggedStreamEvents = $pluginManager->get("eventlogger")->getLoggedStreamEvents();
     $this->assertEquals(1, count($loggedStreamEvents));
 }
 /**
  * @test
  */
 public function it_publishes_take_snapshot_commands_for_all_known_aggregates()
 {
     $inMemoryAdapter = new InMemoryAdapter();
     $eventEmitter = new ProophActionEventEmitter();
     $eventStore = new EventStore($inMemoryAdapter, $eventEmitter);
     $repository = new AggregateRepository($eventStore, AggregateType::fromAggregateRootClass('ProophTest\\EventStore\\Mock\\User'), new ConfigurableAggregateTranslator());
     $result = [];
     $router = new CommandRouter();
     $router->route(TakeSnapshot::class)->to(function (TakeSnapshot $command) use(&$result) {
         $result[] = ['aggregate_type' => $command->aggregateType(), 'aggregate_id' => $command->aggregateId()];
     });
     $commandBus = new CommandBus();
     $commandBus->utilize($router);
     $plugin = new SnapshotPlugin($commandBus, 2);
     $plugin->setUp($eventStore);
     $eventStore->beginTransaction();
     $eventStore->create(new Stream(new StreamName('event_stream'), new \ArrayIterator()));
     $eventStore->commit();
     $eventStore->beginTransaction();
     $user = User::create('Alex', '*****@*****.**');
     $repository->addAggregateRoot($user);
     $eventStore->commit();
     $eventStore->beginTransaction();
     $user = $repository->getAggregateRoot($user->getId()->toString());
     $user->changeName('John');
     $user->changeName('Jane');
     $user->changeName('Jim');
     $eventStore->commit();
     $eventStore->beginTransaction();
     $eventWithoutMetadata1 = UsernameChanged::with(['new_name' => 'John Doe'], 5);
     $eventWithoutMetadata2 = UsernameChanged::with(['new_name' => 'Jane Doe'], 6);
     $eventStore->appendTo(new StreamName('event_stream'), new \ArrayIterator([$eventWithoutMetadata1, $eventWithoutMetadata2]));
     $eventStore->commit();
     $this->assertCount(2, $result);
     $this->assertArrayHasKey('aggregate_type', $result[0]);
     $this->assertArrayHasKey('aggregate_id', $result[0]);
     $this->assertArrayHasKey('aggregate_type', $result[1]);
     $this->assertArrayHasKey('aggregate_id', $result[1]);
     $this->assertEquals(User::class, $result[0]['aggregate_type']);
     $this->assertEquals(User::class, $result[1]['aggregate_type']);
 }
 /**
  * @test
  */
 public function it_publishes_take_snapshot_commands_by_event_name()
 {
     $this->eventStore->beginTransaction();
     $user = User::create('Alex', '*****@*****.**');
     $this->repository->addAggregateRoot($user);
     $this->eventStore->commit();
     $this->eventStore->beginTransaction();
     $user = $this->repository->getAggregateRoot($user->getId()->toString());
     $user->changeName('Jim');
     $this->eventStore->commit();
     $this->eventStore->beginTransaction();
     $eventWithoutMetadata1 = UsernameChanged::with(['new_name' => 'John Doe'], 5);
     $this->eventStore->appendTo(new StreamName('event_stream'), new \ArrayIterator([$eventWithoutMetadata1]));
     $this->eventStore->commit();
     $this->assertCount(1, $this->result);
     $this->assertArrayHasKey('aggregate_type', $this->result[0]);
     $this->assertArrayHasKey('aggregate_id', $this->result[0]);
     $this->assertEquals(User::class, $this->result[0]['aggregate_type']);
 }
 /**
  * @test
  */
 public function it_uses_snapshot_store_and_applies_pending_events()
 {
     $this->prepareSnapshotStoreAggregateRepository();
     $this->eventStore->beginTransaction();
     $user = User::create('John Doe', '*****@*****.**');
     $this->repository->addAggregateRoot($user);
     $this->eventStore->commit();
     $snapshot = new Snapshot(AggregateType::fromAggregateRootClass('ProophTest\\EventStore\\Mock\\User'), $user->getId()->toString(), $user, 1, new \DateTimeImmutable('now', new \DateTimeZone('UTC')));
     $this->snapshotStore->save($snapshot);
     $this->eventStore->beginTransaction();
     $fetchedUser = $this->repository->getAggregateRoot($user->getId()->toString());
     $fetchedUser->changeName('Max Mustermann');
     $this->eventStore->commit();
     $loadedEvents = [];
     $this->eventStore->getActionEventEmitter()->attachListener('loadEventsByMetadataFrom.post', function (ActionEvent $event) use(&$loadedEvents) {
         foreach ($event->getParam('streamEvents', []) as $streamEvent) {
             $loadedEvents[] = $streamEvent;
         }
         $event->getParam('streamEvents')->rewind();
     });
     $this->repository->getAggregateRoot($user->getId()->toString());
     $this->assertCount(1, $loadedEvents);
     $this->assertInstanceOf(UsernameChanged::class, $loadedEvents[0]);
     $this->assertEquals(2, $this->repository->extractAggregateVersion($fetchedUser));
 }