public function testAll()
 {
     $em = $this->createMock('Doctrine\\ORM\\EntityManagerInterface');
     $current_entity = new MockEntity();
     $original_entity = new MockEntity();
     $mutated_fields = ['test'];
     $event = new EntityChangedEvent($em, $current_entity, $original_entity, $mutated_fields);
     $this->assertEquals($em, $event->getEntityManager());
     $this->assertEquals($current_entity, $event->getCurrentEntity());
     $this->assertEquals($original_entity, $event->getOriginalEntity());
     $this->assertEquals($mutated_fields, $event->getMutatedFields());
 }
 /**
  * @param EntityChangedEvent $event
  */
 public function entityChanged(EntityChangedEvent $event)
 {
     $entity = $event->getCurrentEntity();
     $annotation = $this->resolver->getBlamableAnnotation($event->getEntityManager(), $entity);
     if (null === $annotation || !$entity instanceof BlamableInterface) {
         return;
     }
     $changed_at = $this->provider->getChangedAt();
     $updated_by = $this->provider->getUpdatedBy();
     $entity->setUpdatedBy($updated_by);
     $entity->setUpdatedAt($changed_at);
     if (null === $event->getOriginalEntity()) {
         // new entity, also fill in created at
         $entity->setCreatedAt($changed_at);
     }
 }
 /**
  * @param EntityChangedEvent $event
  */
 public function entityChanged(EntityChangedEvent $event)
 {
     $em = $event->getEntityManager();
     $entity = $event->getCurrentEntity();
     if (null === ($annotation = $this->resolver->getMutationAnnotation($em, $entity))) {
         return;
     }
     $fields = array_intersect($event->getMutatedFields(), $this->resolver->getMutatableFields($em, $entity));
     if (empty($fields)) {
         return;
     }
     $strategy = $annotation->getStrategy();
     if ($strategy === Mutation::STRATEGY_COPY_PREVIOUS && null === $event->getOriginalEntity()) {
         return;
     }
     switch ($strategy) {
         case Mutation::STRATEGY_COPY_CURRENT:
             $mutation_source = $entity;
             break;
         case Mutation::STRATEGY_COPY_PREVIOUS:
             $mutation_source = $event->getOriginalEntity();
             break;
         default:
             throw new \RuntimeException(sprintf("Unknown strategy '%s'.", $strategy));
     }
     $mutation = $em->getClassMetadata($this->resolver->getMutationClassName($em, $entity))->getReflectionClass()->newInstance($entity, $mutation_source);
     $em->persist($mutation);
     if ($entity instanceof MutationAwareInterface) {
         $entity->addMutation($mutation);
     }
 }
 /**
  * Checks if the current entity is eligable for a revision.
  *
  * @param EntityChangedEvent $event
  */
 private function shouldBePersisted(EntityChangedEvent $event)
 {
     if (!($entity = $event->getCurrentEntity()) instanceof RevisionableInterface) {
         return false;
     }
     $em = $event->getEntityManager();
     if (null === $this->resolver->getRevisionAnnotation($em, $entity)) {
         return false;
     }
     $fields = $this->resolver->getRevisionableFields($em, $entity);
     // only create a revision if the mutated fields are tracked
     if (count(array_intersect($fields, $event->getMutatedFields())) == 0) {
         return false;
     }
     return true;
 }