123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- <?php
- namespace Ddeboer\DataImport\Tests;
- use Ddeboer\DataImport\Exception\WriterException;
- use Ddeboer\DataImport\Reader\ArrayReader;
- use Ddeboer\DataImport\Step\ConverterStep;
- use Ddeboer\DataImport\Step\FilterStep;
- use Ddeboer\DataImport\Step\MappingStep;
- use Ddeboer\DataImport\Step\ValueConverterStep;
- use Ddeboer\DataImport\Writer\ArrayWriter;
- use Ddeboer\DataImport\Workflow\StepAggregator;
- use Ddeboer\DataImport\Filter\CallbackFilter;
- use Ddeboer\DataImport\ValueConverter\CallbackValueConverter;
- use Ddeboer\DataImport\ItemConverter\CallbackItemConverter;
- use Ddeboer\DataImport\Writer\CallbackWriter;
- use Ddeboer\DataImport\Exception\SourceNotFoundException;
- class WorkflowTest extends \PHPUnit_Framework_TestCase
- {
- public function testAddStep()
- {
- $step = $this->getMock('Ddeboer\DataImport\Step');
- $this->getWorkflow()->addStep($step);
- }
- public function testAddCallbackWriter()
- {
- $this->getWorkflow()->addWriter(new CallbackWriter(function($item) {
- // var_dump($item);
- }));
- }
- public function testWriterIsPreparedAndFinished()
- {
- $writer = $this->getMockBuilder('\Ddeboer\DataImport\Writer\CallbackWriter')
- ->disableOriginalConstructor()
- ->getMock();
- $writer->expects($this->once())
- ->method('prepare');
- $writer->expects($this->once())
- ->method('finish');
- $this->getWorkflow()->addWriter($writer)
- ->process();
- }
- public function testWorkflowWithObjects()
- {
- $reader = new ArrayReader(array(
- new Dummy('foo'),
- new Dummy('bar'),
- new Dummy('foobar'),
- ));
- $data = array();
- $writer = new ArrayWriter($data);
- $workflow = new StepAggregator($reader);
- $workflow->addWriter($writer);
- $converterStep = new ConverterStep([
- function($item) { return array('name' => $item->name); }
- ]);
- $valueStep = new ValueConverterStep();
- $valueStep->add('[name]', function($name) { return strrev($name); });
- $workflow->addStep($converterStep)->addStep($valueStep);
- $workflow->process();
- $this->assertEquals(array(
- array('name' => 'oof'),
- array('name' => 'rab'),
- array('name' => 'raboof')
- ), $data);
- }
- /**
- * @expectedException \Ddeboer\DataImport\Exception\UnexpectedTypeException
- */
- public function testItemConverterWhichReturnObjects()
- {
- $reader = new ArrayReader(array(
- new Dummy('foo'),
- new Dummy('bar'),
- new Dummy('foobar'),
- ));
- $data = array();
- $writer = new ArrayWriter($data);
- $workflow = new StepAggregator($reader);
- $workflow->addWriter($writer);
- $converterStep = new ConverterStep();
- $converterStep->add(function($item) { return $item; });
- $workflow->addStep($converterStep)->process();
- }
- /**
- * @expectedException \Ddeboer\DataImport\Exception\UnexpectedTypeException
- */
- public function testItemConverterWithObjectsAndNoItemConverters()
- {
- $reader = new ArrayReader(array(
- new Dummy('foo'),
- new Dummy('bar'),
- new Dummy('foobar'),
- ));
- $data = array();
- $writer = new ArrayWriter($data);
- $workflow = new StepAggregator($reader);
- $workflow->addWriter($writer);
- $workflow->process();
- }
- public function testFilterPriority()
- {
- $offsetFilter = $this->getMockBuilder('\Ddeboer\DataImport\Filter\OffsetFilter')
- ->disableOriginalConstructor()
- ->setMethods(array('__invoke'))
- ->getMock();
- $offsetFilter->expects($this->never())->method('filter');
- $validatorFilter = $this->getMockBuilder('\Ddeboer\DataImport\Filter\ValidatorFilter')
- ->disableOriginalConstructor()
- ->setMethods(array('__invoke'))
- ->getMock();
- $validatorFilter->expects($this->exactly(3))
- ->method('__invoke')
- ->will($this->returnValue(false));
- $filterStep = (new FilterStep())
- ->add($offsetFilter)
- ->add($validatorFilter);
- $this->getWorkflow()
- ->addStep($filterStep)
- ->process();
- }
- public function testFilterPriorityOverride()
- {
- $offsetFilter = $this->getMockBuilder('\Ddeboer\DataImport\Filter\OffsetFilter')
- ->disableOriginalConstructor()
- ->setMethods(array('__invoke'))
- ->getMock();
- $offsetFilter->expects($this->exactly(3))
- ->method('__invoke')
- ->will($this->returnValue(false));
- $validatorFilter = $this->getMockBuilder('\Ddeboer\DataImport\Filter\ValidatorFilter')
- ->disableOriginalConstructor()
- ->setMethods(array('__invoke'))
- ->getMock();
- $validatorFilter->expects($this->never())->method('filter');
- $filterStep = (new FilterStep())
- ->add($offsetFilter, 257)
- ->add($validatorFilter);
- $this->getWorkflow()
- ->addStep($filterStep)
- ->process();
- }
- public function testExceptionInterfaceThrownFromWriterIsCaught()
- {
- $originalData = array(array('foo' => 'bar'));
- $reader = new ArrayReader($originalData);
- $array = array();
- $writer = $this->getMock('Ddeboer\DataImport\Writer\ArrayWriter', array(), array(&$array));
- $exception = new SourceNotFoundException("Log me!");
- $writer->expects($this->once())
- ->method('writeItem')
- ->with($originalData[0])
- ->will($this->throwException($exception));
- $logger = $this->getMock('Psr\Log\LoggerInterface');
- $logger->expects($this->once())
- ->method('error')
- ->with($exception->getMessage());
- $workflow = new StepAggregator($reader);
- $workflow->setLogger($logger);
- $workflow->setSkipItemOnFailure(true);
- $workflow->addWriter($writer);
- $workflow->process();
- }
- public function testWorkflowResultWhenAllSuccessful()
- {
- $workflow = $this->getWorkflow();
- $result = $workflow->process();
- $this->assertInstanceOf('Ddeboer\DataImport\Result', $result);
- $this->assertInstanceOf('DateTime', $result->getStartTime());
- $this->assertInstanceOf('DateTime', $result->getEndTime());
- $this->assertInstanceOf('DateInterval', $result->getElapsed());
- $this->assertInstanceOf('Ddeboer\DataImport\Result', $result);
- $this->assertSame(3, $result->getTotalProcessedCount());
- $this->assertSame(3, $result->getSuccessCount());
- $this->assertSame(0, $result->getErrorCount());
- $this->assertFalse($result->hasErrors());
- $this->assertEmpty($result->getExceptions());
- $this->assertSame(null, $result->getName());
- }
- public function testMultipleMappingsForAnItemAfterAnotherItemConverterwasAdded()
- {
- $originalData = array(array('foo' => 'bar', 'baz' => 'value'));
- $outputTestData = array();
- $writer = new ArrayWriter($outputTestData);
- $reader = new ArrayReader($originalData);
- $workflow = new StepAggregator($reader);
- $converterStep = new ConverterStep();
- // add a dummy item converter
- $converterStep->add(function($item) { return $item; });
- $mappingStep = (new MappingStep())
- ->map('[foo]', '[bar]')
- ->map('[baz]', '[bazzoo]');
- // add multiple mappings
- $workflow
- ->addStep($converterStep)
- ->addStep($mappingStep)
- ->addWriter($writer)
- ->process()
- ;
- $this->assertArrayHasKey('bar', $outputTestData[0]);
- $this->assertArrayHasKey('bazzoo', $outputTestData[0]);
- }
- public function _testWorkflowResultWithExceptionThrowFromWriter()
- {
- $workflow = $this->getWorkflow();
- $workflow->setSkipItemOnFailure(true);
- $writer = $this->getMock('Ddeboer\DataImport\Writer\WriterInterface');
- $e = new WriterException();
- $writer
- ->expects($this->at(1))
- ->method('writeItem')
- ->with(array('first' => 'James', 'last' => 'Bond'))
- ->will($this->throwException($e));
- $workflow->addWriter($writer);
- $result = $workflow->process();
- $this->assertInstanceOf('Ddeboer\DataImport\Result', $result);
- $this->assertInstanceOf('DateTime', $result->getStartTime());
- $this->assertInstanceOf('DateTime', $result->getEndTime());
- $this->assertInstanceOf('DateInterval', $result->getElapsed());
- $this->assertInstanceOf('Ddeboer\DataImport\Result', $result);
- $this->assertSame(3, $result->getTotalProcessedCount());
- $this->assertSame(2, $result->getSuccessCount());
- $this->assertSame(1, $result->getErrorCount());
- $this->assertTrue($result->hasErrors());
- $this->assertSame(array($e), iterator_to_array($result->getExceptions()));
- $this->assertSame(null, $result->getName());
- }
- protected function getWorkflow()
- {
- $reader = new ArrayReader(array(
- array(
- 'first' => 'James',
- 'last' => 'Bond'
- ),
- array(
- 'first' => 'Miss',
- 'last' => 'Moneypenny'
- ),
- array(
- 'first' => null,
- 'last' => 'Doe'
- )
- ));
- return new StepAggregator($reader);
- }
- }
- class Dummy
- {
- public $name;
- public function __construct($name)
- {
- $this->name = $name;
- }
- }
|