Skip to content

Commit

Permalink
Add An Abstract StreamWriter
Browse files Browse the repository at this point in the history
Also add A utility test class StreamWriterTest for easier test stream
creation.

Signed-off-by: Benoît Burnichon <bburnichon@gmail.com>
  • Loading branch information
bburnichon committed Jul 5, 2014
1 parent 1f58609 commit 054e0e0
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
117 changes: 117 additions & 0 deletions src/Ddeboer/DataImport/Writer/AbstractStreamWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Ddeboer\DataImport\Writer;

abstract class AbstractStreamWriter implements WriterInterface
{
private $stream;
private $closeStreamOnFinish = true;

/** @var string */
private $eol = "\n";

/**
* Constructor
*
* @param resource $stream
*/
public function __construct($stream = null)
{
if (null !== $stream) {
$this->setStream($stream);
}
}

/**
* Set Stream Resource
*
* @param $stream
* @throws \InvalidArgumentException
* @return $this
*/
public function setStream($stream)
{
if (! is_resource($stream) || ! 'stream' == get_resource_type($stream)) {
throw new \InvalidArgumentException(sprintf(
'Expects argument to be a stream resource, got %s',
is_resource($stream) ? get_resource_type($stream) : gettype($stream)
));
}

$this->stream = $stream;

return $this;
}

/**
* Get underlying stream resource
*
* @return resource
*/
public function getStream()
{
if (null === $this->stream) {
$this->setStream(fopen('php://temp', 'r+'));
}

return $this->stream;
}

/**
* Set End Of Line string
* @param string $eol
* @return $this
*/
public function setEol($eol)
{
$this->eol = (string) $eol;

return $this;
}

/**
* Get End Of Line string
*
* @return string
*/
public function getEol()
{
return $this->eol;
}

/**
* @inheritdoc
*/
public function prepare()
{
return $this;
}

/**
* @inheritdoc
*/
public function finish()
{
if (is_resource($this->stream) && $this->closeStreamOnFinish()) {
fclose($this->stream);
}

return $this;
}

/**
* Should underlying stream be closed on finish?
*
* @param null|bool $closeStreamOnFinish
*
* @return bool
*/
public function closeStreamOnFinish($closeStreamOnFinish = null)
{
if (null !== $closeStreamOnFinish) {
$this->closeStreamOnFinish = (bool) $closeStreamOnFinish;
}

return $this->closeStreamOnFinish;
}
}
72 changes: 72 additions & 0 deletions tests/Ddeboer/DataImport/Tests/Writer/AbstractStreamWriterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Ddeboer\DataImport\Tests\Writer;

class AbstractStreamWriterTest extends StreamWriterTest
{
protected function setUp()
{
$this->writer = $this->getMockForAbstractClass('Ddeboer\\DataImport\\Writer\\AbstractStreamWriter');
}

public function testItImplementsWriterInterface()
{
$this->assertInstanceOf('Ddeboer\\DataImport\\Writer\\WriterInterface', $this->writer);
}

public function testItThrowsInvalidArgumentExceptionOnInvalidStream()
{
$invalidStreams = array(0, 1, null, 'stream', new \stdClass());
foreach ($invalidStreams as $invalidStream) {
try {
$this->writer->setStream($invalidStream);
$this->fail('Above call should throw exception');
} catch (\InvalidArgumentException $exception) {
$this->assertContains('Expects argument to be a stream resource', $exception->getMessage());
}
}
}

public function testGetStreamReturnsAStreamResource()
{
$this->assertTrue('resource' == gettype($stream = $this->writer->getStream()), 'getStream should return a resource');
$this->assertEquals('stream', get_resource_type($stream));
}

public function testSetStream()
{
$this->assertSame($this->writer, $this->writer->setStream($this->getStream()));
$this->assertSame($this->getStream(), $this->writer->getStream());
}

public function testEol()
{
$this->assertSame("\n", $this->writer->getEol());
$this->assertSame($this->writer, $this->writer->setEol("\r\n"));
$this->assertSame("\r\n", $this->writer->getEol());
}

public function testCloseOnFinishIsInhibitable()
{
$this->assertTrue($this->writer->closeStreamOnFinish());
$this->assertFalse($this->writer->closeStreamOnFinish(false));
$this->assertFalse($this->writer->closeStreamOnFinish());
$this->assertTrue($this->writer->closeStreamOnFinish(true));
$this->assertTrue($this->writer->closeStreamOnFinish());
}

public function testFinishCloseStreamAccordingToCloseOnFinishState()
{
$stream = $this->getStream();
$this->writer->setStream($stream);
$this->writer->prepare();

$this->writer->closeStreamOnFinish(false);
$this->writer->finish();
$this->assertTrue(is_resource($stream));

$this->writer->closeStreamOnFinish(true);
$this->writer->finish();
$this->assertFalse(is_resource($stream));
}
}
30 changes: 30 additions & 0 deletions tests/Ddeboer/DataImport/Tests/Writer/StreamWriterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Ddeboer\DataImport\Tests\Writer;

use Ddeboer\DataImport\Writer\AbstractStreamWriter;

abstract class StreamWriterTest extends \PHPUnit_Framework_TestCase
{
protected $stream;

/** @var AbstractStreamWriter */
protected $writer;

protected function tearDown()
{
if (is_resource($this->stream)) {
fclose($this->stream);
$this->stream = null;
}
}

protected function getStream()
{
if (!is_resource($this->stream)) {
$this->stream = fopen('php://temp', 'r+');
}

return $this->stream;
}
}

0 comments on commit 054e0e0

Please sign in to comment.