/**
  * @param array $contextSharedKeys
  * @param array $context
  * @param array $job
  * @param bool $isJobSuccess
  * @param int $jobExecutions
  * @param array $expectedContext
  *
  * @dataProvider executeDataProvider
  */
 public function testExecute(array $contextSharedKeys, array $context, array $job, $isJobSuccess = true, $jobExecutions = 0, $expectedContext = [])
 {
     if ($job) {
         list($jobType, $jobName) = $job;
         $this->executor->addPostProcessingJob($jobType, $jobName);
     }
     $this->executor->setContextSharedKeys($contextSharedKeys);
     /** @var \PHPUnit_Framework_MockObject_MockObject|StepExecution $stepExecution */
     $stepExecution = $this->getMockBuilder('Akeneo\\Bundle\\BatchBundle\\Entity\\StepExecution')->disableOriginalConstructor()->getMock();
     $executionContext = new ExecutionContext();
     foreach ($context as $key => $value) {
         $executionContext->put($key, $value);
     }
     $jobExecution = $this->getMock('Akeneo\\Bundle\\BatchBundle\\Entity\\JobExecution');
     $jobInstance = $this->getMock('Akeneo\\Bundle\\BatchBundle\\Entity\\JobInstance');
     $jobExecution->expects($this->any())->method('getJobInstance')->will($this->returnValue($jobInstance));
     $jobExecution->expects($this->any())->method('getExecutionContext')->will($this->returnValue($executionContext));
     $stepExecution->expects($this->any())->method('getJobExecution')->will($this->returnValue($jobExecution));
     $this->executor->setStepExecution($stepExecution);
     $jobResult = new JobResult();
     $jobResult->setSuccessful($isJobSuccess);
     if (!$isJobSuccess) {
         $this->setExpectedException('Oro\\Bundle\\ImportExportBundle\\Exception\\RuntimeException');
     }
     $this->jobExecutor->expects($this->exactly($jobExecutions))->method('executeJob')->willReturn($jobResult);
     $this->processor->expects($this->any())->method('process')->willReturnArgument(0);
     $this->reader->expects($this->atLeastOnce())->method('read')->willReturnOnConsecutiveCalls(new \stdClass(), new \stdClass(), new \stdClass(), null);
     $this->executor->execute();
     $this->assertEquals($expectedContext, $executionContext->getKeys());
 }
 /**
  * {@inheritdoc}
  */
 public function doExecute(StepExecution $stepExecution)
 {
     $itemsToWrite = array();
     $writeCount = 0;
     $this->initializeStepElements($stepExecution);
     $stopExecution = false;
     while (!$stopExecution) {
         try {
             $readItem = $this->reader->read();
             if (null === $readItem) {
                 $stopExecution = true;
                 continue;
             }
         } catch (InvalidItemException $e) {
             $this->handleStepExecutionWarning($this->stepExecution, $this->reader, $e);
             continue;
         }
         $processedItem = $this->process($readItem);
         if (null !== $processedItem) {
             $itemsToWrite[] = $processedItem;
             $writeCount++;
             if (0 === $writeCount % $this->batchSize) {
                 $this->write($itemsToWrite);
                 $itemsToWrite = array();
                 $this->getJobRepository()->updateStepExecution($stepExecution);
             }
         }
     }
     if (count($itemsToWrite) > 0) {
         $this->write($itemsToWrite);
     }
     $this->flushStepElements();
 }
 /**
  * {@inheritdoc}
  */
 public function execute(StepExecutionWarningHandlerInterface $warningHandler = null)
 {
     $itemsToWrite = [];
     $writeCount = 0;
     $scheduleWrite = false;
     try {
         $stopExecution = false;
         while (!$stopExecution) {
             try {
                 $readItem = $this->reader->read();
                 if (null === $readItem) {
                     $stopExecution = true;
                     continue;
                 }
             } catch (InvalidItemException $e) {
                 $this->handleStepExecutionWarning($this->reader, $e, $warningHandler);
                 continue;
             }
             $processedItem = $this->process($readItem, $warningHandler);
             if (null !== $processedItem) {
                 $itemsToWrite[] = $processedItem;
                 $writeCount++;
                 if (0 === $writeCount % $this->batchSize || $scheduleWrite) {
                     $this->write($itemsToWrite, $warningHandler);
                     $itemsToWrite = [];
                     $scheduleWrite = false;
                 }
             }
             if ($this->reader instanceof IteratorBasedReader) {
                 $sourceIterator = $this->reader->getSourceIterator();
                 if ($sourceIterator instanceof SubordinateReaderInterface && $sourceIterator->writeRequired()) {
                     $scheduleWrite = true;
                 }
             }
         }
         if (count($itemsToWrite) > 0) {
             $this->write($itemsToWrite, $warningHandler);
         }
         $this->ensureResourcesReleased($warningHandler);
     } catch (\Exception $error) {
         $this->ensureResourcesReleased($warningHandler);
         throw $error;
     }
 }
Beispiel #4
0
 /**
  * Executes a step
  *
  * @param StepExecutionWarningHandlerInterface|null $warningHandler
  *
  * @throws \Exception If any critical error occurs
  */
 public function execute(StepExecutionWarningHandlerInterface $warningHandler = null)
 {
     $itemsToWrite = array();
     $writeCount = 0;
     try {
         $stopExecution = false;
         while (!$stopExecution) {
             try {
                 $readItem = $this->reader->read();
                 if (null === $readItem) {
                     $stopExecution = true;
                     continue;
                 }
             } catch (InvalidItemException $e) {
                 $this->handleStepExecutionWarning($this->reader, $e, $warningHandler);
                 continue;
             }
             $processedItem = $this->process($readItem, $warningHandler);
             if (null !== $processedItem) {
                 $itemsToWrite[] = $processedItem;
                 $writeCount++;
                 if (0 === $writeCount % $this->batchSize) {
                     $this->write($itemsToWrite, $warningHandler);
                     $itemsToWrite = array();
                 }
             }
         }
         if (count($itemsToWrite) > 0) {
             $this->write($itemsToWrite, $warningHandler);
         }
         $this->ensureResourcesReleased($warningHandler);
     } catch (\Exception $error) {
         $this->ensureResourcesReleased($warningHandler);
         throw $error;
     }
 }