public static function reconstitute(EventStream $events) : AggregateRoot { $instance = new self($events->aggregateId()); foreach ($events as $event) { $instance->apply($event); } return $instance; }
function it_get_stream_of_id_given(EventStream $stream, Id $aggregateId) { $aggregateId->id()->willReturn('aggregate-id'); $eventCollection = new DomainEventCollection([new DomainEventStub('foo', 'bar')]); $stream->events()->shouldBeCalled()->willReturn($eventCollection); $stream->aggregateId()->shouldBeCalled()->willReturn($aggregateId); $this->appendTo($stream); $this->streamOfId($aggregateId)->shouldReturnAnInstanceOf(EventStream::class); }
public function appendTo(EventStream $stream) { foreach ($stream->events() as $event) { $content = []; $eventReflection = new \ReflectionClass($event); foreach ($eventReflection->getProperties() as $property) { $property->setAccessible(true); $content[$property->getName()] = $property->getValue($event); } $this->store[] = ['stream_id' => $stream->aggregateId(), 'type' => get_class($event), 'content' => json_encode($content)]; } }
function it_reconstitutes_the_aggregate_root(EventStream $events, Id $aggregateId) { $events->aggregateId()->shouldBeCalled()->willReturn($aggregateId); $this::reconstitute($events)->shouldReturnAnInstanceOf(AggregateRoot::class); }