/** * @param array $expectedConfig */ public function assertExecuteJob(array $expectedConfig = null) { $jobResult = new JobResult(); $jobResult->setContext(new TestContext()); $jobResult->setSuccessful(true); if ($expectedConfig) { $this->jobExecutor->expects($this->any())->method('executeJob')->with($this->equalTo(ProcessorRegistry::TYPE_IMPORT), $this->equalTo('test job'), $this->callback(function (array $config) use($expectedConfig) { // dictionary if (!array_key_exists('initialSyncInterval', $config)) { return true; } $this->assertArrayHasKey(ProcessorRegistry::TYPE_IMPORT, $config); $diff = array_diff_key($config[ProcessorRegistry::TYPE_IMPORT], $expectedConfig); if (!$diff) { $this->assertEquals($expectedConfig, $config[ProcessorRegistry::TYPE_IMPORT]); return true; } $intersect = array_diff_key($config[ProcessorRegistry::TYPE_IMPORT], $diff); $this->assertEquals($expectedConfig, $intersect); if ($diff) { $this->assertArrayHasKey('initialSyncedTo', $diff); /** @var \DateTime $date */ $date = $diff['initialSyncedTo']; $interval = $date->diff(new \DateTime('now', new \DateTimeZone('UTC'))); $this->assertEmpty($interval->m); } return true; }))->will($this->returnValue($jobResult)); } else { $this->jobExecutor->expects($this->any())->method('executeJob')->with(ProcessorRegistry::TYPE_IMPORT, 'test job', $this->isType('array'))->will($this->returnValue($jobResult)); } }
/** * @dataProvider testProcessDataProvider */ public function testProcess($data, $expected) { $this->integration->expects($this->once())->method('getConnectors')->willReturn($data['integrationConnectors']); $this->integration->expects($this->any())->method('getId')->willReturn($data['channel']); $this->integration->expects($this->atLeastOnce())->method('getType')->willReturn($data['integrationType']); $this->integration->expects($this->once())->method('isEnabled')->willReturn(true); $this->registry->expects($this->any())->method('getConnectorType')->willReturnMap($data['realConnectorsMap']); $this->processorRegistry->expects($this->any())->method('getProcessorAliasesByEntity')->willReturn([]); $jobResult = new JobResult(); $jobResult->setContext(new TestContext()); $jobResult->setSuccessful(true); $mocker = $this->jobExecutor->expects($this->exactly(count($expected)))->method('executeJob'); call_user_func_array([$mocker, 'withConsecutive'], $expected); $mocker->willReturn($jobResult); $processor = $this->getSyncProcessor(); $processor->process($this->integration, $data['connector'], $data['parameters']); }
public function testOneIntegrationConnectorProcess() { $connector = 'testConnector'; $this->integration->expects($this->never())->method('getConnectors'); $this->integration->expects($this->once())->method('getId')->will($this->returnValue('testChannel')); $expectedAlias = 'test_alias'; $this->processorRegistry->expects($this->once())->method('getProcessorAliasesByEntity')->with(ProcessorRegistry::TYPE_EXPORT)->will($this->returnValue(array($expectedAlias))); $realConnector = new TestConnector(); $this->registry->expects($this->once())->method('getConnectorType')->will($this->returnValue($realConnector)); $this->integration->expects($this->once())->method('getEnabled')->will($this->returnValue(true)); $jobResult = new JobResult(); $jobResult->setContext(new TestContext()); $jobResult->setSuccessful(true); $this->jobExecutor->expects($this->once())->method('executeJob')->with('export', 'tstJobName', ['export' => ['entityName' => 'testEntity', 'channel' => 'testChannel', 'processorAlias' => $expectedAlias, 'testParameter' => 'testValue']])->will($this->returnValue($jobResult)); $processor = $this->getReverseSyncProcessor(); $processor->process($this->integration, $connector, ['testParameter' => 'testValue']); }
public function testOneChannelConnectorProcess() { $connector = 'testConnector'; $connectors = [$connector]; $this->integration->expects($this->once())->method('getConnectors')->will($this->returnValue($connectors)); $this->integration->expects($this->once())->method('getId')->will($this->returnValue('testChannel')); $this->integration->expects($this->atLeastOnce())->method('getType')->will($this->returnValue('testChannelType')); $this->integration->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); $realConnector = new TestConnector(); $this->registry->expects($this->once())->method('getConnectorType')->will($this->returnValue($realConnector)); $this->processorRegistry->expects($this->once())->method('getProcessorAliasesByEntity')->will($this->returnValue([])); $jobResult = new JobResult(); $jobResult->setContext(new TestContext()); $jobResult->setSuccessful(true); $this->jobExecutor->expects($this->once())->method('executeJob')->with('import', 'test job', ['import' => ['processorAlias' => false, 'entityName' => 'testEntity', 'channel' => 'testChannel', 'channelType' => 'testChannelType', 'testParameter' => 'testValue']])->will($this->returnValue($jobResult)); $processor = $this->getSyncProcessor(); $processor->process($this->integration, $connector, ['testParameter' => 'testValue']); }
/** * Set data to JobResult * TODO: Find a way to work with multiple amount of job and step executions * TODO https://magecore.atlassian.net/browse/BAP-2600 * * @param JobResult $jobResult * @param JobInstance $jobInstance */ protected function setJobResultData(JobResult $jobResult, JobInstance $jobInstance) { $jobResult->setJobId($jobInstance->getId()); $jobResult->setJobCode($jobInstance->getCode()); /** @var JobExecution $jobExecution */ $jobExecution = $jobInstance->getJobExecutions()->first(); if ($jobExecution) { $stepExecutions = $jobExecution->getStepExecutions(); /** @var StepExecution $firstStepExecution */ $firstStepExecution = $stepExecutions->first(); if ($firstStepExecution) { $context = $this->contextRegistry->getByStepExecution($firstStepExecution); if ($stepExecutions->count() > 1) { /** @var StepExecution $stepExecution */ foreach ($stepExecutions->slice(1) as $stepExecution) { ContextHelper::mergeContextCounters($context, $this->contextRegistry->getByStepExecution($stepExecution)); } } $jobResult->setContext($context); } } }
/** * @param string $jobType * @param string $jobName * @param array $configuration * @return JobResult */ public function executeJob($jobType, $jobName, array $configuration = array()) { // create and persist job instance and job execution $jobInstance = new JobInstance(self::CONNECTOR_NAME, $jobType, $jobName); $jobInstance->setCode($this->generateJobCode($jobName)); $jobInstance->setLabel(sprintf('%s.%s', $jobType, $jobName)); $jobInstance->setRawConfiguration($configuration); $jobExecution = new JobExecution(); $jobExecution->setJobInstance($jobInstance); $jobResult = new JobResult(); $jobResult->setSuccessful(false); $this->entityManager->beginTransaction(); try { $job = $this->jobRegistry->getJob($jobInstance); if (!$job) { throw new RuntimeException(sprintf('Can\'t find job "%s"', $jobName)); } // TODO: Refactor whole logic of job execution to perform actions in transactions $job->execute($jobExecution); $failureExceptions = $this->collectFailureExceptions($jobExecution); if ($jobExecution->getStatus()->getValue() == BatchStatus::COMPLETED && !$failureExceptions) { $this->entityManager->commit(); $jobResult->setSuccessful(true); } else { $this->entityManager->rollback(); foreach ($failureExceptions as $failureException) { $jobResult->addFailureException($failureException); } } } catch (\Exception $exception) { $this->entityManager->rollback(); $jobExecution->addFailureException($exception); $jobResult->addFailureException($exception->getMessage()); } // save job instance $this->entityManager->persist($jobInstance); $this->entityManager->flush($jobInstance); // set data to JobResult $jobResult->setJobId($jobInstance->getId()); $jobResult->setJobCode($jobInstance->getCode()); /** @var JobExecution $jobExecution */ $jobExecution = $jobInstance->getJobExecutions()->first(); if ($jobExecution) { $stepExecution = $jobExecution->getStepExecutions()->first(); if ($stepExecution) { $context = $this->contextRegistry->getByStepExecution($stepExecution); $jobResult->setContext($context); } } return $jobResult; }