Skip to content

Commit

Permalink
Change CsvWriter to extends AbstractStreamWriter
Browse files Browse the repository at this point in the history
Signed-off-by: Benoît Burnichon <bburnichon@gmail.com>
  • Loading branch information
bburnichon committed Jul 6, 2014
1 parent 3d47922 commit 141e36f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
27 changes: 8 additions & 19 deletions src/Ddeboer/DataImport/Writer/CsvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@

/**
* Writes to a CSV file
*
*/
class CsvWriter extends AbstractWriter
class CsvWriter extends AbstractStreamWriter
{
private $delimiter = ';';
private $enclosure = '"';

/**
* Constructor
*
* @param \SplFileObject $file CSV file
* @param string $mode See http://php.net/manual/en/function.fopen.php
* @param string $delimiter The delimiter
* @param string $enclosure The enclosure
* @param string $delimiter The delimiter
* @param string $enclosure The enclosure
* @param resource $stream
*/
public function __construct(\SplFileObject $file, $mode = 'w', $delimiter = ';', $enclosure = '"')
public function __construct($delimiter = ';', $enclosure = '"', $stream = null)
{
$this->fp = fopen($file->getPathname(), $mode);
parent::__construct($stream);

$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
}
Expand All @@ -31,17 +30,7 @@ public function __construct(\SplFileObject $file, $mode = 'w', $delimiter = ';',
*/
public function writeItem(array $item)
{
fputcsv($this->fp, $item, $this->delimiter, $this->enclosure);

return $this;
}

/**
* {@inheritdoc}
*/
public function finish()
{
fclose($this->fp);
fputcsv($this->getStream(), $item, $this->delimiter, $this->enclosure);

return $this;
}
Expand Down
13 changes: 5 additions & 8 deletions tests/Ddeboer/DataImport/Tests/Writer/CsvWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use Ddeboer\DataImport\Writer\CsvWriter;

class CsvWriterTest extends \PHPUnit_Framework_TestCase
class CsvWriterTest extends StreamWriterTest
{
public function testWriteItem()
{
$outputFile = new \SplFileObject(tempnam('/tmp', null));
$writer = new CsvWriter($outputFile);
$writer = new CsvWriter(';', '"', $this->getStream());

$writer->writeItem(array('first', 'last'));

Expand All @@ -25,19 +24,17 @@ public function testWriteItem()
)
);

$fileContents = file_get_contents($outputFile->getPathname());
$this->assertEquals(
$this->assertContentsEquals(
"first;last\nJames;Bond\n;\"Dr. No\"\n",
$fileContents
$writer
);

$writer->finish();
}

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

$this->assertSame($writer, $writer->prepare());
$this->assertSame($writer, $writer->writeItem(array('foo' => 'bar', 'bar' => 'foo')));
Expand Down
16 changes: 16 additions & 0 deletions tests/Ddeboer/DataImport/Tests/Writer/StreamWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Ddeboer\DataImport\Tests\Writer;

use Ddeboer\DataImport\Writer\AbstractStreamWriter;
use PHPUnit_Framework_Assert;
use PHPUnit_Framework_Constraint_IsEqual;

abstract class StreamWriterTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -27,4 +29,18 @@ protected function getStream()

return $this->stream;
}

/**
* @param string $expected
* @param AbstractStreamWriter $actual
* @param string $message
*/
public static function assertContentsEquals($expected, $actual, $message = '')
{
$stream = $actual->getStream();
rewind($stream);
$actual = stream_get_contents($stream);

self::assertEquals($expected, $actual, $message);
}
}

0 comments on commit 141e36f

Please sign in to comment.