Beispiel #1
0
 /**
  * {@inheritdoc}
  */
 public function wrap(Identifies $aggregateId, DomainEvents $domainEvents)
 {
     $lookupKey = $aggregateId->toString();
     if (!isset($this->aggregateVersions[$lookupKey])) {
         $this->aggregateVersions[$lookupKey] = -1;
     }
     $envelopeClass = $this->eventEnvelopeClass;
     $envelopes = [];
     /** @var DomainEvent $event */
     foreach ($domainEvents as $event) {
         $aggregateVersion = ++$this->aggregateVersions[$lookupKey];
         /** @noinspection PhpUndefinedMethodInspection */
         $envelopes[] = $envelopeClass::envelop($this->eventIdFactory->generate(), $this->eventNameResolver->resolveEventName($event), $event, $aggregateId, $aggregateVersion, Timestamp::now(), new Metadata([]));
     }
     return new EventStream($aggregateId, $envelopes);
 }
Beispiel #2
0
 /**
  * @param Identifies    $id
  * @param int           $version
  * @param Metadata|null $metadata
  * @return \PHPUnit_Framework_MockObject_MockObject
  */
 public function mockEnvelope(Identifies $id, $version, Metadata $metadata = null)
 {
     $class = 'SimpleES\\EventSourcing\\Event\\EnvelopsEvent';
     $envelope = $this->testCase->getMock($class);
     $envelope->expects($this->testCase->any())->method('eventId')->will($this->testCase->returnValue($this->mockIdentifier()));
     $envelope->expects($this->testCase->any())->method('eventName')->will($this->testCase->returnValue('event_' . ($version + 1)));
     $envelope->expects($this->testCase->any())->method('event')->will($this->testCase->returnValue($this->getEventStreamEventOne($id)));
     $envelope->expects($this->testCase->any())->method('aggregateId')->will($this->testCase->returnValue($id));
     $envelope->expects($this->testCase->any())->method('aggregateVersion')->will($this->testCase->returnValue($version));
     $envelope->expects($this->testCase->any())->method('tookPlaceAt')->will($this->testCase->returnValue(Timestamp::now()));
     if ($metadata === null) {
         $metadata = new Metadata([]);
     }
     $envelope->expects($this->testCase->any())->method('metadata')->will($this->testCase->returnValue($metadata));
     $envelope->expects($this->testCase->any())->method('enrichMetadata')->will($this->testCase->returnCallback(function (Metadata $newMetadata) use($id, $version, $metadata) {
         return $this->mockEnvelope($id, $version, $metadata->merge($newMetadata));
     }));
     return $envelope;
 }
 /**
  * {@inheritdoc}
  */
 public function read(Identifies $aggregateId)
 {
     $statement = $this->prepareSelectStatement();
     $statement->bindValue('aggregate_id', (string) $aggregateId, Type::GUID);
     $statement->execute();
     $envelopes = [];
     while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
         $eventName = $row['event_name'];
         $eventClass = $this->eventNameResolver->resolveEventClass($eventName);
         $event = $this->serializer->deserialize($row['event_payload'], $eventClass);
         $metadata = $this->serializer->deserialize($row['metadata'], 'SimpleES\\EventSourcing\\Metadata\\Metadata');
         $envelopes[] = EventEnvelope::fromStore(EventId::fromString($row['event_id']), $eventName, $event, $aggregateId, (int) $row['aggregate_version'], Timestamp::fromString($row['took_place_at']), $metadata);
     }
     if (!$envelopes) {
         throw AggregateIdNotFound::create($aggregateId);
     }
     return new EventStream($aggregateId, $envelopes);
 }
 /**
  * @param Identifies $aggregateId
  *
  * @return EventStream
  */
 private function createEventStream(Identifies $aggregateId)
 {
     $envelope1 = Mockery::mock('SimpleES\\EventSourcing\\Event\\Stream\\EnvelopsEvent');
     $envelope1->shouldReceive('eventId')->andReturn(EventId::fromString('event-1'));
     $envelope1->shouldReceive('eventName')->andReturn('an_event_happened');
     $envelope1->shouldReceive('event')->andReturn(Mockery::mock('SimpleES\\EventSourcing\\Event\\DomainEvent'));
     $envelope1->shouldReceive('aggregateId')->andReturn($aggregateId);
     $envelope1->shouldReceive('aggregateVersion')->andReturn(0);
     $envelope1->shouldReceive('tookPlaceAt')->andReturn(Timestamp::now());
     $envelope1->shouldReceive('metadata')->andReturn(new Metadata([]));
     $envelope2 = Mockery::mock('SimpleES\\EventSourcing\\Event\\Stream\\EnvelopsEvent');
     $envelope2->shouldReceive('eventId')->andReturn(EventId::fromString('event-2'));
     $envelope2->shouldReceive('eventName')->andReturn('another_event_happened');
     $envelope2->shouldReceive('event')->andReturn(Mockery::mock('SimpleES\\EventSourcing\\Event\\DomainEvent'));
     $envelope2->shouldReceive('aggregateId')->andReturn($aggregateId);
     $envelope2->shouldReceive('aggregateVersion')->andReturn(1);
     $envelope2->shouldReceive('tookPlaceAt')->andReturn(Timestamp::now());
     $envelope2->shouldReceive('metadata')->andReturn(new Metadata([]));
     $envelope3 = Mockery::mock('SimpleES\\EventSourcing\\Event\\Stream\\EnvelopsEvent');
     $envelope3->shouldReceive('eventId')->andReturn(EventId::fromString('event-3'));
     $envelope3->shouldReceive('eventName')->andReturn('yet_another_event_happened');
     $envelope3->shouldReceive('event')->andReturn(Mockery::mock('SimpleES\\EventSourcing\\Event\\DomainEvent'));
     $envelope3->shouldReceive('aggregateId')->andReturn($aggregateId);
     $envelope3->shouldReceive('aggregateVersion')->andReturn(2);
     $envelope3->shouldReceive('tookPlaceAt')->andReturn(Timestamp::now());
     $envelope3->shouldReceive('metadata')->andReturn(new Metadata([]));
     $eventStream = new EventStream($aggregateId, [$envelope1, $envelope2, $envelope3]);
     return $eventStream;
 }
Beispiel #5
0
 /**
  * @test
  */
 public function itDoesNotChangeItselfWhenSubtractedFrom()
 {
     $timestamp = Timestamp::fromString('2014-12-23T17:30:00.000000+0000');
     $timestamp->subtract(new \DateInterval('P1DT1H'));
     $this->assertSame('2014-12-23T17:30:00.000000+0000', $timestamp->toString());
 }
Beispiel #6
0
 /**
  * {@inheritdoc}
  */
 public static function envelop(Identifies $eventId, $eventName, DomainEvent $event, Identifies $aggregateId, $aggregateVersion)
 {
     return new static($eventId, $eventName, $event, $aggregateId, $aggregateVersion, Timestamp::now(), new Metadata([]));
 }