Exemple #1
0
 /**
  * @test
  * @dataProvider messageProvider
  * @param AggregateIdInterface $aggregateId
  * @param AggregateRootInterface $aggregate
  * @param string $version
  */
 public function itShouldCreateASnapshotFromNamedConstructor(AggregateIdInterface $aggregateId, AggregateRootInterface $aggregate, $version)
 {
     $snapshot = Snapshot::take($aggregateId, $aggregate, $version);
     $this->assertInstanceOf(Snapshot::class, $snapshot);
     $this->assertNotEmpty((int) $snapshot->getCreatedAt()->format('u'), 'Expected microseconds to be set');
     $this->assertEquals(new \DateTimeZone('UTC'), $snapshot->getCreatedAt()->getTimezone());
 }
Exemple #2
0
 /**
  * Takes a snapshot
  * @param StreamName $streamName
  * @param AggregateRootInterface $aggregate
  * @param DomainMessage $message - The domain message
  * @return bool
  */
 public function take(StreamName $streamName, AggregateRootInterface $aggregate, DomainMessage $message)
 {
     $id = $aggregate->getAggregateRootId();
     if (!$this->strategy->isFulfilled($streamName, $aggregate)) {
         return false;
     }
     if (!$this->snapshotStore->has($id, $message->getVersion())) {
         $this->snapshotStore->save(Snapshot::take($id, $aggregate, $message->getVersion()));
     }
     return true;
 }
 /**
  * @test
  */
 public function itCanSaveASnapshot()
 {
     $id = AggregateId::generate();
     $aggregate = AggregateRoot::create($id, 'test');
     $snapshot = Snapshot::take($id, $aggregate, '10');
     $expectedSerializedAggregate = sprintf('["serialized": "%s"]', spl_object_hash($snapshot));
     $expectedStorageArray = ['version' => '10', 'created_at' => $snapshot->getCreatedAt()->format('U.u'), 'snapshot' => ['type' => AggregateRoot::class, 'payload' => $expectedSerializedAggregate]];
     $expectedStoredData = '["version etc..."]';
     $this->serializer->serialize($aggregate, 'json')->willReturn($expectedSerializedAggregate)->shouldBeCalledTimes(1);
     $this->serializer->serialize($expectedStorageArray, 'json')->willReturn($expectedStoredData)->shouldBeCalledTimes(1);
     $this->client->hset(RedisSnapshotAdapter::KEY_NAMESPACE, (string) $id, $expectedStoredData)->shouldBeCalledTimes(1);
     $adapter = $this->createAdapter();
     $adapter->save($snapshot);
 }
 /**
  * @inheritdoc
  */
 public function save(Snapshot $snapshot)
 {
     $data = ['version' => $snapshot->getVersion(), 'created_at' => $snapshot->getCreatedAt()->format('U.u'), 'snapshot' => ['type' => $snapshot->getType(), 'payload' => $this->serializer->serialize($snapshot->getAggregate(), 'json')]];
     $this->redis->hset(static::KEY_NAMESPACE, (string) $snapshot->getAggregateId(), $this->serializer->serialize($data, 'json'));
 }
 public function save(Snapshot $snapshot)
 {
     $this->snapshots->add(new Pair((string) $snapshot->getAggregateId(), $snapshot));
 }
 private function createEventData(Snapshot $snapshot)
 {
     return ['aggregate_id' => $snapshot->getAggregateId(), 'version' => $snapshot->getVersion(), 'created_at' => $snapshot->getCreatedAt()->getTimestamp(), 'type' => $snapshot->getType(), 'payload' => $this->serializer->serialize($snapshot->getAggregate(), 'json')];
 }