/**
  * Filtering collection
  *
  * @param ArrayCollection|CrudEntityInterface[] $collection Collection
  * @param bool                                  $replace    replace
  *
  * @return CrudUnitOfWork
  */
 public function filterCollection(ArrayCollection $collection, $replace = true)
 {
     $unitOfWork = new CrudUnitOfWork();
     foreach ($collection as $entity) {
         $entityClass = get_class($entity);
         if ($entity->getPrimaryKey()) {
             $crudEntity = $this->find($entityClass, $entity->getPrimaryKey());
         } else {
             $this->crudTransformer->initializeClassMetadata($entityClass);
             $conditions = $this->crudTransformer->getUniqueSearchConditions($entity);
             $crudEntity = $this->getEntityManager($entityClass)->getRepository($entityClass)->findOneBy($conditions);
         }
         if ($crudEntity instanceof CrudEntityInterface) {
             if ($replace) {
                 $key = $collection->indexOf($entity);
                 $collection->remove($key);
                 $collection->set($key, $crudEntity);
             }
             $unitOfWork->update($crudEntity);
         } else {
             $unitOfWork->insert($entity);
         }
     }
     return $unitOfWork;
 }
 /**
  * Testing transform of a value with association with collection
  *
  * @return void
  */
 public function testTransformPropertyValueAssociationWithCollection()
 {
     $class1 = $this->prepareClass();
     $class1->expects($this->exactly(2))->method('getId')->willReturn('class1');
     $class2 = $this->prepareClass();
     $class2->expects($this->exactly(2))->method('getId')->willReturn('class2');
     $collection = new ArrayCollection(array($class1, $class2));
     $classMetadata = $this->prepareClassMetadata();
     $this->entityManager->expects($this->once())->method('getClassMetadata')->willReturn($classMetadata);
     $this->crudTransformer->initializeClassMetadata('className');
     $classMetadata->expects($this->exactly(2))->method('hasAssociation')->willReturn(true);
     $classMetadata->expects($this->exactly(2))->method('getAssociationTargetClass')->willReturn('\\stdClass');
     $classMetadata->expects($this->exactly(4))->method('getSingleIdentifierFieldName')->willReturn('id');
     $this->assertEquals($class1, $this->crudTransformer->transformPropertyValue('id', 'class1', $collection));
     $this->assertEquals($class2, $this->crudTransformer->transformPropertyValue('id', 'class2', $collection));
 }