Skip to content

Commit

Permalink
Split method closeStreamOnFinish into setter/getter
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 ada4375 commit e3ddbd0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ class MyStreamWriter extends AbstractStreamWriter
}

$writer = new MyStreamWriter(fopen('php://temp', 'r+'));
$writer->closeStreamOnFinish();
$writer->setCloseStreamOnFinish(false);

$workflow->addWriter(new MyStreamWriter());
$workflow->process();
Expand Down
22 changes: 15 additions & 7 deletions src/Ddeboer/DataImport/Writer/AbstractStreamWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getStream()
{
if (null === $this->stream) {
$this->setStream(fopen('php://temp', 'r+'));
$this->closeStreamOnFinish(false);
$this->setCloseStreamOnFinish(false);
}

return $this->stream;
Expand Down Expand Up @@ -98,7 +98,7 @@ public function prepare()
*/
public function finish()
{
if (is_resource($this->stream) && $this->closeStreamOnFinish()) {
if (is_resource($this->stream) && $this->getCloseStreamOnFinish()) {
fclose($this->stream);
}

Expand All @@ -108,16 +108,24 @@ public function finish()
/**
* Should underlying stream be closed on finish?
*
* @param null|bool $closeStreamOnFinish
* @param bool $closeStreamOnFinish
*
* @return bool
*/
public function closeStreamOnFinish($closeStreamOnFinish = null)
public function setCloseStreamOnFinish($closeStreamOnFinish = true)
{
if (null !== $closeStreamOnFinish) {
$this->closeStreamOnFinish = (bool) $closeStreamOnFinish;
}
$this->closeStreamOnFinish = (bool) $closeStreamOnFinish;

return $this;
}

/**
* Is Stream closed on finish?
*
* @return bool
*/
public function getCloseStreamOnFinish()
{
return $this->closeStreamOnFinish;
}
}
2 changes: 1 addition & 1 deletion src/Ddeboer/DataImport/Writer/StreamMergeWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function setStreamWriters(array $writers)
public function setStreamWriter($key, AbstractStreamWriter $writer)
{
$writer->setStream($this->getStream());
$writer->closeStreamOnFinish(false);
$writer->setCloseStreamOnFinish(false);
$this->writers[$key] = $writer;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ public function testEol()

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());
$this->assertTrue($this->writer->getCloseStreamOnFinish());
$this->assertSame($this->writer, $this->writer->setCloseStreamOnFinish(false));
$this->assertFalse($this->writer->getCloseStreamOnFinish());
$this->assertSame($this->writer, $this->writer->setCloseStreamOnFinish(true));
$this->assertTrue($this->writer->getCloseStreamOnFinish());
}

public function testCloseOnFinishIsFalseForAutoOpenedStreams()
{
$this->writer->closeStreamOnFinish(true);
$this->writer->setCloseStreamOnFinish(true);
$this->writer->getStream();
$this->assertFalse($this->writer->closeStreamOnFinish());
$this->assertFalse($this->writer->getCloseStreamOnFinish());
}

public function testFinishCloseStreamAccordingToCloseOnFinishState()
Expand All @@ -68,11 +68,11 @@ public function testFinishCloseStreamAccordingToCloseOnFinishState()
$this->writer->setStream($stream);
$this->writer->prepare();

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

$this->writer->closeStreamOnFinish(true);
$this->writer->setCloseStreamOnFinish(true);
$this->writer->finish();
$this->assertFalse(is_resource($stream));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ public function testSetStream()
public function testSetWriterShouldInhibitStreamClose()
{
$fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
->setMethods(array('closeStreamOnFinish'))
->setMethods(array('setCloseStreamOnFinish'))
->getMockForAbstractClass();

$fooWriter
->expects($this->once())
->method('closeStreamOnFinish')->with(false)
->method('setCloseStreamOnFinish')->with(false)
->will($this->returnArgument(0));

$this->writer->setStreamWriter('foo', $fooWriter);
Expand Down

0 comments on commit e3ddbd0

Please sign in to comment.