function it_checks_if_the_given_job_code_exists_in_the_context(JobInstance $jobInstance) { $this->addJobName('a_job_code'); $jobInstance->getJobName()->willReturn('a_job_code'); $this->isVisible([], ['jobInstance' => $jobInstance])->shouldReturn(true); $jobInstance->getJobName()->willReturn('another_job_code'); $this->isVisible([], ['jobInstance' => $jobInstance])->shouldReturn(false); }
/** * {@inheritdoc} */ public function getEditTemplate(JobInstance $jobInstance) { $jobName = $jobInstance->getJobName(); if (isset($this->jobTemplates[$jobName]) && isset($this->jobTemplates[$jobName]['templates']['edit'])) { return $this->jobTemplates[$jobName]['templates']['edit']; } return sprintf(self::DEFAULT_EDIT_TEMPLATE, ucfirst($jobInstance->getType())); }
function it_adds_a_violation_if_job_instance_has_an_unknown_type($jobRegistry, $context, JobInstanceConstraint $constraint, JobInstance $jobInstance, ConstraintViolationBuilderInterface $violation) { $jobInstance->getJobName()->willReturn(null); $jobRegistry->get(null)->willThrow(new UndefinedJobException('The job "" is not registered')); $jobInstance->getType()->willReturn('import'); $context->buildViolation($constraint->message, ['%job_type%' => 'import'])->shouldBeCalled()->willReturn($violation); $violation->atPath($constraint->property)->shouldBeCalled()->willReturn($violation); $violation->addViolation()->shouldBeCalled(); $this->validate($jobInstance, $constraint); }
function it_returns_false_for_the_unsupported_job($jobRegistry, ItemReaderInterface $reader, JobExecution $jobExecution, JobInstance $jobInstance, Job $job, ItemStep $step) { $jobInstance->getJobName()->willReturn('my_job_name'); $jobRegistry->get('my_job_name')->willReturn($job); $jobExecution->getJobInstance()->willReturn($jobInstance); $jobExecution->getId()->willReturn(12); $jobInstance->getType()->willReturn('type'); $job->getSteps()->willReturn([$step]); $step->getReader()->willReturn($reader); $this->supports($jobExecution)->shouldReturn(false); }
function it_generates_default_export_job_template(JobInstance $jobInstance) { $jobInstance->getJobName()->willReturn('an_unknown_job'); $jobInstance->getType()->willReturn('export'); $expectedJobTemplate = sprintf(JobTemplateProvider::DEFAULT_CREATE_TEMPLATE, 'Export'); $this->getCreateTemplate($jobInstance)->shouldReturn($expectedJobTemplate); $expectedJobTemplate = sprintf(JobTemplateProvider::DEFAULT_SHOW_TEMPLATE, 'Export'); $this->getShowTemplate($jobInstance)->shouldReturn($expectedJobTemplate); $expectedJobTemplate = sprintf(JobTemplateProvider::DEFAULT_EDIT_TEMPLATE, 'Export'); $this->getEditTemplate($jobInstance)->shouldReturn($expectedJobTemplate); }
/** * {@inheritdoc} */ public function createJobExecution(JobInstance $jobInstance) { if (null !== $jobInstance->getId()) { $jobInstance = $this->jobManager->merge($jobInstance); } else { $this->jobManager->persist($jobInstance); } $jobExecution = new $this->jobExecutionClass(); $jobExecution->setJobInstance($jobInstance); $this->updateJobExecution($jobExecution); return $jobExecution; }
/** * Launch a job * TODO: refactor all this * * @param JobInstance $job */ protected function launchJob(JobInstance $job) { $app = new Application($this->container->get('kernel')); $cmd = new BatchCommand(); $cmd->setContainer($this->container); $cmd->setApplication($app); $cmd->run(new ArrayInput(['command' => 'akeneo:batch:job', 'code' => $job->getCode(), '--no-debug' => true, '--no-log' => true, '-v' => true]), new ConsoleOutput()); $execution = $this->getJobExecution($job); if (!$this->executionComplete($execution)) { throw new JobExecutionException($execution); } }
/** * @param JobInstance $jobInstance * @param string $field * @param mixed $data */ protected function setData(JobInstance $jobInstance, $field, $data) { switch ($field) { case 'connector': $jobInstance->setConnector($data); break; case 'alias': $jobInstance->setJobName($data); break; case 'label': $jobInstance->setLabel($data); break; case 'type': $jobInstance->setType($data); break; case 'configuration': $job = $this->jobRegistry->get($jobInstance->getJobName()); /** @var JobParameters $jobParameters */ $jobParameters = $this->jobParametersFactory->create($job, $data); $jobInstance->setRawParameters($jobParameters->all()); break; case 'code': $jobInstance->setCode($data); break; } }
function it_normalizes_a_job_execution_instance($serializer, $translator, $labelProvider, JobInstance $jobInstance, JobExecution $jobExecution, StepExecution $exportExecution, StepExecution $cleanExecution, BatchStatus $status) { $jobExecution->getFailureExceptions()->willReturn([['message' => 'error', 'messageParameters' => ['foo' => 'bar']]]); $jobInstance->getJobName()->willReturn('wow_job'); $translator->trans('error', ['foo' => 'bar'])->willReturn('Such error'); $labelProvider->getJobLabel('wow_job')->willReturn('Wow job'); $jobExecution->isRunning()->willReturn(true); $jobExecution->getStatus()->willReturn($status); $jobExecution->getJobInstance()->willReturn($jobInstance); $status->getValue()->willReturn(1); $translator->trans('pim_import_export.batch_status.1')->willReturn('COMPLETED'); $jobExecution->getStepExecutions()->willReturn([$exportExecution, $cleanExecution]); $serializer->normalize($exportExecution, 'any', [])->willReturn('**exportExecution**'); $serializer->normalize($cleanExecution, 'any', [])->willReturn('**cleanExecution**'); $this->normalize($jobExecution, 'any')->shouldReturn(['label' => 'Wow job', 'failures' => ['Such error'], 'stepExecutions' => ['**exportExecution**', '**cleanExecution**'], 'isRunning' => true, 'status' => 'COMPLETED']); }
function it_updates_an_job_instance($jobParametersFactory, $jobRegistry, JobInstance $jobInstance, JobInterface $job, JobParameters $jobParameters) { $jobInstance->getJobName()->willReturn('fixtures_currency_csv'); $jobRegistry->get('fixtures_currency_csv')->willReturn($job); $jobParametersFactory->create($job, ['filePath' => 'currencies.csv'])->willReturn($jobParameters); $jobParameters->all()->willReturn(['filePath' => 'currencies.csv']); $jobInstance->setJobName('fixtures_currency_csv')->shouldBeCalled(); $jobInstance->setCode('fixtures_currency_csv')->shouldBeCalled(); $jobInstance->setConnector('Data fixtures')->shouldBeCalled(); $jobInstance->setLabel('Currencies data fixtures')->shouldBeCalled(); $jobInstance->setRawParameters(['filePath' => 'currencies.csv'])->shouldBeCalled(); $jobInstance->setType('type')->shouldBeCalled(); $this->update($jobInstance, ['connector' => 'Data fixtures', 'alias' => 'fixtures_currency_csv', 'label' => 'Currencies data fixtures', 'type' => 'type', 'configuration' => ['filePath' => 'currencies.csv'], 'code' => 'fixtures_currency_csv']); }
/** * To string * @return string */ public function __toString() { $startTime = self::formatDate($this->startTime); $endTime = self::formatDate($this->endTime); $updatedTime = self::formatDate($this->updatedTime); $jobInstanceCode = $this->jobInstance != null ? $this->jobInstance->getCode() : ''; $message = "startTime=%s, endTime=%s, updatedTime=%s, status=%d, exitStatus=%s, exitDescription=[%s], job=[%s]"; return sprintf($message, $startTime, $endTime, $updatedTime, $this->status, $this->exitStatus, $this->exitDescription, $jobInstanceCode); }
function it_normalizes_job_instance(JobInstance $jobinstance) { $jobinstance->getCode()->willReturn('product_export'); $jobinstance->getLabel()->willReturn('Product export'); $jobinstance->getConnector()->willReturn('myconnector'); $jobinstance->getType()->willReturn('EXPORT'); $jobinstance->getRawParameters()->willReturn(['delimiter' => ';']); $this->normalize($jobinstance)->shouldReturn(['code' => 'product_export', 'label' => 'Product export', 'connector' => 'myconnector', 'type' => 'EXPORT', 'configuration' => '{"delimiter":";"}']); }
function it_does_not_support_a_incompatible_job($jobRegistry, ItemWriterInterface $writer, JobExecution $jobExecution, JobInstance $jobInstance, Job $job, ItemStep $step) { $jobInstance->getJobName()->willReturn('my_job_name'); $jobRegistry->get('my_job_name')->willReturn($job); $jobExecution->getJobInstance()->willReturn($jobInstance); $jobExecution->getId()->willReturn(12); $jobInstance->getType()->willReturn('type'); $job->getSteps()->willReturn([$step]); $step->getWriter()->willReturn($writer); $this->supports($jobExecution)->shouldReturn(false); }
function it_provides_a_configured_job(JobInstance $jobInstance, $job) { $rawConfiguration = ['my raw conf']; $jobInstance->getConnector()->willReturn('Data fixtures'); $jobInstance->getType()->willReturn('fixtures'); $jobInstance->getAlias()->willReturn('fixtures_category_csv'); $jobInstance->getRawConfiguration()->willReturn($rawConfiguration); $job->setConfiguration($rawConfiguration)->shouldBeCalled(); $jobInstance->setJob($job)->shouldBeCalled(); $this->getJob($jobInstance)->shouldReturn($job); }
function it_configures_job_instances_with_several_replacement_paths(JobInstance $instance) { $myFilePath = __FILE__; $myInstallerPath = dirname($myFilePath); $myFileName = str_replace($myInstallerPath, '', $myFilePath); $myReplacementFileCommunity = $myFilePath; $myReplacementFileEnterprise = $myFilePath; $replacementPaths = [$myFileName => [$myReplacementFileCommunity, $myReplacementFileEnterprise]]; $instance->getCode()->willReturn('my_original_code'); $instance->getRawParameters()->willReturn(['filePath' => $myFileName]); $instance->setRawParameters(['filePath' => $myReplacementFileCommunity])->shouldBeCalled(); $instance->setCode('my_original_code0')->shouldBeCalled(); $instance->getRawParameters()->willReturn(['filePath' => $myFileName]); $instance->setRawParameters(['filePath' => $myReplacementFileEnterprise])->shouldBeCalled(); $instance->setCode('my_original_code1')->shouldBeCalled(); $configuredInstances = $this->configureJobInstancesWithReplacementPaths([$instance], $replacementPaths); $configuredInstances->shouldHaveCount(2); }
/** * Get a registered job definition from a JobInstance * * @param JobInstance $jobInstance * * @throws \LogicException * @return JobInterface */ public function getJob(JobInstance $jobInstance) { if ($connector = $this->getConnector($jobInstance->getConnector(), $jobInstance->getType())) { if ($job = $this->getConnectorJob($connector, $jobInstance->getAlias())) { $job->setConfiguration($jobInstance->getRawConfiguration()); $jobInstance->setJob($job); return $job; } } return null; }
function it_creates_a_file_if_writer_is_correct($jobRegistry, Writer $writer, JobExecution $jobExecution, JobInstance $jobInstance, Job $job, ItemStep $step, $factory, $filesystem) { $file1 = tempnam(sys_get_temp_dir(), 'spec'); $file2 = tempnam(sys_get_temp_dir(), 'spec'); $jobInstance->getJobName()->willReturn('my_job_name'); $jobRegistry->get('my_job_name')->willReturn($job); $jobExecution->getJobInstance()->willReturn($jobInstance); $jobExecution->getId()->willReturn(12); $jobInstance->getType()->willReturn('type'); $job->getSteps()->willReturn([$step]); $step->getWriter()->willReturn($writer); $writer->getWrittenFiles()->willReturn([$file1 => 'file1', $file2 => 'file2']); $writer->getPath()->willReturn(sys_get_temp_dir()); $filesystem->has('type/my_job_name/12/archive')->willReturn(false); $filesystem->createDir('type/my_job_name/12/archive')->shouldBeCalled(); $factory->createZip(Argument::any())->willReturn($filesystem); $filesystem->put('file1', '')->shouldBeCalled(); $filesystem->put('file2', '')->shouldBeCalled(); $this->archive($jobExecution); unlink($file1); unlink($file2); }
/** * @param TableNode $table * * @Given /^the following jobs?:$/ */ public function theFollowingJobs(TableNode $table) { $registry = $this->getContainer()->get('akeneo_batch.connectors'); foreach ($table->getHash() as $data) { $jobInstance = new JobInstance($data['connector'], $data['type'], $data['alias']); $jobInstance->setCode($data['code']); $jobInstance->setLabel($data['label']); $job = $registry->getJob($jobInstance); $jobInstance->setJob($job); $this->validate($jobInstance); $this->persist($jobInstance); } }
/** * {@inheritdoc} */ protected function normalizeConfiguration(JobInstance $job) { $configuration = json_encode($job->getRawParameters()); return $configuration; }
/** * {@inheritdoc} */ public function getLastJobExecution(JobInstance $jobInstance, $status) { return $this->jobManager->createQueryBuilder()->select('j')->from($this->jobExecutionClass, 'j')->where('j.jobInstance = :job_instance')->andWhere('j.status = :status')->setParameter('job_instance', $jobInstance->getId())->setParameter('status', $status)->orderBy('j.startTime', 'DESC')->setMaxResults(1)->getQuery()->getOneOrNullResult(); }
/** * Configure job instance with uploaded file, returns true if well configured * * @param JobInstance $jobInstance * * @return boolean */ protected function configureWithUploadFile(JobInstance $jobInstance) { $fileInfo = $this->getFileInfo(); if (null === $fileInfo) { return false; } $uploadedFile = $fileInfo->getUploadedFile(); $file = $uploadedFile->move(sys_get_temp_dir(), $uploadedFile->getClientOriginalName()); $rawParameters = $jobInstance->getRawParameters(); $rawParameters['filePath'] = $file->getRealPath(); $jobInstance->setRawParameters($rawParameters); return true; }
/** * @param JobInstance $job * * @Given /^I am on the ("([^"]*)" import job) edit page$/ */ public function iAmOnTheImportJobEditPage(JobInstance $job) { $this->getNavigationContext()->openPage('Import edit', ['id' => $job->getId()]); }
/** * @param TableNode $table * * @Given /^the following jobs?:$/ */ public function theFollowingJobs(TableNode $table) { foreach ($table->getHash() as $data) { $jobInstance = new JobInstance($data['connector'], $data['type'], $data['alias']); $jobInstance->setCode($data['code']); $jobInstance->setLabel($data['label']); $this->getContainer()->get('akeneo_batch.saver.job_instance')->save($jobInstance); } }
/** * @param JobInstance $job * * @When /^I launch the ("([^"]*)" (import|export) job)$/ */ public function iLaunchTheExportJob(JobInstance $job) { $jobType = ucfirst($job->getType()); $this->getNavigationContext()->openPage(sprintf('%s launch', $jobType), ['id' => $job->getId()]); }
/** * @param string $action * @param JobInstance $job * * @return \Behat\Behat\Context\Step\Then * * @When /^I should not be able to (launch|edit) the ("([^"]*)" (export|import) job)$/ */ public function iShouldNotBeAbleToAccessTheJob($action, JobInstance $job) { $this->currentPage = sprintf("%s %s", ucfirst($job->getType()), $action); $page = $this->getCurrentPage()->open(['id' => $job->getId()]); return new Step\Then('I should see "403 Forbidden"'); }
/** * Create a jobExecution * * @param JobInstance $jobInstance * @param UserInterface $user * * @return JobExecution */ protected function createJobExecution(JobInstance $jobInstance, UserInterface $user) { $job = $this->jobRegistry->get($jobInstance->getJobName()); $jobParameters = $this->jobParametersFactory->create($job, $jobInstance->getRawParameters()); $jobExecution = $this->jobRepository->createJobExecution($jobInstance, $jobParameters); $jobExecution->setUser($user->getUsername()); $this->jobRepository->updateJobExecution($jobExecution); return $jobExecution; }
/** * @param JobInstance $job * * @Given /^I should be on the ("([^"]*)" (import|export) job) page$/ */ public function iShouldBeOnTheJobPage(JobInstance $job) { $jobPage = sprintf('%s show', ucfirst($job->getType())); $expectedAddress = $this->getPage($jobPage)->getUrl(['id' => $job->getId()]); $this->assertAddress($expectedAddress); }
function it_sets_job_instance(JobInstance $jobInstance) { $jobInstance->addJobExecution($this)->shouldBeCalled(); $this->setJobInstance($jobInstance); }
/** * Get normalized configuration * * @param JobInstance $job * * @return mixed */ protected function normalizeConfiguration(JobInstance $job) { return $job->getRawConfiguration(); }
/** * Configure job instance for uploaded file * * @param JobInstance $jobInstance * @param File $file * * @return bool */ protected function configureUploadJob(JobInstance $jobInstance, File $file) { $success = false; $job = $jobInstance->getJob(); foreach ($job->getSteps() as $step) { if (method_exists($step, 'getReader')) { $reader = $step->getReader(); if ($reader instanceof UploadedFileAwareInterface) { $constraints = $reader->getUploadedFileConstraints(); $this->fileError = $this->getValidator()->validate($file, $constraints); if ($this->fileError->count() !== 0) { foreach ($this->fileError as $error) { $this->addFlash('error', $error->getMessage()); } return false; } else { $reader->setUploadedFile($file); $success = true; } } } } return $success; }