Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -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', $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 testCloseOnFinishIsInhibitable()
{
$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->setCloseStreamOnFinish(true);
$this->writer->getStream();
$this->assertFalse($this->writer->getCloseStreamOnFinish());
}
public function testFinishCloseStreamAccordingToCloseOnFinishState()
{
$stream = $this->getStream();
$this->writer->setStream($stream);
$this->writer->prepare();
$this->writer->setCloseStreamOnFinish(false);
$this->writer->finish();
$this->assertTrue(is_resource($stream));
$this->writer->setCloseStreamOnFinish(true);
$this->writer->finish();
$this->assertFalse(is_resource($stream));
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Ddeboer\DataImport\Tests\Writer;
use Ddeboer\DataImport\Writer\BatchWriter;
class BatchWriterTest extends \PHPUnit_Framework_TestCase
{
public function testWriteItem()
{
$delegate = $this->getMock('Ddeboer\DataImport\Writer');
$writer = new BatchWriter($delegate);
$delegate->expects($this->once())
->method('prepare');
$delegate->expects($this->never())
->method('writeItem');
$writer->prepare();
$writer->writeItem(['Test']);
}
public function testFlush()
{
$delegate = $this->getMock('Ddeboer\DataImport\Writer');
$writer = new BatchWriter($delegate);
$delegate->expects($this->exactly(20))
->method('writeItem');
$writer->prepare();
for ($i = 0; $i < 20; $i++) {
$writer->writeItem(['Test']);
}
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Ddeboer\DataImport\Tests\Writer;
use Ddeboer\DataImport\Writer\CallbackWriter;
/**
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*/
class CallbackWriterTest extends \PHPUnit_Framework_TestCase
{
public function testPrepare()
{
$callable = function(array $item) {
return '';
};
$writer = new CallbackWriter($callable);
$writer->prepare();
}
public function testWriteItem()
{
$string = '';
$callable = function(array $item) use (&$string) {
$string = implode(',', array_values($item));
};
$writer = new CallbackWriter($callable);
$writer->writeItem(array('foo' => 'bar', 'bar' => 'foo'));
$this->assertEquals('bar,foo', $string);
}
public function testFinish()
{
$callable = function(array $item) {
return '';
};
$writer = new CallbackWriter($callable);
$writer->finish();
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Ddeboer\DataImport\Tests\Writer;
use Ddeboer\DataImport\Writer\ConsoleProgressWriter;
use Ddeboer\DataImport\Workflow\StepAggregator;
use Ddeboer\DataImport\Reader\ArrayReader;
use Symfony\Component\Console\Output\NullOutput;
class ConsoleProgressWriterTest extends \PHPUnit_Framework_TestCase
{
public function testWrite()
{
$data = array(
array(
'first' => 'The first',
'second' => 'Second property'
), array(
'first' => 'Another first',
'second' => 'Last second'
)
);
$reader = new ArrayReader($data);
$output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')
->getMock();
$outputFormatter = $this->getMock('Symfony\Component\Console\Formatter\OutputFormatterInterface');
$output->expects($this->once())
->method('isDecorated')
->will($this->returnValue(true));
$output->expects($this->atLeastOnce())
->method('getFormatter')
->will($this->returnValue($outputFormatter));
$output->expects($this->atLeastOnce())
->method('write');
$writer = new ConsoleProgressWriter($output, $reader);
$workflow = new StepAggregator($reader);
$workflow->addWriter($writer)
->process();
$this->assertEquals('debug', $writer->getVerbosity());
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Ddeboer\DataImport\Tests\Writer;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Helper\Table;
use Ddeboer\DataImport\Workflow\StepAggregator;
use Ddeboer\DataImport\Reader\ArrayReader;
use Ddeboer\DataImport\ItemConverter\MappingItemConverter;
use Ddeboer\DataImport\Writer\ConsoleTableWriter;
/**
* @author Igor Mukhin <igor.mukhin@gmail.com>
*/
class ConsoleTableWriterTest extends \PHPUnit_Framework_TestCase
{
public function testRightColumnsHeadersNamesAfterItemConverter()
{
$data = array(
array(
'firstname' => 'John',
'lastname' => 'Doe'
),
array(
'firstname' => 'Ivan',
'lastname' => 'Sidorov'
)
);
$reader = new ArrayReader($data);
$output = new BufferedOutput();
$table = $this->getMockBuilder('Symfony\Component\Console\Helper\Table')
->disableOriginalConstructor()
->getMock();
$table->expects($this->at(2))
->method('addRow');
$workflow = new StepAggregator($reader);
$workflow
->addWriter(new ConsoleTableWriter($output, $table))
->process()
;
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace Ddeboer\DataImport\Tests\Writer;
use Ddeboer\DataImport\Writer\CsvWriter;
class CsvWriterTest extends StreamWriterTest
{
public function testWriteItem()
{
$writer = new CsvWriter(';', '"', $this->getStream());
$writer->prepare();
$writer->writeItem(array('first', 'last'));
$writer->writeItem(array(
'first' => 'James',
'last' => 'Bond'
));
$writer->writeItem(array(
'first' => '',
'last' => 'Dr. No'
));
$this->assertContentsEquals(
"first;last\nJames;Bond\n;\"Dr. No\"\n",
$writer
);
$writer->finish();
}
public function testWriteUtf8Item()
{
$writer = new CsvWriter(';', '"', $this->getStream(), true);
$writer->prepare();
$writer->writeItem(array('Précédent', 'Suivant'));
$this->assertContentsEquals(
chr(0xEF) . chr(0xBB) . chr(0xBF) . "Précédent;Suivant\n",
$writer
);
$writer->finish();
}
/**
* Test that column names not prepended to first row
* if CsvWriter's 5-th parameter not given
*
* @author Igor Mukhin <igor.mukhin@gmail.com>
*/
public function testHeaderNotPrependedByDefault()
{
$writer = new CsvWriter(';', '"', $this->getStream(), false);
$writer->prepare();
$writer->writeItem(array(
'col 1 name'=>'col 1 value',
'col 2 name'=>'col 2 value',
'col 3 name'=>'col 3 value'
));
# Values should be at first line
$this->assertContentsEquals(
"\"col 1 value\";\"col 2 value\";\"col 3 value\"\n",
$writer
);
$writer->finish();
}
/**
* Test that column names prepended at first row
* and values have been written at second line
* if CsvWriter's 5-th parameter set to true
*
* @author Igor Mukhin <igor.mukhin@gmail.com>
*/
public function testHeaderPrependedWhenOptionSetToTrue()
{
$writer = new CsvWriter(';', '"', $this->getStream(), false, true);
$writer->prepare();
$writer->writeItem(array(
'col 1 name'=>'col 1 value',
'col 2 name'=>'col 2 value',
'col 3 name'=>'col 3 value'
));
# Column names should be at second line
# Values should be at second line
$this->assertContentsEquals(
"\"col 1 name\";\"col 2 name\";\"col 3 name\"\n" .
"\"col 1 value\";\"col 2 value\";\"col 3 value\"\n",
$writer
);
$writer->finish();
}
}

View File

@@ -0,0 +1,166 @@
<?php
namespace Ddeboer\DataImport\Tests\Writer;
use Ddeboer\DataImport\Writer\DoctrineWriter;
use Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity;
class DoctrineWriterTest extends \PHPUnit_Framework_TestCase
{
public function testWriteItem()
{
$em = $this->getEntityManager();
$em->expects($this->once())
->method('persist');
$writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
$association = new TestEntity();
$item = array(
'firstProperty' => 'some value',
'secondProperty' => 'some other value',
'firstAssociation'=> $association
);
$writer->writeItem($item);
}
protected function getEntityManager()
{
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->setMethods(array('getRepository', 'getClassMetadata', 'persist', 'flush', 'clear', 'getConnection', 'getReference'))
->disableOriginalConstructor()
->getMock();
$repo = $this->getMockBuilder('Doctrine\ORM\EntityRepository')
->disableOriginalConstructor()
->getMock();
$metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
->setMethods(array('getName', 'getFieldNames', 'getAssociationNames', 'setFieldValue', 'getAssociationMappings'))
->disableOriginalConstructor()
->getMock();
$metadata->expects($this->any())
->method('getName')
->will($this->returnValue('Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity'));
$metadata->expects($this->any())
->method('getFieldNames')
->will($this->returnValue(array('firstProperty', 'secondProperty')));
$metadata->expects($this->any())
->method('getAssociationNames')
->will($this->returnValue(array('firstAssociation')));
$metadata->expects($this->any())
->method('getAssociationMappings')
->will($this->returnValue(array(array('fieldName' => 'firstAssociation','targetEntity' => 'Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity'))));
$configuration = $this->getMockBuilder('Doctrine\DBAL\Configuration')
->setMethods(array('getConnection'))
->disableOriginalConstructor()
->getMock();
$connection = $this->getMockBuilder('Doctrine\DBAL\Connection')
->setMethods(array('getConfiguration', 'getDatabasePlatform', 'getTruncateTableSQL', 'executeQuery'))
->disableOriginalConstructor()
->getMock();
$connection->expects($this->any())
->method('getConfiguration')
->will($this->returnValue($configuration));
$connection->expects($this->any())
->method('getDatabasePlatform')
->will($this->returnSelf());
$connection->expects($this->any())
->method('getTruncateTableSQL')
->will($this->returnValue('TRUNCATE SQL'));
$connection->expects($this->any())
->method('executeQuery')
->with('TRUNCATE SQL');
$em->expects($this->once())
->method('getRepository')
->will($this->returnValue($repo));
$em->expects($this->once())
->method('getClassMetadata')
->will($this->returnValue($metadata));
$em->expects($this->any())
->method('getConnection')
->will($this->returnValue($connection));
$self = $this;
$em->expects($this->any())
->method('persist')
->will($this->returnCallback(function ($argument) use ($self) {
$self->assertNotNull($argument->getFirstAssociation());
return true;
}));
return $em;
}
public function testLoadAssociationWithoutObject()
{
$em = $this->getEntityManager();
$em->expects($this->once())
->method('persist');
$em->expects($this->once())
->method('getReference');
$writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
$item = array(
'firstProperty' => 'some value',
'secondProperty' => 'some other value',
'firstAssociation' => 'firstAssociationId'
);
$writer->writeItem($item);
}
public function testLoadAssociationWithPresetObject()
{
$em = $this->getEntityManager();
$em->expects($this->once())
->method('persist');
$em->expects($this->never())
->method('getReference');
$writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
$association = new TestEntity();
$item = array(
'firstProperty' => 'some value',
'secondProperty' => 'some other value',
'firstAssociation' => $association,
);
$writer->writeItem($item);
}
/**
* Test to make sure that we are clearing the write entity
*/
public function testFlushAndClear()
{
$em = $this->getEntityManager();
$em->expects($this->once())
->method('clear')
->with($this->equalTo('Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity'));
$writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
$writer->finish();
}
}

View File

@@ -0,0 +1,128 @@
<?php
namespace Ddeboer\DataImport\Tests\Writer;
use Ddeboer\DataImport\Writer\ExcelWriter;
class ExcelWriterTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (!extension_loaded('zip')) {
$this->markTestSkipped();
}
}
public function testWriteItemAppendWithSheetTitle()
{
$file = tempnam(sys_get_temp_dir(), null);
$writer = new ExcelWriter(new \SplFileObject($file, 'w'), 'Sheet 1');
$writer->prepare();
$writer->writeItem(array('first', 'last'));
$writer->writeItem(array(
'first' => 'James',
'last' => 'Bond'
));
$writer->writeItem(array(
'first' => '',
'last' => 'Dr. No'
));
$writer->finish();
// Open file with append mode ('a') to add a sheet
$writer = new ExcelWriter(new \SplFileObject($file, 'a'), 'Sheet 2');
$writer->prepare();
$writer->writeItem(array('first', 'last'));
$writer->writeItem(array(
'first' => 'Miss',
'last' => 'Moneypenny'
));
$writer->finish();
$excel = \PHPExcel_IOFactory::load($file);
$this->assertTrue($excel->sheetNameExists('Sheet 1'));
$this->assertEquals(3, $excel->getSheetByName('Sheet 1')->getHighestRow());
$this->assertTrue($excel->sheetNameExists('Sheet 2'));
$this->assertEquals(2, $excel->getSheetByName('Sheet 2')->getHighestRow());
}
public function testWriteItemWithoutSheetTitle()
{
$outputFile = new \SplFileObject(tempnam(sys_get_temp_dir(), null));
$writer = new ExcelWriter($outputFile);
$writer->prepare();
$writer->writeItem(array('first', 'last'));
$writer->finish();
}
/**
* Test that column names not prepended to first row if ExcelWriter's 4-th
* parameter not given
*
* @author Igor Mukhin <igor.mukhin@gmail.com>
*/
public function testHeaderNotPrependedByDefault()
{
$file = tempnam(sys_get_temp_dir(), null);
$writer = new ExcelWriter(new \SplFileObject($file, 'w'), null, 'Excel2007');
$writer->prepare();
$writer->writeItem(array(
'col 1 name'=>'col 1 value',
'col 2 name'=>'col 2 value',
'col 3 name'=>'col 3 value'
));
$writer->finish();
$excel = \PHPExcel_IOFactory::load($file);
$sheet = $excel->getActiveSheet()->toArray();
# Values should be at first line
$this->assertEquals(array('col 1 value', 'col 2 value', 'col 3 value'), $sheet[0]);
}
/**
* Test that column names prepended at first row
* and values have been written at second line
* if ExcelWriter's 4-th parameter set to true
*
* @author Igor Mukhin <igor.mukhin@gmail.com>
*/
public function testHeaderPrependedWhenOptionSetToTrue()
{
$file = tempnam(sys_get_temp_dir(), null);
$writer = new ExcelWriter(new \SplFileObject($file, 'w'), null, 'Excel2007', true);
$writer->prepare();
$writer->writeItem(array(
'col 1 name'=>'col 1 value',
'col 2 name'=>'col 2 value',
'col 3 name'=>'col 3 value'
));
$writer->finish();
$excel = \PHPExcel_IOFactory::load($file);
$sheet = $excel->getActiveSheet()->toArray();
# Check column names at first line
$this->assertEquals(array('col 1 name', 'col 2 name', 'col 3 name'), $sheet[0]);
# Check values at second line
$this->assertEquals(array('col 1 value', 'col 2 value', 'col 3 value'), $sheet[1]);
}
}

View File

@@ -0,0 +1,106 @@
<?php
namespace Ddeboer\DataImport\Tests\Writer;
use Ddeboer\DataImport\Writer\PdoWriter;
class PdoWriterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PDO
*/
private $pdo;
public function setUp()
{
$this->pdo = new \PDO('sqlite::memory:');
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); //important
$this->pdo->exec('DROP TABLE IF EXISTS `example`');
$this->pdo->exec('CREATE TABLE `example` (a TEXT, b TEXT)');
}
public function testValidWriteItem()
{
$writer = new PdoWriter($this->pdo, 'example');
$writer->prepare();
$writer->writeItem(array('a' => 'foo', 'b' => 'bar'));
$writer->finish();
$stmnt = $this->pdo->query('SELECT * FROM `example`');
$this->assertEquals(
array(array('a'=>'foo', 'b'=>'bar')),
$stmnt->fetchAll(\PDO::FETCH_ASSOC),
'database does not contain expected row'
);
}
public function testValidWriteMultiple()
{
$writer = new PdoWriter($this->pdo, 'example');
$writer->prepare();
$writer->writeItem(array('a' => 'foo', 'b' => 'bar'));
$writer->writeItem(array('a' => 'cat', 'b' => 'dog'));
$writer->writeItem(array('a' => 'ac', 'b' => 'dc'));
$writer->finish();
$stmnt = $this->pdo->query('SELECT * FROM `example`');
$this->assertEquals(
array(array('a'=>'foo', 'b'=>'bar'), array('a'=>'cat', 'b'=>'dog'), array('a'=>'ac', 'b'=>'dc')),
$stmnt->fetchAll(\PDO::FETCH_ASSOC),
'database does not contain all expected rows'
);
}
/**
* @expectedException \Ddeboer\DataImport\Exception\WriterException
*/
public function testWriteTooManyValues()
{
$writer = new PdoWriter($this->pdo, 'example');
$writer->prepare();
$writer->writeItem(array('foo', 'bar', 'baz')); //expects two
$writer->finish();
}
/**
* @expectedException \Ddeboer\DataImport\Exception\WriterException
*/
public function testWriteToNonexistentTable()
{
$writer = new PdoWriter($this->pdo, 'foobar');
$writer->prepare();
$writer->writeItem(array('foo', 'bar'));
$writer->finish();
}
/**
* Tests PDO instance with silent errors.
*
* @expectedException \Ddeboer\DataImport\Exception\WriterException
*/
public function testStatementCreateFailureWithNoException()
{
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
$writer = new PdoWriter($this->pdo, 'foob`ar');
$writer->prepare();
$writer->writeItem(array('foo', 'bar'));
$writer->finish();
}
/**
* Tests PDO instance with silent errors. First inert prepares the statement, second creates an exception.
*
* @expectedException \Ddeboer\DataImport\Exception\WriterException
*/
public function testWriteFailureWithNoException()
{
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT);
$writer = new PdoWriter($this->pdo, 'example');
$writer->prepare();
$writer->writeItem(array('foo', 'bar'));
$writer->writeItem(array('foo', 'bar', 'baz'));
$writer->finish();
}
}

View File

@@ -0,0 +1,141 @@
<?php
namespace Ddeboer\DataImport\Tests\Writer;
use Ddeboer\DataImport\Writer\StreamMergeWriter;
class StreamMergeWriterTest extends AbstractStreamWriterTest
{
/** @var StreamMergeWriter */
protected $writer;
protected function setUp()
{
parent::setUp();
$this->writer = new StreamMergeWriter();
}
public function testItIsInstantiable()
{
$this->assertInstanceOf('Ddeboer\DataImport\Writer\StreamMergeWriter', $this->writer);
}
public function testItIsAStreamWriter()
{
$this->assertInstanceOf('Ddeboer\DataImport\Writer\AbstractStreamWriter', $this->writer);
}
public function testDiscriminantField()
{
$this->assertSame($this->writer, $this->writer->setDiscriminantField('foo'));
$this->assertSame('foo', $this->writer->getDiscriminantField());
}
public function testDiscriminantFieldDefaultsToDiscr()
{
$this->assertSame('discr', $this->writer->getDiscriminantField());
}
public function testSetStreamWriterForSpecificDiscrValue()
{
$fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
->setMethods(array('setStream'))
->getMockForAbstractClass();
$this->writer->setStream($stream = $this->getStream());
$fooWriter
->expects($this->once())
->method('setStream')
->with($stream)
->will($this->returnSelf());
$this->assertSame($this->writer, $this->writer->setStreamWriter('foo', $fooWriter));
$this->assertSame($fooWriter, $this->writer->getStreamWriter('foo'));
}
public function testHasStreamWriter()
{
$fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
->getMockForAbstractClass();
$this->assertFalse($this->writer->hasStreamWriter('foo'), 'no foo stream writer should be registered');
$this->writer->setStreamWriter('foo', $fooWriter);
$this->assertTrue($this->writer->hasStreamWriter('foo'), 'foo stream writer should be registered');
}
public function testStreamWriters()
{
$fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
->getMockForAbstractClass();
$barWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
->getMockForAbstractClass();
$writers = array(
'foo' => $fooWriter,
'bar' => $barWriter,
);
$this->assertSame($this->writer, $this->writer->setStreamWriters($writers));
$this->assertSame($writers, $this->writer->getStreamWriters());
}
public function testWriteItem()
{
$fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
->getMockForAbstractClass();
$barWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
->getMockForAbstractClass();
$writers = array(
'foo' => $fooWriter,
'bar' => $barWriter,
);
$this->writer->setStreamWriters($writers);
$this->writer->setDiscriminantField('foo');
$barItem = array('foo' => 'bar');
$fooWriter->expects($this->never())
->method('writeItem');
$barWriter->expects($this->once())
->method('writeItem')
->with($barItem)
->will($this->returnSelf());
$this->writer->writeItem($barItem);
}
public function testSetStream()
{
$fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
->getMockForAbstractClass();
$barWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
->getMockForAbstractClass();
$writers = array(
'foo' => $fooWriter,
'bar' => $barWriter,
);
$stream = $this->writer->getStream();
$this->writer->setStreamWriters($writers);
$this->assertSame($stream, $fooWriter->getStream());
$this->assertSame($stream, $barWriter->getStream());
$this->assertSame($this->writer, $this->writer->setStream($stream = $this->getStream()));
$this->assertSame($stream, $fooWriter->getStream());
$this->assertSame($stream, $barWriter->getStream());
}
public function testSetWriterShouldInhibitStreamClose()
{
$fooWriter = $this->getMockBuilder('Ddeboer\DataImport\Writer\AbstractStreamWriter')
->setMethods(array('setCloseStreamOnFinish'))
->getMockForAbstractClass();
$fooWriter
->expects($this->once())
->method('setCloseStreamOnFinish')->with(false)
->will($this->returnArgument(0));
$this->writer->setStreamWriter('foo', $fooWriter);
}
}

View File

@@ -0,0 +1,44 @@
<?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;
}
/**
* @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);
}
}