Skip to content

Commit

Permalink
Merge pull request #88 from bburnichon/ticket_87_writer_fluent_interface
Browse files Browse the repository at this point in the history
Add proper writer fluent interface tests
  • Loading branch information
ddeboer committed Jul 7, 2014
2 parents 1f58609 + f49fa9d commit 31c799f
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/Ddeboer/DataImport/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,23 @@ abstract class AbstractWriter implements WriterInterface
* This template method can be overridden in concrete writer
* implementations.
*
* @return WriterInterface
* @return $this
*/
public function prepare() {}
public function prepare()
{
return $this;
}

/**
* Wrap up the writer after all items have been written
*
* This template method can be overridden in concrete writer
* implementations.
*
* @return WriterInterface
* @return $this
*/
public function finish() {}
public function finish()
{
return $this;
}
}
6 changes: 6 additions & 0 deletions src/Ddeboer/DataImport/Writer/ConsoleProgressWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,22 @@ public function prepare()
$this->progress = new ProgressBar($this->output, $this->reader->count());
$this->progress->setFormat($this->verbosity);
$this->progress->start();

return $this;
}

public function writeItem(array $item)
{
$this->progress->advance();

return $this;
}

public function finish()
{
$this->progress->finish();

return $this;
}

public function getVerbosity()
Expand Down
2 changes: 2 additions & 0 deletions src/Ddeboer/DataImport/Writer/CsvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ public function writeItem(array $item)
public function finish()
{
fclose($this->fp);

return $this;
}
}
2 changes: 2 additions & 0 deletions src/Ddeboer/DataImport/Writer/ExcelWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,7 @@ public function finish()
{
$writer = \PHPExcel_IOFactory::createWriter($this->excel, $this->type);
$writer->save($this->filename);

return $this;
}
}
4 changes: 4 additions & 0 deletions src/Ddeboer/DataImport/Writer/PdoWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function __construct(\PDO $pdo, $tableName)
*/
public function prepare()
{
return $this;
}

/**
Expand Down Expand Up @@ -80,12 +81,15 @@ public function writeItem(array $item)
//convert exception so the abstracton doesn't leak
throw new WriterException('Write failed ('.$e->getMessage().').', null, $e);
}

return $this;
}

/**
* {@inheritDoc}
*/
public function finish()
{
return $this;
}
}
6 changes: 3 additions & 3 deletions src/Ddeboer/DataImport/Writer/WriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface WriterInterface
/**
* Prepare the writer before writing the items
*
* @return WriterInterface
* @return $this
*/
public function prepare();

Expand All @@ -21,14 +21,14 @@ public function prepare();
*
* @param array $item The data item with converted values
*
* @return WriterInterface
* @return $this
*/
public function writeItem(array $item);

/**
* Wrap up the writer after all items have been written
*
* @return WriterInterface
* @return $this
*/
public function finish();
}
13 changes: 13 additions & 0 deletions tests/Ddeboer/DataImport/Tests/Writer/CallbackWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,17 @@ public function testFinish()
$writer = new CallbackWriter($callable);
$this->assertEquals($writer, $writer->finish());
}

public function testFluentInterface()
{
$callable = function(array $item) {
return '';
};

$writer = new CallbackWriter($callable);

$this->assertSame($writer, $writer->prepare());
$this->assertSame($writer, $writer->writeItem(array('foo' => 'bar', 'bar' => 'foo')));
$this->assertSame($writer, $writer->finish());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Ddeboer\DataImport\Writer\ConsoleProgressWriter;
use Ddeboer\DataImport\Workflow;
use Ddeboer\DataImport\Reader\ArrayReader;
use Symfony\Component\Console\Output\NullOutput;

class ConsoleProgressWriterTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -43,4 +44,25 @@ public function testWrite()

$this->assertEquals('debug', $writer->getVerbosity());
}

public function testFluentInterface()
{
$data = array(
array(
'first' => 'The first',
'second' => 'Second property'
), array(
'first' => 'Another first',
'second' => 'Last second'
)
);
$reader = new ArrayReader($data);
$output = new NullOutput();

$writer = new ConsoleProgressWriter($output, $reader);

$this->assertSame($writer, $writer->prepare());
$this->assertSame($writer, $writer->writeItem(array('foo' => 'bar', 'bar' => 'foo')));
$this->assertSame($writer, $writer->finish());
}
}
10 changes: 10 additions & 0 deletions tests/Ddeboer/DataImport/Tests/Writer/CsvWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ public function testWriteItem()

$writer->finish();
}

public function testFluentInterface()
{
$outputFile = new \SplFileObject(tempnam('/tmp', null));
$writer = new CsvWriter($outputFile);

$this->assertSame($writer, $writer->prepare());
$this->assertSame($writer, $writer->writeItem(array('foo' => 'bar', 'bar' => 'foo')));
$this->assertSame($writer, $writer->finish());
}
}
16 changes: 16 additions & 0 deletions tests/Ddeboer/DataImport/Tests/Writer/DoctrineWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,20 @@ protected function getEntityManager()

return $em;
}

public function testFluentInterface()
{
$writer = new DoctrineWriter($this->getEntityManager(), 'DdeboerDataImport:TestEntity');

$association = new TestEntity();
$item = array(
'firstProperty' => 'some value',
'secondProperty' => 'some other value',
'firstAssociation'=> $association
);

$this->assertSame($writer, $writer->prepare());
$this->assertSame($writer, $writer->writeItem($item));
$this->assertSame($writer, $writer->finish());
}
}
12 changes: 11 additions & 1 deletion tests/Ddeboer/DataImport/Tests/Writer/ExcelWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ public function testWriteItemWithoutSheetTitle()
->writeItem(array('first', 'last'))
->finish();
}
}

public function testFluentInterface()
{
$outputFile = new \SplFileObject(tempnam(sys_get_temp_dir(), null));
$writer = new ExcelWriter($outputFile);

$this->assertSame($writer, $writer->prepare());
$this->assertSame($writer, $writer->writeItem(array('first', 'last')));
$this->assertSame($writer, $writer->finish());
}
}
10 changes: 10 additions & 0 deletions tests/Ddeboer/DataImport/Tests/Writer/PdoWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,14 @@ public function testWriteFailureWithNoException()
$writer->writeItem(array('foo', 'bar', 'baz'));
$writer->finish();
}

public function testFluentInterface()
{
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
$writer = new PdoWriter($this->pdo, 'example');

$this->assertSame($writer, $writer->prepare());
$this->assertSame($writer, $writer->writeItem(array('foo', 'bar')));
$this->assertSame($writer, $writer->finish());
}
}

0 comments on commit 31c799f

Please sign in to comment.