summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-04-21 09:38:45 +0200
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-04-22 11:30:56 +0200
commit11a2d555d2c1e28f51a8aa20c9c2544a5b2f131f (patch)
tree538910f5cd8f8da601dcf79f852a330645585d1b
parent23b43fc9386bf007ca28d6e361e8d265647b35c9 (diff)
downloadcsv-11a2d555d2c1e28f51a8aa20c9c2544a5b2f131f.zip
csv-11a2d555d2c1e28f51a8aa20c9c2544a5b2f131f.tar.gz
csv-11a2d555d2c1e28f51a8aa20c9c2544a5b2f131f.tar.bz2
Bug fix getConversionIterator
-rw-r--r--.travis.yml5
-rw-r--r--src/AbstractCsv.php5
-rw-r--r--src/Reader.php5
-rw-r--r--test/CsvTest.php81
-rw-r--r--test/FactoryTest.php30
-rw-r--r--test/ReaderTest.php236
-rw-r--r--test/StreamFilterTest.php4
-rw-r--r--test/WriterTest.php78
8 files changed, 178 insertions, 266 deletions
diff --git a/.travis.yml b/.travis.yml
index 1daa574..878052b 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,8 +4,13 @@ php:
- 5.4
- 5.5
- 5.6
+ - 7.0
- hhvm
+matrix:
+ allow_failures:
+ - php: 7.0
+
before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-source --dev
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php
index 50e1226..78b408f 100644
--- a/src/AbstractCsv.php
+++ b/src/AbstractCsv.php
@@ -153,7 +153,10 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
*/
protected function getConversionIterator()
{
- return $this->getIterator();
+ $iterator = $this->getIterator();
+ $iterator->setFlags(SplFileObject::READ_CSV|SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
+
+ return $iterator;
}
/**
diff --git a/src/Reader.php b/src/Reader.php
index 4e764b5..16d233e 100644
--- a/src/Reader.php
+++ b/src/Reader.php
@@ -66,7 +66,7 @@ class Reader extends AbstractCsv
*/
protected function getConversionIterator()
{
- $iterator = $this->getIterator();
+ $iterator = parent::getConversionIterator();
$iterator = $this->applyBomStripping($iterator);
$iterator = $this->applyIteratorFilter($iterator);
$iterator = $this->applyIteratorSortBy($iterator);
@@ -242,8 +242,7 @@ class Reader extends AbstractCsv
}
if (0 == $offset && $this->isBomStrippable()) {
- $bom = $this->getInputBom();
- $res[0] = mb_substr($res[0], mb_strlen($bom));
+ $res[0] = mb_substr($res[0], mb_strlen($this->getInputBom()));
}
return $res;
diff --git a/test/CsvTest.php b/test/CsvTest.php
index 69c8418..68f0ba8 100644
--- a/test/CsvTest.php
+++ b/test/CsvTest.php
@@ -2,7 +2,6 @@
namespace League\Csv\test;
-use DateTime;
use League\Csv\Reader;
use League\Csv\Writer;
use PHPUnit_Framework_TestCase;
@@ -10,8 +9,6 @@ use SplFileInfo;
use SplFileObject;
use SplTempFileObject;
-date_default_timezone_set('UTC');
-
/**
* @group csv
*/
@@ -26,12 +23,12 @@ class CsvTest extends PHPUnit_Framework_TestCase
public function setUp()
{
- $csv = new SplTempFileObject();
+ $tmp = new SplTempFileObject();
foreach ($this->expected as $row) {
- $csv->fputcsv($row);
+ $tmp->fputcsv($row);
}
- $this->csv = Reader::createFromFileObject($csv, "\n");
+ $this->csv = Reader::createFromFileObject($tmp);
}
public function tearDown()
@@ -39,63 +36,25 @@ class CsvTest extends PHPUnit_Framework_TestCase
$this->csv = null;
}
- public function testIterator()
+ public function testInterface()
{
- $this->csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
- foreach ($this->csv as $key => $row) {
- $this->assertSame($this->expected[$key], $row);
- }
+ $this->assertInstanceOf('IteratorAggregate', $this->csv);
+ $this->assertInstanceOf('JsonSerializable', $this->csv);
}
public function testToHTML()
{
- $expected = <<<EOF
-<table class="table-csv-data">
-<tr>
-<td>john</td>
-<td>doe</td>
-<td>john.doe@example.com</td>
-</tr>
-<tr>
-<td>jane</td>
-<td>doe</td>
-<td>jane.doe@example.com</td>
-</tr>
-</table>
-EOF;
- $this->csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
- $this->assertSame($expected, $this->csv->toHTML());
+ $this->assertContains("<table", $this->csv->toHTML());
}
public function testToXML()
{
- $expected = <<<EOF
-<?xml version="1.0" encoding="UTF-8"?>
-<csv>
- <row>
- <cell>john</cell>
- <cell>doe</cell>
- <cell>john.doe@example.com</cell>
- </row>
- <row>
- <cell>jane</cell>
- <cell>doe</cell>
- <cell>jane.doe@example.com</cell>
- </row>
-</csv>
-
-EOF;
- $this->csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
- $doc = $this->csv->toXML();
- $this->assertInstanceof('\DomDocument', $doc);
- $doc->formatOutput = true;
- $this->assertSame($expected, $doc->saveXML());
+ $this->assertInstanceOf('DOMDocument', $this->csv->toXML());
}
public function testJsonSerialize()
{
- $this->csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
- $this->assertSame(json_encode($this->expected), json_encode($this->csv));
+ $this->assertContains(['john', 'doe', 'john.doe@example.com'], json_decode(json_encode($this->csv), true));
}
/**
@@ -121,24 +80,24 @@ EOF;
/**
* @runInSeparateProcess
*/
- public function testOutput()
+ public function testOutputSize()
{
- if (defined('HHVM_VERSION')) {
- $this->markTestSkipped();
- }
$this->assertSame(60, $this->csv->output("test.csv"));
- $headers = \xdebug_get_headers();
- $this->assertSame($headers[0], "Content-Type: application/octet-stream");
- $this->assertSame($headers[1], "Content-Transfer-Encoding: binary");
- $this->assertSame($headers[2], "Content-Disposition: attachment; filename=\"test.csv\"");
}
/**
- * @expectedException PHPUnit_Framework_Error
+ * @runInSeparateProcess
*/
- public function testFailedOutput()
+ public function testOutputHeaders()
{
- $this->csv->output(new DateTime);
+ if (! function_exists('xdebug_get_headers')) {
+ $this->markTestSkipped();
+ }
+ $this->csv->output("test.csv");
+ $headers = \xdebug_get_headers();
+ $this->assertSame($headers[0], "Content-Type: application/octet-stream");
+ $this->assertSame($headers[1], "Content-Transfer-Encoding: binary");
+ $this->assertSame($headers[2], "Content-Disposition: attachment; filename=\"test.csv\"");
}
public function testToString()
diff --git a/test/FactoryTest.php b/test/FactoryTest.php
index 7a85f92..0c11957 100644
--- a/test/FactoryTest.php
+++ b/test/FactoryTest.php
@@ -2,7 +2,6 @@
namespace League\Csv\test;
-use DateTime;
use League\Csv\Reader;
use PHPUnit_Framework_TestCase;
use SplFileInfo;
@@ -10,8 +9,6 @@ use SplFileObject;
use SplTempFileObject;
use StdClass;
-date_default_timezone_set('UTC');
-
/**
* @group factory
*/
@@ -40,7 +37,6 @@ class FactoryTest extends PHPUnit_Framework_TestCase
/**
* @expectedException InvalidArgumentException
- * @expectedExceptionMessage an `SplTempFileObject` object does not contain a valid path
*/
public function testCreateFromPathWithSplTempFileObject()
{
@@ -55,24 +51,6 @@ class FactoryTest extends PHPUnit_Framework_TestCase
$this->assertInstanceof('League\Csv\Reader', $reader);
}
- /**
- * @expectedException PHPUnit_Framework_Error
- */
- public function testCreateFromStringThrowExceptionWithBadNewline()
- {
- $expected = "john,doe,john.doe@example.com".PHP_EOL
- ."jane,doe,jane.doe@example.com".PHP_EOL;
- Reader::createFromString($expected, new \StdClass);
- }
-
- /**
- * @expectedException PHPUnit_Framework_Error
- */
- public function testCreateFromStringFromNotStringableObject()
- {
- Reader::createFromString(new DateTime());
- }
-
public function testCreateFromFileObject()
{
$reader = Reader::createFromFileObject(new SplTempFileObject());
@@ -88,12 +66,4 @@ class FactoryTest extends PHPUnit_Framework_TestCase
$this->assertInstanceof('League\Csv\Reader', $reader);
$this->assertInstanceof('SplFileObject', $reader->getIterator());
}
-
- /**
- * @expectedException PHPUnit_Framework_Error
- */
- public function testCreateFromFileObjectFailed()
- {
- Reader::createFromFileObject(new StdClass());
- }
}
diff --git a/test/ReaderTest.php b/test/ReaderTest.php
index e9efb83..87c238d 100644
--- a/test/ReaderTest.php
+++ b/test/ReaderTest.php
@@ -21,68 +21,95 @@ class ReaderTest extends PHPUnit_Framework_TestCase
public function setUp()
{
- $csv = new SplTempFileObject();
+ $tmp = new SplTempFileObject();
foreach ($this->expected as $row) {
- $csv->fputcsv($row);
+ $tmp->fputcsv($row);
}
- $this->csv = Reader::createFromFileObject($csv);
- $this->csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
+ $this->csv = Reader::createFromFileObject($tmp);
+ }
+
+ public function testSetLimit()
+ {
+ $this->assertCount(1, $this->csv->setLimit(1)->fetchAll());
}
/**
* @expectedException \InvalidArgumentException
- * @expectedExceptionMessage the limit must an integer greater or equals to -1
*/
- public function testSetLimit()
+ public function testSetLimitThrowException()
{
- $this->csv->setLimit(1);
- $this->assertCount(1, $this->csv->fetchAll());
$this->csv->setLimit(-4);
}
+ public function testSetOffset()
+ {
+ $this->assertContains(
+ ['jane', 'doe', 'jane.doe@example.com'],
+ $this->csv->setOffset(1)->fetchAll()
+ );
+ }
+
/**
* @expectedException \InvalidArgumentException
- * @expectedExceptionMessage the offset must be a positive integer or 0
*/
- public function testSetOffset()
+ public function testSetOffsetThrowException()
{
- $this->csv->setOffset(1);
- $this->assertCount(1, $this->csv->fetchAll());
-
$this->csv->setOffset('toto');
}
- public function testIntervalLimitTooLong()
+ /**
+ * @param $offset
+ * @param $limit
+ * @param $expected
+ * @dataProvider intervalTest
+ */
+ public function testInterval($offset, $limit, $expected)
{
- $this->csv->setOffset(1);
- $this->csv->setLimit(10);
- $this->assertSame([['jane', 'doe', 'jane.doe@example.com']], $this->csv->fetchAll());
+ $this->csv->setOffset($offset);
+ $this->csv->setLimit($limit);
+ $this->assertContains(
+ ['jane', 'doe', 'jane.doe@example.com'],
+ $this->csv->setOffset(1)->fetchAll()
+ );
+ }
+
+ public function intervalTest()
+ {
+ return [
+ 'tooHigh' => [1, 10, 1],
+ 'normal' => [1, 1, 1],
+ ];
}
- public function testInterval()
+ /**
+ * @expectedException \OutOfBoundsException
+ */
+ public function testIntervalThrowException()
{
$this->csv->setOffset(1);
- $this->csv->setLimit(1);
- $this->assertCount(1, $this->csv->fetchAll());
+ $this->csv->setLimit(0);
+ $this->csv->fetchAll();
}
+
public function testFilter()
{
$func = function ($row) {
return ! in_array('jane', $row);
};
$this->csv->addFilter($func);
-
- $this->assertCount(1, $this->csv->fetchAll());
+ $this->assertNotContains(['jane', 'doe', 'jane.doe@example.com'], $this->csv->fetchAll());
$func2 = function ($row) {
return ! in_array('john', $row);
};
$this->csv->addFilter($func2);
$this->csv->addFilter($func);
+ $res = $this->csv->fetchAll();
- $this->assertCount(0, $this->csv->fetchAll());
+ $this->assertNotContains(['john', 'doe', 'john.doe@example.com'], $res);
+ $this->assertNotContains(['jane', 'doe', 'jane.doe@example.com'], $res);
$this->csv->addFilter($func2);
$this->csv->addFilter($func);
@@ -90,7 +117,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase
$this->csv->removeFilter($func2);
$this->assertFalse($this->csv->hasFilter($func2));
- $this->assertCount(1, $this->csv->fetchAll());
+ $this->assertContains(['john', 'doe', 'john.doe@example.com'], $this->csv->fetchAll());
}
public function testSortBy()
@@ -99,28 +126,21 @@ class ReaderTest extends PHPUnit_Framework_TestCase
return strcmp($rowA[0], $rowB[0]);
};
$this->csv->addSortBy($func);
+ $this->csv->addFilter(function ($row) {
+ return $row != [null];
+
+ });
$this->assertSame(array_reverse($this->expected), $this->csv->fetchAll());
$this->csv->addSortBy($func);
$this->csv->addSortBy($func);
$this->csv->removeSortBy($func);
$this->assertTrue($this->csv->hasSortBy($func));
- $this->assertSame(array_reverse($this->expected), $this->csv->fetchAll());
- }
+ $this->csv->addFilter(function ($row) {
+ return $row != [null];
- public function testSortBy2()
- {
- $string = 'john,doe,john.doe@example.com'.PHP_EOL.'john,doe,john.doe@example.com';
- $csv = Reader::createFromString($string);
- $csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
- $func = function ($rowA, $rowB) {
- return strcmp($rowA[0], $rowB[0]);
- };
- $csv->addSortBy($func);
- $this->assertSame([
- ['john', 'doe', 'john.doe@example.com'],
- ['john', 'doe', 'john.doe@example.com'],
- ], $csv->fetchAll());
+ });
+ $this->assertSame(array_reverse($this->expected), $this->csv->fetchAll());
}
public function testFetchAll()
@@ -129,17 +149,16 @@ class ReaderTest extends PHPUnit_Framework_TestCase
return array_map('strtoupper', $value);
};
- $this->assertSame($this->expected, $this->csv->fetchAll());
- $this->assertSame(array_map($func, $this->expected), $this->csv->fetchAll($func));
+ $res = $this->csv->fetchAll($func);
+ $this->assertContains(['JANE', 'DOE', 'JANE.DOE@EXAMPLE.COM'], $res);
}
public function testFetchAssoc()
{
$keys = ['firstname', 'lastname', 'email'];
$res = $this->csv->fetchAssoc($keys);
- foreach ($res as $index => $row) {
+ foreach ($res as $offset => $row) {
$this->assertSame($keys, array_keys($row));
- $this->assertSame($this->expected[$index], array_values($row));
}
}
@@ -152,13 +171,18 @@ class ReaderTest extends PHPUnit_Framework_TestCase
foreach ($res as $row) {
$this->assertSame($keys, array_keys($row));
}
+ $this->assertContains([
+ 'firstname' => 'JANE',
+ 'lastname' => 'DOE',
+ 'email' => 'JANE.DOE@EXAMPLE.COM'
+ ], $res);
}
public function testFetchAssocLessKeys()
{
$keys = ['firstname'];
$res = $this->csv->fetchAssoc($keys);
- $this->assertSame([['firstname' => 'john'], ['firstname' => 'jane']], $res);
+ $this->assertContains(['firstname' => 'john'], $res);
}
public function testFetchAssocMoreKeys()
@@ -166,10 +190,12 @@ class ReaderTest extends PHPUnit_Framework_TestCase
$keys = ['firstname', 'lastname', 'email', 'age'];
$res = $this->csv->fetchAssoc($keys);
- foreach ($res as $row) {
- $this->assertCount(4, array_values($row));
- $this->assertNull($row['age']);
- }
+ $this->assertContains([
+ 'firstname' => 'jane',
+ 'lastname' => 'doe',
+ 'email' => 'jane.doe@example.com',
+ 'age' => null,
+ ], $res);
}
public function testFetchAssocWithRowIndex()
@@ -181,15 +207,14 @@ class ReaderTest extends PHPUnit_Framework_TestCase
[6, 7, 8],
];
- $tmpFile = new SplTempFileObject();
+ $tmp = new SplTempFileObject();
foreach ($arr as $row) {
- $tmpFile->fputcsv($row);
+ $tmp->fputcsv($row);
}
- $csv = Reader::createFromFileObject($tmpFile);
- $csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
+ $csv = Reader::createFromFileObject($tmp);
$res = $csv->setOffSet(2)->fetchAssoc(2);
- $this->assertSame([['D' => '6', 'E' => '7', 'F' => '8']], $res);
+ $this->assertContains(['D' => '6', 'E' => '7', 'F' => '8'], $res);
}
/**
@@ -203,7 +228,6 @@ class ReaderTest extends PHPUnit_Framework_TestCase
$tmpFile->fputcsv($row);
}
$csv = Reader::createFromFileObject($tmpFile);
- $csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
$csv->stripBom(true);
$this->assertSame($res, $csv->fetchAll()[0][0]);
@@ -229,18 +253,16 @@ class ReaderTest extends PHPUnit_Framework_TestCase
public function testStripBOMWithFetchAssoc()
{
- $tmpFile = new SplTempFileObject();
$expected = [
[Reader::BOM_UTF16_LE.'john', 'doe', 'john.doe@example.com', ],
['jane', 'doe', 'jane.doe@example.com', ],
];
- $tmpFile = new SplTempFileObject();
+ $tmp = new SplTempFileObject();
foreach ($expected as $row) {
- $tmpFile->fputcsv($row);
+ $tmp->fputcsv($row);
}
- $csv = Reader::createFromFileObject($tmpFile);
- $csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
+ $csv = Reader::createFromFileObject($tmp);
$csv->stripBom(true);
$res = array_keys($csv->fetchAssoc()[0]);
@@ -249,7 +271,6 @@ class ReaderTest extends PHPUnit_Framework_TestCase
/**
* @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Use a flat non empty array with unique string values
*/
public function testFetchAssocKeyFailure()
{
@@ -257,10 +278,11 @@ class ReaderTest extends PHPUnit_Framework_TestCase
}
/**
+ * @param $offset
+ * @dataProvider invalidOffsetWithFetchAssoc
* @expectedException \InvalidArgumentException
- * @expectedExceptionMessage the row index must be a positive integer or 0
*/
- public function testFetchAssocWithInvalidKey()
+ public function testFetchAssocWithInvalidOffset($offset)
{
$arr = [
['A', 'B', 'C'],
@@ -269,40 +291,33 @@ class ReaderTest extends PHPUnit_Framework_TestCase
[6, 7, 8],
];
- $tmpFile = new SplTempFileObject();
+ $tmp = new SplTempFileObject();
foreach ($arr as $row) {
- $tmpFile->fputcsv($row);
+ $tmp->fputcsv($row);
}
- $csv = Reader::createFromFileObject($tmpFile);
- $csv->fetchAssoc(-23);
+ Reader::createFromFileObject($tmp)->fetchAssoc($offset);
}
- /**
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage the specified row does not exist
- */
- public function testFetchAssocWithInvalidOffset()
+ public function invalidOffsetWithFetchAssoc()
{
- $arr = [
- ['A', 'B', 'C'],
- [1, 2, 3],
- ['D', 'E', 'F'],
- [6, 7, 8],
+ return [
+ 'negative' => [-23],
+ 'tooHigh' => [23],
];
-
- $tmpFile = new SplTempFileObject();
- foreach ($arr as $row) {
- $tmpFile->fputcsv($row);
- }
-
- $csv = Reader::createFromFileObject($tmpFile);
- $csv->fetchAssoc(23);
}
public function testFetchCol()
{
+ $this->csv->addFilter(function ($row) {
+ return $row != [null];
+
+ });
$this->assertSame(['john', 'jane'], $this->csv->fetchColumn(0));
+ $this->csv->addFilter(function ($row) {
+ return $row != [null];
+
+ });
$this->assertSame(['john', 'jane'], $this->csv->fetchColumn());
}
@@ -318,7 +333,10 @@ class ReaderTest extends PHPUnit_Framework_TestCase
$file->fputcsv($row);
}
$csv = Reader::createFromFileObject($file);
- $csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
+ $this->csv->addFilter(function ($row) {
+ return $row != [null];
+
+ });
$res = $csv->fetchColumn(2);
$this->assertInternalType('array', $res);
$this->assertCount(1, $res);
@@ -336,8 +354,11 @@ class ReaderTest extends PHPUnit_Framework_TestCase
$file->fputcsv($row);
}
$csv = Reader::createFromFileObject($file);
- $csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
$res = $csv->fetchColumn(2);
+ $this->csv->addFilter(function ($row) {
+ return $row != [null];
+
+ });
$this->assertInternalType('array', $res);
$this->assertCount(0, $res);
}
@@ -348,13 +369,15 @@ class ReaderTest extends PHPUnit_Framework_TestCase
$func = function ($value) {
return array_map('strtoupper', $value);
};
+ $this->csv->addFilter(function ($row) {
+ return $row != [null];
+ });
$this->assertSame(['JOHN', 'JANE'], $this->csv->fetchColumn(0, $func));
}
/**
* @expectedException \InvalidArgumentException
- * @expectedExceptionMessage the column index must be a positive integer or 0
*/
public function testFetchColFailure()
{
@@ -363,7 +386,6 @@ class ReaderTest extends PHPUnit_Framework_TestCase
/**
* @expectedException \InvalidArgumentException
- * @expectedExceptionMessage the offset must be a positive integer or 0
*/
public function testfetchOne()
{
@@ -376,6 +398,10 @@ class ReaderTest extends PHPUnit_Framework_TestCase
public function testEach()
{
$transform = [];
+ $this->csv->addFilter(function ($row) {
+ return $row != [null];
+
+ });
$res = $this->csv->each(function ($row) use (&$transform) {
$transform[] = array_map('strtoupper', $row);
@@ -383,6 +409,10 @@ class ReaderTest extends PHPUnit_Framework_TestCase
});
$this->assertSame($res, 2);
$this->assertSame(strtoupper($this->expected[0][0]), $transform[0][0]);
+ }
+
+ public function testEachWithEarlyReturns()
+ {
$res = $this->csv->each(function ($row, $index) {
if ($index > 0) {
return false;
@@ -395,34 +425,6 @@ class ReaderTest extends PHPUnit_Framework_TestCase
public function testGetWriter()
{
- $writer = $this->csv->newWriter();
- $writer->insertOne(['toto', 'le', 'herisson']);
- $expected = <<<EOF
-<table class="table-csv-data">
-<tr>
-<td>john</td>
-<td>doe</td>
-<td>john.doe@example.com</td>
-</tr>
-<tr>
-<td>jane</td>
-<td>doe</td>
-<td>jane.doe@example.com</td>
-</tr>
-<tr>
-<td>toto</td>
-<td>le</td>
-<td>herisson</td>
-</tr>
-</table>
-EOF;
- $writer->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
- $this->assertSame($expected, $writer->toHTML());
- }
-
- public function testGetWriter2()
- {
- $csv = Reader::createFromPath(__DIR__.'/foo.csv')->newWriter('a+');
- $this->assertInstanceOf('\League\Csv\Writer', $csv);
+ $this->assertInstanceOf('\League\Csv\Writer', $this->csv->newWriter());
}
}
diff --git a/test/StreamFilterTest.php b/test/StreamFilterTest.php
index d8c2162..56862ed 100644
--- a/test/StreamFilterTest.php
+++ b/test/StreamFilterTest.php
@@ -28,7 +28,6 @@ class StreamFilterTest extends PHPUnit_Framework_TestCase
/**
* @expectedException LogicException
- * @expectedExceptionMessage The stream filter API can not be used
*/
public function testInitStreamFilterWithSplFileObject()
{
@@ -47,7 +46,6 @@ class StreamFilterTest extends PHPUnit_Framework_TestCase
/**
* @expectedException LogicException
- * @expectedExceptionMessage The stream filter API can not be used
*/
public function testFailedprependStreamFilter()
{
@@ -58,7 +56,6 @@ class StreamFilterTest extends PHPUnit_Framework_TestCase
/**
* @expectedException LogicException
- * @expectedExceptionMessage The stream filter API can not be used
*/
public function testFailedapppendStreamFilter()
{
@@ -69,7 +66,6 @@ class StreamFilterTest extends PHPUnit_Framework_TestCase
/**
* @expectedException OutOfBoundsException
- * @expectedExceptionMessage the $mode should be a valid `STREAM_FILTER_*` constant
*/
public function testaddMultipleStreamFilter()
{
diff --git a/test/WriterTest.php b/test/WriterTest.php
index 99b5009..1d0aa98 100644
--- a/test/WriterTest.php
+++ b/test/WriterTest.php
@@ -3,15 +3,12 @@
namespace League\Csv\test;
use ArrayIterator;
-use DateTime;
use League\Csv\Writer;
use LimitIterator;
use PHPUnit_Framework_TestCase;
use SplFileObject;
use SplTempFileObject;
-date_default_timezone_set('UTC');
-
/**
* @group writer
*/
@@ -45,81 +42,52 @@ class WriterTest extends PHPUnit_Framework_TestCase
{
$expected = [
['john', 'doe', 'john.doe@example.com'],
- 'john,doe,john.doe@example.com',
+ 'jane,doe,jane.doe@example.com',
];
foreach ($expected as $row) {
$this->csv->insertOne($row);
}
- $this->csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
- foreach ($this->csv as $row) {
- $this->assertSame(['john', 'doe', 'john.doe@example.com'], $row);
- }
+ $this->assertContains(['john', 'doe', 'john.doe@example.com'], $this->csv);
+ $this->assertContains(['jane', 'doe', 'jane.doe@example.com'], $this->csv);
}
public function testInsertNormalFile()
{
$csv = Writer::createFromPath(__DIR__.'/foo.csv', 'a+');
$csv->insertOne(['jane', 'doe', 'jane.doe@example.com']);
- $iterator = new LimitIterator($csv->getIterator(), 1, 1);
- $iterator->rewind();
- $this->assertSame(['jane', 'doe', 'jane.doe@example.com'], $iterator->getInnerIterator()->current());
- }
-
- /**
- * @expectedException PHPUnit_Framework_Error
- */
- public function testInsertWithoutValidation()
- {
- $expected = [
- ['john', 'doe', 'john.doe@example.com'],
- 'john,doe,john.doe@example.com',
- ['john', null, 'john.doe@example.com'],
- new \StdClass,
- ];
- $this->csv->insertAll($expected);
+ $this->assertContains(['jane', 'doe', 'jane.doe@example.com'], $csv);
}
/**
- * @expectedException PHPUnit_Framework_Error
+ * @expectedException InvalidArgumentException
*/
- public function testFailedInsertWithWrongData()
+ public function testFailedSaveWithWrongType()
{
- $this->csv->insertOne(new DateTime());
+ $this->csv->insertAll(new \StdClass());
}
/**
- * @expectedException PHPUnit_Framework_Error
+ * @param $argument
+ * @param $expected
+ * @dataProvider dataToSave
*/
- public function testFailedInsertWithMultiDimensionArray()
+ public function testSave($argument, $expected)
{
- $this->csv->insertOne(['john', new DateTime()]);
+ $this->csv->insertAll($argument);
+ $this->assertContains($expected, $this->csv);
}
- public function testSave()
+ public function dataToSave()
{
$multipleArray = [
['john', 'doe', 'john.doe@example.com'],
'jane,doe,jane.doe@example.com',
];
- $this->csv->insertAll($multipleArray);
- $this->csv->insertAll(new ArrayIterator($multipleArray));
- $this->csv->setFlags(SplFileObject::READ_AHEAD|SplFileObject::SKIP_EMPTY);
- foreach ($this->csv as $key => $row) {
- $expected = ['jane', 'doe', 'jane.doe@example.com'];
- if ($key%2 == 0) {
- $expected = ['john', 'doe', 'john.doe@example.com'];
- }
- $this->assertSame($expected, $row);
- }
- }
- /**
- * @expectedException InvalidArgumentException
- * @expectedExceptionMessage the provided data must be an array OR a \Traversable object
- */
- public function testFailedSaveWithWrongType()
- {
- $this->csv->insertAll(new DateTime());
+ return [
+ 'array' => [$multipleArray, $multipleArray[0]],
+ 'iterator' => [new ArrayIterator($multipleArray), ['jane', 'doe', 'jane.doe@example.com']],
+ ];
}
public function testGetReader()
@@ -184,4 +152,14 @@ class WriterTest extends PHPUnit_Framework_TestCase
$this->csv->clearFormatters();
$this->assertFalse($this->csv->hasFormatter($func));
}
+
+ public function testConversionWithWriter()
+ {
+ $this->csv->insertAll([
+ ['john', 'doe', 'john.doe@example.com'],
+ ['jane', 'doe', 'jane.doe@example.com'],
+ ['toto', 'le', 'herisson'],
+ ]);
+ $this->assertStringStartsWith("<table", $this->csv->toHTML());
+ }
}