/**
  * @dataProvider valueDataProvider
  * @param mixed $fieldValue
  */
 public function testFixEntityAssociationFieldsEntity($fieldValue)
 {
     $entity = new \stdClass();
     $entity->field = $fieldValue;
     $mapping = array(array('fieldName' => 'field'));
     if ($fieldValue instanceof ArrayCollection) {
         $linkedEntity = $fieldValue->getIterator()->offsetGet(0);
     } else {
         $linkedEntity = $fieldValue;
     }
     $metadata = $this->getMockBuilder('Doctrine\\ORM\\Mapping\\ClassMetadata')->disableOriginalConstructor()->getMock();
     $metadata->expects($this->once())->method('getAssociationMappings')->will($this->returnValue($mapping));
     $metadata->expects($this->once())->method('getIdentifierValues')->with($linkedEntity)->will($this->returnValue('id'));
     $this->entityManager->expects($this->exactly(2))->method('getClassMetadata')->with(get_class($entity))->will($this->returnValue($metadata));
     $uow = $this->getMockBuilder('\\Doctrine\\ORM\\UnitOfWork')->disableOriginalConstructor()->getMock();
     $uow->expects($this->once())->method('getEntityState')->with($linkedEntity)->will($this->returnValue(UnitOfWork::STATE_DETACHED));
     $this->entityManager->expects($this->once())->method('getUnitOfWork')->will($this->returnValue($uow));
     $this->entityManager->expects($this->once())->method('find')->with(get_class($entity), 'id')->will($this->returnCallback(function () use($entity) {
         $entity->reloaded = true;
         return $entity;
     }));
     $this->fixer->fixEntityAssociationFields($entity, 0);
     if ($fieldValue instanceof ArrayCollection) {
         $this->assertTrue($entity->field->getIterator()->offsetGet(0)->reloaded);
     } else {
         $this->assertTrue($entity->field->reloaded);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function write(array $items)
 {
     foreach ($items as $item) {
         $this->entityManager->persist($item);
         $this->detachFixer->fixEntityAssociationFields($item, 1);
     }
     $this->entityManager->flush();
     $this->entityManager->clear();
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function write(array $items)
 {
     foreach ($items as $item) {
         $this->entityManager->persist($item);
         $this->detachFixer->fixEntityAssociationFields($item, 1);
     }
     $this->entityManager->flush();
     $configuration = $this->contextRegistry->getByStepExecution($this->stepExecution)->getConfiguration();
     if (empty($configuration[self::SKIP_CLEAR])) {
         $this->entityManager->clear();
     }
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function write(array $items)
 {
     $entityManager = $this->doctrineHelper->getEntityManager($this->getClassName($items));
     foreach ($items as $item) {
         $entityManager->persist($item);
         $this->detachFixer->fixEntityAssociationFields($item, 1);
     }
     $entityManager->flush();
     $configuration = $this->getConfig();
     if (empty($configuration[self::SKIP_CLEAR])) {
         $entityManager->clear();
     }
 }
Esempio n. 5
0
 /**
  * @param array $configuration
  *
  * @dataProvider configurationProvider
  */
 public function testWrite($configuration)
 {
     $fooItem = $this->getMock('FooItem');
     $barItem = $this->getMock('BarItem');
     $this->detachFixer->expects($this->at(0))->method('fixEntityAssociationFields')->with($fooItem, 1);
     $this->detachFixer->expects($this->at(1))->method('fixEntityAssociationFields')->with($barItem, 1);
     $this->entityManager->expects($this->at(0))->method('persist')->with($fooItem);
     $this->entityManager->expects($this->at(1))->method('persist')->with($barItem);
     $this->entityManager->expects($this->at(2))->method('flush');
     /** @var StepExecution $stepExecution */
     $stepExecution = $this->getMockBuilder('Akeneo\\Bundle\\BatchBundle\\Entity\\StepExecution')->disableOriginalConstructor()->getMock();
     $context = $this->getMock('Oro\\Bundle\\ImportExportBundle\\Context\\ContextInterface');
     $context->expects($this->once())->method('getConfiguration')->will($this->returnValue($configuration));
     $this->contextRegistry->expects($this->once())->method('getByStepExecution')->with($stepExecution)->will($this->returnValue($context));
     if (empty($configuration[EntityWriter::SKIP_CLEAR])) {
         $this->entityManager->expects($this->at(3))->method('clear');
     }
     $this->writer->setStepExecution($stepExecution);
     $this->writer->write([$fooItem, $barItem]);
 }