Example #1
0
 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);
 }
Example #2
0
$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);
Example #3
0
 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));
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function getAccount(AccountNumber $accountNumber)
 {
     return $this->uow->get(Contract::fromClass(Account::class), EventStreamIdentifier::fromString($accountNumber));
 }
Example #5
0
 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();
 }
Example #6
0
 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();
 }