/** * @test */ public function testWorkflow() { vfsStream::setup('fixtures', null, ['foo.csv' => 'Test,Data,Column 3' . "\n" . '1,Something,Column 3' . "\n" . '2,Another Thing,Column 3' . "\n"]); $reader = new CsvReader(vfsStream::url('fixtures/foo.csv')); $converter = new HeaderConverter(); $writer = new CsvWriter(vfsStream::url('fixtures/bar.csv')); $workflow = new Workflow(); $workflow->addConverter($converter)->addWriter($writer); $result = $workflow->process($reader); $this->assertSame(0, $result->getErrorCount()); $this->assertSame(3, $result->getItemWriteCount()); $this->assertSame(3, $result->getReadCount()); $this->assertSame(3, $result->getWriteCount()); }
/** * @test */ public function testWorkflow() { $reader = new ExcelReader(PHPExcel_IOFactory::load(__DIR__ . '/fixtures/test.xlsx')); $writer = new ArrayWriter(); $workflow = new Workflow(); $workflow->addConverter(new HeaderConverter())->addFilter(new SkipFirstFilter(1))->addWriter($writer); $result = $workflow->process($reader); $this->assertSame(0, $result->getErrorCount()); $this->assertSame(4, $result->getReadCount()); $this->assertSame(3, $result->getItemWriteCount()); $this->assertSame('line 1A', $writer->getData()[0]['col A']); $this->assertSame('line 2B', $writer->getData()[1]['col B']); $this->assertSame('line 3C', $writer->getData()[2]['col C']); }
/** * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { $inputFile = $input->getArgument('input'); $outputFile = $input->getArgument('output'); $inputFormat = substr($inputFile, strrpos($inputFile, '.') + 1); $outputFormat = $outputFile ? substr($outputFile, strrpos($outputFile, '.') + 1) : null; $output->writeln(sprintf('Convert <info>%s</info> into <info>%s</info>', $inputFormat, $outputFormat)); $workflow = new Workflow(); if ($inputFormat === 'csv') { $reader = new CsvReader($inputFile); $outputFormat = $outputFormat ? $outputFormat : 'xlsx'; $outputFile = $outputFile ? $outputFile : str_replace('.csv', '.xlsx', $inputFile); } else { if ($inputFormat === 'xlsx' || $inputFormat === 'xls') { $reader = new ExcelReader(PHPExcel_IOFactory::load($inputFile)); $outputFormat = $outputFormat ? $outputFormat : 'csv'; $outputFile = $outputFile ? $outputFile : str_replace(['.xlsx', '.xls'], '.csv', $inputFile); } else { $output->writeln(sprintf('<error>Invalid input file format %s.</error>', $inputFormat)); return; } } $workflow->addConverter(new HeaderConverter()); $workflow->addFilter(new SkipFirstFilter(1)); if ($outputFormat === 'csv') { $writer = new CsvWriter($outputFile); $writer->autoDetectHeader(); } else { if ($outputFormat === 'xlsx' || $outputFormat == 'xls') { $writer = new ExcelWriter($outputFile); $writer->autoDetectHeader(); } else { $output->writeln(sprintf('<error>Invalid output file format: %s</error>', $outputFormat)); return; } } $workflow->addConverter(new NullConverter()); $workflow->addWriter($writer); $workflow->addWriter(new ConsoleProgressWriter(new ProgressBar($output, $reader->count()))); $result = $workflow->process($reader); $output->writeln(''); $output->writeln(sprintf('Read rows: %d', $result->getReadCount())); $output->writeln(sprintf('Written rows: %d', $result->getItemWriteCount())); $output->writeln(sprintf('Error rows: %d', $result->getErrorCount())); if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { foreach ($result->getExceptions() as $exception) { $output->writeln(sprintf('- <error>%s</error>', $exception->getMessage())); if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) { $output->writeln(sprintf('<error>%s</error>', $exception->getTraceAsString())); } } } }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Plum\Plum\Writer\ArrayWriter; use Plum\Plum\Workflow; use Plum\Plum\Reader\ArrayReader; use Plum\Plum\Converter\CallbackConverter; use Plum\Plum\Filter\CallbackFilter; $reader = new ArrayReader(['foobar', 'qoobar', 'bazbaz']); $writer1 = new ArrayWriter(); $writer2 = new ArrayWriter(); $workflow = new Workflow(); $workflow->addConverter(new CallbackConverter(function ($item) { return strtoupper($item); })); $workflow->addWriter($writer1); $workflow->addFilter(new CallbackFilter(function ($item) { return $item != 'BAZBAZ'; })); $workflow->addWriter($writer2); $workflow->process($reader); print_r($writer1->getData()); print_r($writer2->getData());
/** * @test * @covers Plum\Plum\Workflow::process() * @covers Plum\Plum\Workflow::processReader() * @covers Plum\Plum\Workflow::processItem() */ public function processShouldCollectExceptionsIfResumeOnErrorOptionIsTrue() { $iterator = m::mock('\\Iterator'); $iterator->shouldReceive('rewind'); $iterator->shouldReceive('valid')->andReturn(true)->once(); $iterator->shouldReceive('current')->andReturn('foobar'); $iterator->shouldReceive('next'); $iterator->shouldReceive('valid')->andReturn(false)->once(); $reader = $this->getMockReader(); $reader->shouldReceive('getIterator')->andReturn($iterator); $exception = new Exception(); $converter = $this->getMockConverter(); $converter->shouldReceive('convert')->with('foobar')->once()->andThrow($exception); $workflow = new Workflow(['resumeOnError' => true]); $workflow->addConverter($converter); $result = $workflow->process($reader); $this->assertEquals(1, $result->getReadCount()); $this->assertEquals(1, $result->getErrorCount()); $this->assertContains($exception, $result->getExceptions()); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Plum\Plum\Reader\ArrayReader; use Plum\Plum\Writer\ArrayWriter; use Plum\Plum\Workflow; use Plum\Plum\WorkflowConcatenator; $reader = new ArrayReader(['foo', 'bar', 'baz', 'qoo']); $writer = new ArrayWriter(); $concatenator = new WorkflowConcatenator(); $workflow1 = new Workflow(); $workflow1->addWriter($concatenator); $workflow1->process($reader); $workflow2 = new Workflow(); $workflow2->addWriter($writer); $workflow2->process($concatenator); print_r($writer->getData());