public function testGetAndSetSnapshot()
 {
     $this->eventSourcedObject->changeName('Malocher');
     $this->eventSourcedObject->changeEmail('*****@*****.**');
     $decorator = new ProtectedAccessDecorator();
     $decorator->manageObject($this->eventSourcedObject);
     $snapshotEvent = $decorator->getSnapshot();
     $this->assertEquals($this->eventSourcedObject->getSourceVersion(), $snapshotEvent->getSourceVersion());
     $this->assertEquals($this->eventSourcedObject->getId(), $snapshotEvent->getSourceId());
     $payloadCheck = array('name' => 'Malocher', 'email' => '*****@*****.**');
     $this->assertEquals($payloadCheck, $snapshotEvent->getPayload());
     $history = array($snapshotEvent);
     $decorator = new ProtectedAccessDecorator();
     $decorator->constructManagedObjectFromHistory('Malocher\\EventStoreTest\\Coverage\\Mock\\User', '1', $history);
     $mockUser = $decorator->getManagedObject();
     $this->assertEquals($this->eventSourcedObject->getSourceVersion(), $mockUser->getSourceVersion());
     $this->assertEquals('Malocher', $mockUser->getName());
     $this->assertEquals('*****@*****.**', $mockUser->getEmail());
 }
Example #2
0
 /**
  * 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;
 }