/** * @test * @covers Cocur\Plum\Workflow::process() * @covers Cocur\Plum\Workflow::processItem() * @covers Cocur\Plum\Workflow::prepareWriters() * @covers Cocur\Plum\Workflow::finishWriters() */ public function processShouldApplyMultipleWritersToReadItems() { $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); $writer1 = $this->getMockWriter(); $writer1->shouldReceive('prepare')->once(); $writer1->shouldReceive('finish')->once(); $writer1->shouldReceive('writeItem')->with('foobar')->once(); $this->workflow->addWriter($writer1); $writer2 = $this->getMockWriter(); $writer2->shouldReceive('prepare')->once(); $writer2->shouldReceive('finish')->once(); $writer2->shouldReceive('writeItem')->with('foobar')->once(); $this->workflow->addWriter($writer2); $result = $this->workflow->process($reader); $this->assertEquals(1, $result->getReadCount()); $this->assertEquals(2, $result->getWriteCount()); $this->assertEquals(1, $result->getItemWriteCount()); }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Cocur\Plum\Writer\ArrayWriter; use Cocur\Plum\Workflow; use Cocur\Plum\Reader\ArrayReader; use Cocur\Plum\Converter\CallbackConverter; use Cocur\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());
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Cocur\Plum\Reader\ArrayReader; use Cocur\Plum\Writer\ArrayWriter; use Cocur\Plum\Workflow; use Cocur\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());