/**
  * Test creation of new object
  *
  * @return void
  */
 public function testCreateNewObject()
 {
     $objectContent = ['id' => 'one', 'second_id' => 'two'];
     $object = new EntityConverterEntity();
     $object->setIds($objectContent);
     $badProperties = new ConstraintViolationList();
     $badProperties->add(new ConstraintViolation('This is not a valid property of CLASS', 'This is not a valid property of CLASS', array(), null, null, null));
     $this->crudTransformer->expects($this->any())->method('arrayToObject')->willReturn($object);
     $this->crudTransformer->expects($this->any())->method('arrayToObjectPropertyValidation')->willReturn($badProperties);
     $referenceObject = new CircularReferenceEntity();
     $this->entityConverter->expects($this->any())->method('find')->willReturn($referenceObject);
     /** @var EntityConverterEntity $newObject */
     $newObject = $this->entityConverter->createOrUpdateNewObject('\\Ecentria\\Libraries\\EcentriaRestBundle\\Tests\\Entity\\EntityConverterEntity', new Request(array(), array(), array(), array(), array(), array(), json_encode($objectContent)), EntityConverter::MODE_CREATE, array('references' => array('class' => 'CircularReferenceEntity', 'name' => 'CircularReferenceEntity')), false);
     //test validation and references conversion
     $this->assertEquals($referenceObject, $newObject->getCircularReferenceEntity());
     $this->assertEquals($badProperties, $newObject->getViolations());
     $this->assertFalse($newObject->isValid());
     $secondIds = array('id' => 'test ONE', 'second_id' => 'test TWO');
     /** @var EntityConverterEntity $secondObject */
     $secondObject = $this->entityConverter->createOrUpdateNewObject('\\Ecentria\\Libraries\\EcentriaRestBundle\\Tests\\Entity\\EntityConverterEntity', new Request(array(), array(), $secondIds, array(), array(), array(), json_encode($objectContent)), EntityConverter::MODE_RETRIEVE, array(), false);
     //test set ids
     $this->assertEquals($secondIds, $secondObject->getIds());
     // Testing set Ids for update mode
     $secondObject = $this->entityConverter->createOrUpdateNewObject('\\Ecentria\\Libraries\\EcentriaRestBundle\\Tests\\Entity\\EntityConverterEntity', new Request(array(), array(), $secondIds, array(), array(), array(), json_encode($objectContent)), EntityConverter::MODE_UPDATE, array(), false);
     //test set ids
     $this->assertEquals($secondIds, $secondObject->getIds());
 }
 /**
  * Create New Object
  *
  * @param string  $class   Class name
  * @param Request $request HTTP request
  * @param bool    $create  Should a missing object be created?
  * @param array   $options Param converter options
  * @throws \RuntimeException
  * @return CrudEntityInterface|mixed
  */
 public function createNewObject($class, Request $request, $create, $options)
 {
     $ids = [];
     $data = $create ? json_decode($request->getContent(), true) : [];
     if (!is_array($data)) {
         throw new RuntimeException('Invalid JSON request content');
     }
     // Convert array into object and test validity
     $object = $this->crudTransformer->arrayToObject($data, $class);
     if ($object instanceof ValidatableInterface && $create) {
         $violations = $this->crudTransformer->arrayToObjectPropertyValidation($data, $class);
         $valid = !(bool) $violations->count();
         $object->setViolations($violations);
         $object->setValid($valid);
     }
     // Get list of ids from request attributes
     if (!$create && $object instanceof CrudEntityInterface) {
         foreach ($object->getIds() as $field => $value) {
             $ids[$field] = $request->attributes->get($field);
         }
         $object->setIds($ids);
     }
     // Convert external entity references into associated objects
     if (isset($options['references'])) {
         $references = !is_array(current($options['references'])) ? array($options['references']) : $options['references'];
         foreach ($references as $reference) {
             $entity = $this->findObject($reference['class'], $request, array_merge($reference, $options), $reference['name']);
             $setter = $this->crudTransformer->getPropertySetter($reference['name']);
             if (method_exists($object, $setter) && is_object($entity)) {
                 $object->{$setter}($entity);
             }
         }
     }
     return $object;
 }
 /**
  * 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;
 }
 /**
  * Stores the object in the request.
  *
  * @param Request        $request       The request
  * @param ParamConverter $configuration Contains the name, class and options of the object
  *
  * @return bool    True if the object has been successfully set, else false
  */
 public function apply(Request $request, ParamConverter $configuration)
 {
     $name = $configuration->getName();
     $class = $configuration->getClass();
     $data = json_decode($request->getContent(), true);
     $collection = $this->crudTransformer->arrayToCollection($data, $class);
     /**
      * Adding transformed collection
      * to request attribute.
      */
     $request->attributes->set($name, $collection);
     /**
      * Alias to access current collection
      * Used by exception listener
      */
     $request->attributes->set(Alias::DATA, $name);
     return true;
 }
 /**
  * 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));
 }