public function testFetchExistingStreamEnvelopes() { $contract = Contract::fromClass(\stdClass::class); $identifier = EventStreamIdentifier::fromString('Foo'); $event = $this->getMock(DomainEvent::class); $this->persistence->commit($contract, $identifier, 0, new EventCollection([$event])); $result = $this->persistence->fetch($contract, $identifier); $this->assertEquals([$event], $result); }
$dispatcher->addSubscriber($accountHistory); $dispatcher->addSubscriber($accountBalance); $dispatcher->addSubscriber($transferManager); /* * Normal event propagation * * When commands trigger actions on Account, domain events will be emitted * As reaction to those events, projectors will create data for data models */ $bus->handle(new OpenAccountCommand('Foo', 'EUR')); $bus->handle(new DepositCommand('Foo', 20)); $bus->handle(new OpenAccountCommand('Bar', 'EUR')); $bus->handle(new TransferCommand('Foo', 'Bar', 10)); $bus->handle(new WithdrawCommand('Foo', 10)); $bus->handle(new WithdrawCommand('Bar', 10)); var_dump($accountBalance, $accountHistory, $transferManager, $persistence); /* * Introducing new projector * * New projector needs to catch up * Separate emitter is created - just for new projector - it will emit all historical events */ $dispatcher = new EventDispatcher(); $eventEmitter = new DispatcherEmitter($dispatcher); $export = new AccountExportProjector(); $dispatcher->addSubscriber($export); $stream = $eventStore->openStream(Contract::fromClass(Account::class), EventStreamIdentifier::fromString('Foo')); $eventEmitter->emit(new EventCollection($stream->all())); $stream = $eventStore->openStream(Contract::fromClass(Account::class), EventStreamIdentifier::fromString('Bar')); $eventEmitter->emit(new EventCollection($stream->all())); var_dump($export);
public function testRootWillBeReconstitutedFromStream() { $contract = Contract::fromClass(AggregateRootDouble::class); $eventStreamId = EventStreamIdentifier::fromString('test'); $event = $this->getMock(DomainEvent::class); $this->persistence->expects($this->any())->method('fetch')->willReturn([$event]); $aggregate = $this->uow->get($contract, $eventStreamId); $this->assertEquals([$event], iterator_to_array($aggregate->events)); }
/** * {@inheritdoc} */ public function getAccount(AccountNumber $accountNumber) { return $this->uow->get(Contract::fromClass(Account::class), EventStreamIdentifier::fromString($accountNumber)); }
public function testCommitStreamWithoutChanges() { $contract = Contract::fromClass(static::class); $identifier = EventStreamIdentifier::fromString('foo'); $root = $this->getMock(AggregateRoot::class); $root->expects($this->any())->method('getRecordedEvents')->willReturn([]); $this->persistence->expects($this->any())->method('fetch')->willReturn([]); $this->persistence->expects($this->never())->method('commit'); $this->eventEmitter->expects($this->never())->method('emit'); $this->uow->track($contract, $identifier, $root); $this->uow->commit(); }
private function persistAggregate(Aggregate $aggregate) { if (empty($aggregate->getChanges())) { return; } $events = new EventCollection($aggregate->getChanges()); $stream = $this->eventStore->openStream($aggregate->getContract(), EventStreamIdentifier::fromString($aggregate->getIdentifier())); foreach ($events as $event) { $stream->append($event); } $stream->commit(); $this->eventEmitter->emit($events); $aggregate->clearChanges(); }