/** * Simple test of download method */ public function testDownload() { $dataset = 'laposte_hexasmal'; $format = 'json'; $filter = '34000'; $updateExisting = false; $content = uniqid(); $path = uniqid(); $client = $this->getClientMock(); $client->expects($this->once())->method('setTimeout')->with(0); $client->expects($this->once())->method('get')->with('download', array('dataset' => $dataset, 'format' => $format, 'q' => $filter))->willReturn($content); $finder = $this->getFinderMock(); $finder->expects($this->once())->method('save')->with($dataset, $content, $format, $filter, $updateExisting)->willReturn($path); $finder->expects($this->once())->method('getContent')->with($path)->willReturn($content); $downloader = new Downloader($client, $finder); $result = $downloader->download($dataset, $format, $filter, $updateExisting); $this->assertEquals($content, $result); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $this->downloader = $this->getContainer()->get('data_nova.service.downloader'); $dataset = $input->getArgument('dataset'); $format = strtolower($input->getArgument('format')); $query = $input->getArgument('q'); $download = $this->downloader->download($dataset, $format, $input->getArgument('q'), $input->getOption('force-replace')); $filepath = $this->downloader->findDownload($dataset, $format, $query); if ($download) { $output->writeln(sprintf('Dataset %s downloaded to "%s" : %d bytes', $dataset, $filepath, filesize($filepath))); } else { if (false !== $filepath) { if (false === $input->getOption('force-replace')) { $output->writeln('Existing dataset. To overwrite it, try with --force-replace option'); } else { $output->writeln('Error during update of existing dataset.'); } } else { $output->writeln('Error during dataset download.'); } } }
/** * @param Download $download * @param bool $forceUpdate * * @return false|string */ public function download(Download $download, $forceUpdate = false) { return $this->downloader->download($download->getDataset(), $download->getFormat(), $download->getFilter(), $forceUpdate); }