From 79214aa1c99310434ae757bf3e8c9f507d34bf19 Mon Sep 17 00:00:00 2001 From: Florian Eckerstorfer Date: Wed, 28 Oct 2015 21:05:30 +0100 Subject: [PATCH 1/2] Allow injection of League\Csv\Writer in CsvWriter --- src/CsvWriter.php | 15 +++++++++++---- tests/CsvWriterTest.php | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/CsvWriter.php b/src/CsvWriter.php index 80c79b8..0bf1164 100644 --- a/src/CsvWriter.php +++ b/src/CsvWriter.php @@ -14,6 +14,7 @@ use League\Csv\Writer; use LogicException; use Plum\Plum\Writer\WriterInterface; +use SplFileObject; /** * CsvWriter. @@ -48,7 +49,11 @@ class CsvWriter implements WriterInterface */ public function __construct($filename, $delimiter = ',', $enclosure = '"') { - $this->filename = $filename; + if ($filename instanceof Writer) { + $this->csv = $filename; + } else { + $this->filename = $filename; + } $this->delimiter = $delimiter; $this->enclosure = $enclosure; } @@ -101,9 +106,11 @@ public function writeItem($item) */ public function prepare() { - $this->csv = Writer::createFromFileObject(new \SplFileObject($this->filename, 'w')); - $this->csv->setDelimiter($this->delimiter); - $this->csv->setEnclosure($this->enclosure); + if (!$this->csv) { + $this->csv = Writer::createFromFileObject(new SplFileObject($this->filename, 'w')); + $this->csv->setDelimiter($this->delimiter); + $this->csv->setEnclosure($this->enclosure); + } if ($this->header !== null) { $this->writeItem($this->header); diff --git a/tests/CsvWriterTest.php b/tests/CsvWriterTest.php index 5e20253..8c5fdf7 100644 --- a/tests/CsvWriterTest.php +++ b/tests/CsvWriterTest.php @@ -11,7 +11,9 @@ namespace Plum\PlumCsv; +use League\Csv\Writer; use org\bovigo\vfs\vfsStream; +use SplFileObject; /** * CsvWriterTest. @@ -156,4 +158,23 @@ public function finishThrowsAnExceptionIfNoFileHandleExists() $writer = new CsvWriter(vfsStream::url('fixtures/foo.csv')); $writer->finish(); } + + /** + * @test + * @covers Plum\PlumCsv\CsvWriter::__construct() + * @covers Plum\PlumCsv\CsvWriter::writeItem() + * @covers Plum\PlumCsv\CsvWriter::prepare() + * @covers Plum\PlumCsv\CsvWriter::finish() + * @covers Plum\PlumCsv\CsvWriter::verifyHandle() + */ + public function writeItemWritesItemIntoFileInjectingWriter() + { + $csv = Writer::createFromFileObject(new SplFileObject(vfsStream::url('fixtures/foo.csv'), 'w')); + $writer = new CsvWriter($csv); + $writer->prepare(); + $writer->writeItem(['col 1', 'col 2', 'col 3']); + $writer->finish(); + + $this->assertEquals("\"col 1\",\"col 2\",\"col 3\"\n", file_get_contents(vfsStream::url('fixtures/foo.csv'))); + } } From 4912736893094c6f20ec9b1ab85143b58df09012 Mon Sep 17 00:00:00 2001 From: Florian Eckerstorfer Date: Wed, 28 Oct 2015 21:08:54 +0100 Subject: [PATCH 2/2] Fix code style --- tests/CsvWriterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CsvWriterTest.php b/tests/CsvWriterTest.php index 8c5fdf7..d43db11 100644 --- a/tests/CsvWriterTest.php +++ b/tests/CsvWriterTest.php @@ -169,7 +169,7 @@ public function finishThrowsAnExceptionIfNoFileHandleExists() */ public function writeItemWritesItemIntoFileInjectingWriter() { - $csv = Writer::createFromFileObject(new SplFileObject(vfsStream::url('fixtures/foo.csv'), 'w')); + $csv = Writer::createFromFileObject(new SplFileObject(vfsStream::url('fixtures/foo.csv'), 'w')); $writer = new CsvWriter($csv); $writer->prepare(); $writer->writeItem(['col 1', 'col 2', 'col 3']);