/** * @test * @depends it_should_commit_and_fetch_events * @dataProvider providePersistence * @param Persistence $persistence */ public function it_should_throw_when_events_have_been_committed_elsewhere(Persistence $persistence) { $commitId = CommitId::generate(); $streamContract = Contract::with('My.Contract'); $streamId = OrderId::generate(); $eventEnvelope = EventEnvelope::wrap(EventId::generate(), Contract::with("My.SomethingHasHappened"), "My payload2"); $this->setExpectedException(OptimisticConcurrencyFailed::class); $persistence->commit($commitId, $streamContract, $streamId, $expectedStreamVersion = 1, [$eventEnvelope]); }
/** * @param Aggregate $aggregate */ private function persistAggregate(Aggregate $aggregate) { $stream = $this->eventStore->createStream($aggregate->getAggregateContract(), $aggregate->getAggregateId()); $domainEvents = $aggregate->getChanges(); $wrapInEnvelope = function (DomainEvent $domainEvent) { $eventContract = $this->serializer->contractForDomainEvent($domainEvent); $payload = $this->serializer->serialize($eventContract, $domainEvent); return EventEnvelope::wrap(EventId::generate(), $eventContract, $payload); }; $envelopes = $domainEvents->map($wrapInEnvelope); $stream->appendAll($envelopes); // @todo share commitId across aggregates $stream->commit(CommitId::generate()); $aggregate->clearChanges(); }