public function testConstructWithHistoryEvents() { $historyEvents = array(); $userNameChangedEvent = new UserNameChangedEvent(array('oldName' => null, 'newName' => 'Malocher')); $userNameChangedEvent->setSourceId(1); $userNameChangedEvent->setSourceVersion(1); $historyEvents[] = $userNameChangedEvent; $userEmailChangedEvent = new UserEmailChangedEvent(array('oldEmail' => null, 'newEmail' => '*****@*****.**')); $userEmailChangedEvent->setSourceId(1); $userEmailChangedEvent->setSourceVersion(2); $historyEvents[] = $userEmailChangedEvent; $decorator = new ProtectedAccessDecorator(); $decorator->constructManagedObjectFromHistory('Malocher\\EventStoreTest\\Coverage\\Mock\\User', '1', $historyEvents); $mockUser = $decorator->getManagedObject(); $this->assertEquals(2, $mockUser->getSourceVersion()); $this->assertEquals('Malocher', $mockUser->getName()); $this->assertEquals('*****@*****.**', $mockUser->getEmail()); //history events must not be treated as events that have to be stored $this->assertEquals(0, count($decorator->getPendingEvents())); }
/** * Save given EventSourcedObject * * @param EventSourcedObject $eventSourcedObject * * @return void */ public function save(EventSourcedObject $eventSourcedObject) { $sourceFQCN = get_class($eventSourcedObject); $decoratedObject = new ProtectedAccessDecorator(); $decoratedObject->manageObject($eventSourcedObject); $pendingEvents = $decoratedObject->getPendingEvents(); if (count($pendingEvents)) { $this->adapter->addToStream($sourceFQCN, $decoratedObject->getId(), $pendingEvents); $postPersistEvent = new PostPersistEvent($eventSourcedObject, $pendingEvents); $lastEvent = array_pop($pendingEvents); //Check if we have to generate a snapshot if ($this->autoGenerateSnapshots && $this->snapshotInterval > 0 && $lastEvent->getSourceVersion() % $this->snapshotInterval === 0) { $snapshotEvent = $decoratedObject->getSnapshot(); $this->adapter->createSnapshot($sourceFQCN, $decoratedObject->getId(), $snapshotEvent); } $this->addPendingEvent($postPersistEvent); $this->tryDispatchPostPersistEvents(); } $hash = $this->getIdentityHash(get_class($eventSourcedObject), $decoratedObject->getId()); $this->identityMap[$hash] = $eventSourcedObject; }