/** * @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()); }
/** * 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); }