Merge pull request #121 from box/add_test_for_empty_csv

Fix infinite loop for CSV with all lines empty
This commit is contained in:
Adrien Loison 2015-10-05 21:15:11 +02:00
commit 45980195cd
5 changed files with 46 additions and 16 deletions

View File

@ -111,11 +111,14 @@ class RowIterator implements IteratorInterface
*/ */
public function next() public function next()
{ {
$lineData = false;
$this->hasReachedEndOfFile = $this->globalFunctionsHelper->feof($this->filePointer); $this->hasReachedEndOfFile = $this->globalFunctionsHelper->feof($this->filePointer);
if (!$this->hasReachedEndOfFile) { if ($this->hasReachedEndOfFile) {
return;
}
do { do {
$lineData = false;
$utf8EncodedLineData = $this->getNextUTF8EncodedLine(); $utf8EncodedLineData = $this->getNextUTF8EncodedLine();
if ($utf8EncodedLineData !== false) { if ($utf8EncodedLineData !== false) {
$lineData = $this->globalFunctionsHelper->str_getcsv($utf8EncodedLineData, $this->fieldDelimiter, $this->fieldEnclosure); $lineData = $this->globalFunctionsHelper->str_getcsv($utf8EncodedLineData, $this->fieldDelimiter, $this->fieldEnclosure);
@ -132,7 +135,6 @@ class RowIterator implements IteratorInterface
$this->hasReachedEndOfFile = $hasNowReachedEndOfFile; $this->hasReachedEndOfFile = $hasNowReachedEndOfFile;
} }
} }
}
/** /**
* Returns the next line, converted if necessary to UTF-8. * Returns the next line, converted if necessary to UTF-8.

View File

@ -142,6 +142,29 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expectedRows, $allRows); $this->assertEquals($expectedRows, $allRows);
} }
/**
* @return array
*/
public function dataProviderForTestReadShouldReadEmptyFile()
{
return [
['csv_empty.csv'],
['csv_all_lines_empty.csv'],
];
}
/**
* @dataProvider dataProviderForTestReadShouldReadEmptyFile
*
* @param string $fileName
* @return void
*/
public function testReadShouldReadEmptyFile($fileName)
{
$allRows = $this->getAllRowsForFile($fileName);
$this->assertEquals([], $allRows);
}
/** /**
* @return void * @return void
*/ */

View File

@ -3,3 +3,6 @@ require_once(dirname(__DIR__) . '/vendor/autoload.php');
require_once(dirname(__DIR__) . '/tests/Spout/TestUsingResource.php'); require_once(dirname(__DIR__) . '/tests/Spout/TestUsingResource.php');
require_once(dirname(__DIR__) . '/tests/Spout/ReflectionHelper.php'); require_once(dirname(__DIR__) . '/tests/Spout/ReflectionHelper.php');
// Make sure a timezone is set to be able to work with dates
date_default_timezone_set('UTC');

View File

@ -0,0 +1,2 @@

View File