Created
August 16, 2014 22:41
-
-
Save jm42/c7a40fde47573c470a1c to your computer and use it in GitHub Desktop.
Import Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Froebel\Import; | |
use Froebel\Engine; | |
use Froebel\Import\Reader\Reader; | |
class Workflow | |
{ | |
public $engine; | |
public $readers; | |
public function __construct(Engine $engine, $readers) | |
{ | |
$this->engine = $engine; | |
$this->readers = $readers; | |
} | |
public function process() | |
{ | |
foreach ($this->readers as $reader) { | |
foreach ($reader->read() as $item) { | |
$this->engine->index($item); | |
} | |
} | |
} | |
} | |
namespace Froebel\Import\Reader; | |
interface Reader extends \IteratorAggregate | |
{ | |
public function read(); | |
} | |
class ArrayReader implements Reader | |
{ | |
protected $data; | |
public function __construct(array $data = array()) | |
{ | |
$this->data = $data; | |
} | |
public function getIterator() | |
{ | |
return new \ArrayIterator($this->data); | |
} | |
public function read() | |
{ | |
foreach ($this->data as $item) { | |
yield $item; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Froebel\Engine; | |
use Froebel\Hash\ObjectHash; | |
use Froebel\Storage\MemoryStorage; | |
use Froebel\Import\Workflow; | |
use Froebel\Import\Reader\ArrayReader; | |
class WorkflowTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testProcess() | |
{ | |
$data = new \stdClass(); | |
$data->answer = 42; | |
$hash = spl_object_hash($data); | |
$storage = new MemoryStorage(); | |
$engine = new Engine(array(new ObjectHash('a')), $storage); | |
$workflow = new Workflow($engine, array(new ArrayReader(array($data)))); | |
$workflow->process(); | |
$this->assertSame(array($data), $storage->get('a', $hash)); | |
} | |
} | |
class ArrayReaderTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testGetIterator() | |
{ | |
$reader = new ArrayReader(array(1, 2, 3)); | |
$iterator = $reader->getIterator(); | |
$this->assertInstanceOf('\ArrayIterator', $iterator); | |
} | |
public function testReadIterator() | |
{ | |
$reader = new ArrayReader(array(1, 2, 3)); | |
$iterator = $reader->read(); | |
$this->assertInstanceOf('\Iterator', $iterator); | |
} | |
public function testReadCount() | |
{ | |
$reader = new ArrayReader(array(1, 2, 3)); | |
$iterator = $reader->read(); | |
$this->assertCount(3, $iterator); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment