';
diff --git a/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php b/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php
index 38709716..4e725d03 100644
--- a/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php
+++ b/tests/PhpSpreadsheetTests/Functional/FreezePaneTest.php
@@ -19,7 +19,7 @@ class FreezePaneTest extends AbstractFunctional
*
* @param string $format
*/
- public function testFreezePane($format)
+ public function testFreezePane($format): void
{
$cellSplit = 'B4';
$topLeftCell = 'E7';
@@ -43,7 +43,7 @@ class FreezePaneTest extends AbstractFunctional
*
* @param string $format
*/
- public function testFreezePaneWithInvalidSelectedCells($format)
+ public function testFreezePaneWithInvalidSelectedCells($format): void
{
$cellSplit = 'A7';
$topLeftCell = 'A24';
@@ -71,7 +71,7 @@ class FreezePaneTest extends AbstractFunctional
*
* @param string $format
*/
- public function testFreezePaneUserSelectedCell($format)
+ public function testFreezePaneUserSelectedCell($format): void
{
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
@@ -100,7 +100,7 @@ class FreezePaneTest extends AbstractFunctional
*
* @param string $format
*/
- public function testNoFreezePaneUserSelectedCell($format)
+ public function testNoFreezePaneUserSelectedCell($format): void
{
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
diff --git a/tests/PhpSpreadsheetTests/Functional/MergedCellsTest.php b/tests/PhpSpreadsheetTests/Functional/MergedCellsTest.php
index 7a601949..39865817 100644
--- a/tests/PhpSpreadsheetTests/Functional/MergedCellsTest.php
+++ b/tests/PhpSpreadsheetTests/Functional/MergedCellsTest.php
@@ -21,7 +21,7 @@ class MergedCellsTest extends AbstractFunctional
*
* @param string $format
*/
- public function testMergedCells($format)
+ public function testMergedCells($format): void
{
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
diff --git a/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php b/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php
index 7c3a9112..a335fd2f 100644
--- a/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php
+++ b/tests/PhpSpreadsheetTests/Functional/PrintAreaTest.php
@@ -20,7 +20,7 @@ class PrintAreaTest extends AbstractFunctional
*
* @param string $format
*/
- public function testPageSetup($format)
+ public function testPageSetup($format): void
{
// Create new workbook with 3 sheets and different print areas
$spreadsheet = new Spreadsheet();
@@ -35,7 +35,7 @@ class PrintAreaTest extends AbstractFunctional
$worksheet4 = $spreadsheet->createSheet()->setTitle('Sheet 4');
$worksheet4->getPageSetup()->setPrintArea('A4:B4,D1:E4');
- $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, function (BaseReader $reader) {
+ $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, function (BaseReader $reader): void {
$reader->setLoadSheetsOnly(['Sheet 1', 'Sheet 3', 'Sheet 4']);
});
diff --git a/tests/PhpSpreadsheetTests/Functional/ReadBlankCellsTest.php b/tests/PhpSpreadsheetTests/Functional/ReadBlankCellsTest.php
index 02c752ac..9dac3437 100644
--- a/tests/PhpSpreadsheetTests/Functional/ReadBlankCellsTest.php
+++ b/tests/PhpSpreadsheetTests/Functional/ReadBlankCellsTest.php
@@ -22,10 +22,9 @@ class ReadBlankCellsTest extends AbstractFunctional
*
* @dataProvider providerSheetFormat
*
- * @param array $arrayData
* @param mixed $format
*/
- public function testXlsxLoadWithNoBlankCells($format)
+ public function testXlsxLoadWithNoBlankCells($format): void
{
$spreadsheet = new Spreadsheet();
$spreadsheet->getActiveSheet()->getCell('B2')->setValue('');
@@ -33,15 +32,15 @@ class ReadBlankCellsTest extends AbstractFunctional
$spreadsheet->getActiveSheet()->getCell('C3')->setValue('C3');
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
- $this->assertTrue($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('B2'));
- $this->assertFalse($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('C2'));
- $this->assertTrue($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('C3'));
+ self::assertTrue($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('B2'));
+ self::assertFalse($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('C2'));
+ self::assertTrue($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('C3'));
- $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, function ($reader) {
+ $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, function ($reader): void {
$reader->setReadEmptyCells(false);
});
- $this->assertFalse($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('B2'));
- $this->assertFalse($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('C2'));
- $this->assertTrue($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('C3'));
+ self::assertFalse($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('B2'));
+ self::assertFalse($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('C2'));
+ self::assertTrue($reloadedSpreadsheet->getActiveSheet()->getCellCollection()->has('C3'));
}
}
diff --git a/tests/PhpSpreadsheetTests/Functional/ReadFilterTest.php b/tests/PhpSpreadsheetTests/Functional/ReadFilterTest.php
index 288fc57e..efca228c 100644
--- a/tests/PhpSpreadsheetTests/Functional/ReadFilterTest.php
+++ b/tests/PhpSpreadsheetTests/Functional/ReadFilterTest.php
@@ -34,10 +34,9 @@ class ReadFilterTest extends AbstractFunctional
*
* @dataProvider providerCellsValues
*
- * @param array $arrayData
* @param mixed $format
*/
- public function testXlsxLoadWithoutReadFilter($format, array $arrayData)
+ public function testXlsxLoadWithoutReadFilter($format, array $arrayData): void
{
$spreadsheet = new Spreadsheet();
@@ -62,19 +61,18 @@ class ReadFilterTest extends AbstractFunctional
*
* @dataProvider providerCellsValues
*
- * @param array $arrayData
* @param mixed $format
*/
- public function testXlsxLoadWithReadFilter($format, array $arrayData)
+ public function testXlsxLoadWithReadFilter($format, array $arrayData): void
{
$spreadsheet = new Spreadsheet();
$spreadsheet->getActiveSheet()->fromArray($arrayData, null, 'A1');
- $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, function ($reader) {
+ $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, function ($reader): void {
// Create a stub for the readFilter class.
$readFilterStub = $this->createMock(IReadFilter::class);
$readFilterStub->method('readCell')
- ->will($this->returnCallback([$this, 'readFilterReadCell']));
+ ->willReturnCallback([$this, 'readFilterReadCell']);
// apply filter
$reader->setReadFilter($readFilterStub);
});
diff --git a/tests/PhpSpreadsheetTests/Functional/SelectedCellsTest.php b/tests/PhpSpreadsheetTests/Functional/SelectedCellsTest.php
index 03d4be9f..625f2428 100644
--- a/tests/PhpSpreadsheetTests/Functional/SelectedCellsTest.php
+++ b/tests/PhpSpreadsheetTests/Functional/SelectedCellsTest.php
@@ -20,7 +20,7 @@ class SelectedCellsTest extends AbstractFunctional
*
* @param string $format
*/
- public function testSelectedCells($format)
+ public function testSelectedCells($format): void
{
$spreadsheet = new Spreadsheet();
diff --git a/tests/PhpSpreadsheetTests/Functional/StreamTest.php b/tests/PhpSpreadsheetTests/Functional/StreamTest.php
new file mode 100644
index 00000000..dcca6b8a
--- /dev/null
+++ b/tests/PhpSpreadsheetTests/Functional/StreamTest.php
@@ -0,0 +1,43 @@
+getActiveSheet()->setCellValue('A1', 'foo');
+ $writer = IOFactory::createWriter($spreadsheet, $format);
+
+ $stream = fopen('php://memory', 'wb+');
+ self::assertSame(0, fstat($stream)['size']);
+
+ $writer->save($stream);
+
+ self::assertIsResource($stream, 'should not close the stream for further usage out of PhpSpreadsheet');
+ self::assertGreaterThan(0, fstat($stream)['size'], 'something should have been written to the stream');
+ self::assertGreaterThan(0, ftell($stream), 'should not be rewinded, because not all streams support it');
+ }
+}
diff --git a/tests/PhpSpreadsheetTests/Functional/TypeAttributePreservationTest.php b/tests/PhpSpreadsheetTests/Functional/TypeAttributePreservationTest.php
index 53cc718d..e6d6377b 100644
--- a/tests/PhpSpreadsheetTests/Functional/TypeAttributePreservationTest.php
+++ b/tests/PhpSpreadsheetTests/Functional/TypeAttributePreservationTest.php
@@ -9,7 +9,7 @@ class TypeAttributePreservationTest extends AbstractFunctional
public function providerFormulae()
{
$formats = ['Xlsx'];
- $data = require 'data/Functional/TypeAttributePreservation/Formula.php';
+ $data = require 'tests/data/Functional/TypeAttributePreservation/Formula.php';
$result = [];
foreach ($formats as $f) {
@@ -27,9 +27,8 @@ class TypeAttributePreservationTest extends AbstractFunctional
* @dataProvider providerFormulae
*
* @param string $format
- * @param array $values
*/
- public function testFormulae($format, array $values)
+ public function testFormulae($format, array $values): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
diff --git a/tests/PhpSpreadsheetTests/Functional/WorkbookViewAttributesTest.php b/tests/PhpSpreadsheetTests/Functional/WorkbookViewAttributesTest.php
index cce2a95b..f97ad9cf 100644
--- a/tests/PhpSpreadsheetTests/Functional/WorkbookViewAttributesTest.php
+++ b/tests/PhpSpreadsheetTests/Functional/WorkbookViewAttributesTest.php
@@ -24,7 +24,7 @@ class WorkbookViewAttributesTest extends AbstractFunctional
*
* @param string $format
*/
- public function testPreserveWorkbookViewAttributes($format)
+ public function testPreserveWorkbookViewAttributes($format): void
{
// Create a dummy workbook with two worksheets
$workbook = new Spreadsheet();
@@ -36,14 +36,14 @@ class WorkbookViewAttributesTest extends AbstractFunctional
$worksheet2->setCellValue('A1', 2);
// Check that the bookview attributes return default values
- $this->assertTrue($workbook->getShowHorizontalScroll());
- $this->assertTrue($workbook->getShowVerticalScroll());
- $this->assertTrue($workbook->getShowSheetTabs());
- $this->assertTrue($workbook->getAutoFilterDateGrouping());
- $this->assertFalse($workbook->getMinimized());
- $this->assertSame(0, $workbook->getFirstSheetIndex());
- $this->assertSame(600, $workbook->getTabRatio());
- $this->assertSame(Spreadsheet::VISIBILITY_VISIBLE, $workbook->getVisibility());
+ self::assertTrue($workbook->getShowHorizontalScroll());
+ self::assertTrue($workbook->getShowVerticalScroll());
+ self::assertTrue($workbook->getShowSheetTabs());
+ self::assertTrue($workbook->getAutoFilterDateGrouping());
+ self::assertFalse($workbook->getMinimized());
+ self::assertSame(0, $workbook->getFirstSheetIndex());
+ self::assertSame(600, $workbook->getTabRatio());
+ self::assertSame(Spreadsheet::VISIBILITY_VISIBLE, $workbook->getVisibility());
// Set the bookview attributes to non-default values
$workbook->setShowHorizontalScroll(false);
@@ -56,25 +56,25 @@ class WorkbookViewAttributesTest extends AbstractFunctional
$workbook->setVisibility(Spreadsheet::VISIBILITY_HIDDEN);
// Check that bookview attributes were set properly
- $this->assertFalse($workbook->getShowHorizontalScroll());
- $this->assertFalse($workbook->getShowVerticalScroll());
- $this->assertFalse($workbook->getShowSheetTabs());
- $this->assertFalse($workbook->getAutoFilterDateGrouping());
- $this->assertTrue($workbook->getMinimized());
- $this->assertSame(1, $workbook->getFirstSheetIndex());
- $this->assertSame(700, $workbook->getTabRatio());
- $this->assertSame(Spreadsheet::VISIBILITY_HIDDEN, $workbook->getVisibility());
+ self::assertFalse($workbook->getShowHorizontalScroll());
+ self::assertFalse($workbook->getShowVerticalScroll());
+ self::assertFalse($workbook->getShowSheetTabs());
+ self::assertFalse($workbook->getAutoFilterDateGrouping());
+ self::assertTrue($workbook->getMinimized());
+ self::assertSame(1, $workbook->getFirstSheetIndex());
+ self::assertSame(700, $workbook->getTabRatio());
+ self::assertSame(Spreadsheet::VISIBILITY_HIDDEN, $workbook->getVisibility());
$workbook2 = $this->writeAndReload($workbook, $format);
// Check that the read spreadsheet has the right bookview attributes
- $this->assertFalse($workbook2->getShowHorizontalScroll());
- $this->assertFalse($workbook2->getShowVerticalScroll());
- $this->assertFalse($workbook2->getShowSheetTabs());
- $this->assertFalse($workbook2->getAutoFilterDateGrouping());
- $this->assertTrue($workbook2->getMinimized());
- $this->assertSame(1, $workbook2->getFirstSheetIndex());
- $this->assertSame(700, $workbook2->getTabRatio());
- $this->assertSame(Spreadsheet::VISIBILITY_HIDDEN, $workbook2->getVisibility());
+ self::assertFalse($workbook2->getShowHorizontalScroll());
+ self::assertFalse($workbook2->getShowVerticalScroll());
+ self::assertFalse($workbook2->getShowSheetTabs());
+ self::assertFalse($workbook2->getAutoFilterDateGrouping());
+ self::assertTrue($workbook2->getMinimized());
+ self::assertSame(1, $workbook2->getFirstSheetIndex());
+ self::assertSame(700, $workbook2->getTabRatio());
+ self::assertSame(Spreadsheet::VISIBILITY_HIDDEN, $workbook2->getVisibility());
}
}
diff --git a/tests/PhpSpreadsheetTests/Helper/HtmlTest.php b/tests/PhpSpreadsheetTests/Helper/HtmlTest.php
index b15a7285..d47c2f64 100644
--- a/tests/PhpSpreadsheetTests/Helper/HtmlTest.php
+++ b/tests/PhpSpreadsheetTests/Helper/HtmlTest.php
@@ -13,7 +13,7 @@ class HtmlTest extends TestCase
* @param mixed $expected
* @param mixed $input
*/
- public function testUtf8EncodingSupport($expected, $input)
+ public function testUtf8EncodingSupport($expected, $input): void
{
$html = new Html();
$actual = $html->toRichTextObject($input);
diff --git a/tests/PhpSpreadsheetTests/Helper/MigratorTest.php b/tests/PhpSpreadsheetTests/Helper/MigratorTest.php
deleted file mode 100644
index a64cda80..00000000
--- a/tests/PhpSpreadsheetTests/Helper/MigratorTest.php
+++ /dev/null
@@ -1,88 +0,0 @@
-getMapping() as $classname) {
- if (substr_count($classname, '\\')) {
- self::assertTrue(class_exists($classname) || interface_exists($classname), 'mapping is wrong, class does not exists in project: ' . $classname);
- }
- }
- }
-
- public function testReplace()
- {
- $input = <<<'STRING'
-do();
- $fooobjPHPExcel->do();
- $objPHPExcel->do();
- $this->objPHPExcel->do();
- $this->PHPExcel->do();
-
- return \PHPExcel_Cell::stringFromColumnIndex(9);
- }
-}
-STRING;
-
- $expected = <<<'STRING'
-do();
- $fooobjPHPExcel->do();
- $objPHPExcel->do();
- $this->objPHPExcel->do();
- $this->PHPExcel->do();
-
- return \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(9);
- }
-}
-STRING;
-
- $migrator = new Migrator();
- self::assertSame($expected, $migrator->replace($input));
- }
-}
diff --git a/tests/PhpSpreadsheetTests/Helper/SampleTest.php b/tests/PhpSpreadsheetTests/Helper/SampleTest.php
index e97941f7..eb729dc9 100644
--- a/tests/PhpSpreadsheetTests/Helper/SampleTest.php
+++ b/tests/PhpSpreadsheetTests/Helper/SampleTest.php
@@ -14,10 +14,10 @@ class SampleTest extends TestCase
*
* @param mixed $sample
*/
- public function testSample($sample)
+ public function testSample($sample): void
{
// Suppress output to console
- $this->setOutputCallback(function () {
+ $this->setOutputCallback(function (): void {
});
require $sample;
@@ -48,7 +48,7 @@ class SampleTest extends TestCase
foreach ($helper->getSamples() as $samples) {
foreach ($samples as $sample) {
if (!in_array($sample, $skipped)) {
- $file = '../samples/' . $sample;
+ $file = 'samples/' . $sample;
$result[] = [$file];
}
}
diff --git a/tests/PhpSpreadsheetTests/IOFactoryTest.php b/tests/PhpSpreadsheetTests/IOFactoryTest.php
index 90a9af0e..983ba35e 100644
--- a/tests/PhpSpreadsheetTests/IOFactoryTest.php
+++ b/tests/PhpSpreadsheetTests/IOFactoryTest.php
@@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheetTests;
+use InvalidArgumentException;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
@@ -16,7 +17,7 @@ class IOFactoryTest extends TestCase
* @param string $name
* @param string $expected
*/
- public function testCreateWriter($name, $expected)
+ public function testCreateWriter($name, $expected): void
{
$spreadsheet = new Spreadsheet();
$actual = IOFactory::createWriter($spreadsheet, $name);
@@ -37,7 +38,7 @@ class IOFactoryTest extends TestCase
];
}
- public function testRegisterWriter()
+ public function testRegisterWriter(): void
{
IOFactory::registerWriter('Pdf', Writer\Pdf\Mpdf::class);
$spreadsheet = new Spreadsheet();
@@ -51,7 +52,7 @@ class IOFactoryTest extends TestCase
* @param string $name
* @param string $expected
*/
- public function testCreateReader($name, $expected)
+ public function testCreateReader($name, $expected): void
{
$actual = IOFactory::createReader($name);
self::assertInstanceOf($expected, $actual);
@@ -71,7 +72,7 @@ class IOFactoryTest extends TestCase
];
}
- public function testRegisterReader()
+ public function testRegisterReader(): void
{
IOFactory::registerReader('Custom', Reader\Html::class);
$actual = IOFactory::createReader('Custom');
@@ -85,7 +86,7 @@ class IOFactoryTest extends TestCase
* @param string $expectedName
* @param string $expectedClass
*/
- public function testIdentify($file, $expectedName, $expectedClass)
+ public function testIdentify($file, $expectedName, $expectedClass): void
{
$actual = IOFactory::identify($file);
self::assertSame($expectedName, $actual);
@@ -98,7 +99,7 @@ class IOFactoryTest extends TestCase
* @param string $expectedName
* @param string $expectedClass
*/
- public function testCreateReaderForFile($file, $expectedName, $expectedClass)
+ public function testCreateReaderForFile($file, $expectedName, $expectedClass): void
{
$actual = IOFactory::createReaderForFile($file);
self::assertInstanceOf($expectedClass, $actual);
@@ -111,7 +112,7 @@ class IOFactoryTest extends TestCase
* @param string $expectedName
* @param string $expectedClass
*/
- public function testLoad($file, $expectedName, $expectedClass)
+ public function testLoad($file, $expectedName, $expectedClass): void
{
$actual = IOFactory::load($file);
self::assertInstanceOf(Spreadsheet::class, $actual);
@@ -120,38 +121,38 @@ class IOFactoryTest extends TestCase
public function providerIdentify()
{
return [
- ['../samples/templates/26template.xlsx', 'Xlsx', Reader\Xlsx::class],
- ['../samples/templates/GnumericTest.gnumeric', 'Gnumeric', Reader\Gnumeric::class],
- ['../samples/templates/30template.xls', 'Xls', Reader\Xls::class],
- ['../samples/templates/OOCalcTest.ods', 'Ods', Reader\Ods::class],
- ['../samples/templates/SylkTest.slk', 'Slk', Reader\Slk::class],
- ['../samples/templates/Excel2003XMLTest.xml', 'Xml', Reader\Xml::class],
- ['../samples/templates/46readHtml.html', 'Html', Reader\Html::class],
+ ['samples/templates/26template.xlsx', 'Xlsx', Reader\Xlsx::class],
+ ['samples/templates/GnumericTest.gnumeric', 'Gnumeric', Reader\Gnumeric::class],
+ ['samples/templates/30template.xls', 'Xls', Reader\Xls::class],
+ ['samples/templates/OOCalcTest.ods', 'Ods', Reader\Ods::class],
+ ['samples/templates/SylkTest.slk', 'Slk', Reader\Slk::class],
+ ['samples/templates/Excel2003XMLTest.xml', 'Xml', Reader\Xml::class],
+ ['samples/templates/46readHtml.html', 'Html', Reader\Html::class],
];
}
- public function testIdentifyNonExistingFileThrowException()
+ public function testIdentifyNonExistingFileThrowException(): void
{
- $this->expectException(\InvalidArgumentException::class);
+ $this->expectException(InvalidArgumentException::class);
IOFactory::identify('/non/existing/file');
}
- public function testIdentifyExistingDirectoryThrowExceptions()
+ public function testIdentifyExistingDirectoryThrowExceptions(): void
{
- $this->expectException(\InvalidArgumentException::class);
+ $this->expectException(InvalidArgumentException::class);
IOFactory::identify('.');
}
- public function testRegisterInvalidWriter()
+ public function testRegisterInvalidWriter(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Writer\Exception::class);
IOFactory::registerWriter('foo', 'bar');
}
- public function testRegisterInvalidReader()
+ public function testRegisterInvalidReader(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
diff --git a/tests/PhpSpreadsheetTests/Reader/CondNumFmtTest.php b/tests/PhpSpreadsheetTests/Reader/CondNumFmtTest.php
index ca69d23b..c7474c6a 100644
--- a/tests/PhpSpreadsheetTests/Reader/CondNumFmtTest.php
+++ b/tests/PhpSpreadsheetTests/Reader/CondNumFmtTest.php
@@ -8,9 +8,9 @@ use PHPUnit\Framework\TestCase;
class CondNumFmtTest extends TestCase
{
- public function testLoadCondNumFmt()
+ public function testLoadCondNumFmt(): void
{
- $filename = './data/Reader/XLSX/condfmtnum.xlsx';
+ $filename = 'tests/data/Reader/XLSX/condfmtnum.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);
diff --git a/tests/PhpSpreadsheetTests/Reader/CsvContiguousFilter.php b/tests/PhpSpreadsheetTests/Reader/CsvContiguousFilter.php
new file mode 100644
index 00000000..9bc16ae0
--- /dev/null
+++ b/tests/PhpSpreadsheetTests/Reader/CsvContiguousFilter.php
@@ -0,0 +1,57 @@
+startRow = $startRow;
+ $this->endRow = $startRow + $chunkSize;
+ }
+
+ public function setFilterType($type): void
+ {
+ $this->filterType = $type;
+ }
+
+ public function filter1($row)
+ {
+ // Include rows 1-10, followed by 100-110, etc.
+ return $row % 100 <= 10;
+ }
+
+ public function filter0($row)
+ {
+ // Only read the heading row, and the rows that are configured in $this->_startRow and $this->_endRow
+ if (($row == 1) || ($row >= $this->startRow && $row < $this->endRow)) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public function readCell($column, $row, $worksheetName = '')
+ {
+ if ($this->filterType == 1) {
+ return $this->filter1($row);
+ }
+
+ return $this->filter0($row);
+ }
+}
diff --git a/tests/PhpSpreadsheetTests/Reader/CsvContiguousTest.php b/tests/PhpSpreadsheetTests/Reader/CsvContiguousTest.php
new file mode 100644
index 00000000..3a417791
--- /dev/null
+++ b/tests/PhpSpreadsheetTests/Reader/CsvContiguousTest.php
@@ -0,0 +1,81 @@
+getContiguous());
+ $reader->setReadFilter($chunkFilter)
+ ->setContiguous(true);
+
+ // Instantiate a new PhpSpreadsheet object manually
+ $spreadsheet = new Spreadsheet();
+
+ // Set a sheet index
+ $sheet = 0;
+ // Loop to read our worksheet in "chunk size" blocks
+ /** $startRow is set to 2 initially because we always read the headings in row #1 * */
+ for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) {
+ // Tell the Read Filter, the limits on which rows we want to read this iteration
+ $chunkFilter->setRows($startRow, $chunkSize);
+
+ // Increment the worksheet index pointer for the Reader
+ $reader->setSheetIndex($sheet);
+ // Load only the rows that match our filter into a new worksheet in the PhpSpreadsheet Object
+ $reader->loadIntoExisting($this->inputFileName, $spreadsheet);
+ // Set the worksheet title (to reference the "sheet" of data that we've loaded)
+ // and increment the sheet index as well
+ $spreadsheet->getActiveSheet()->setTitle('Country Data #' . (++$sheet));
+ }
+
+ $sheet = $spreadsheet->getSheetByName('Country Data #1');
+ self::assertEquals('Kabul', $sheet->getCell('A2')->getValue());
+ $sheet = $spreadsheet->getSheetByName('Country Data #2');
+ self::assertEquals('Lesotho', $sheet->getCell('B4')->getValue());
+ $sheet = $spreadsheet->getSheetByName('Country Data #3');
+ self::assertEquals(-20.1, $sheet->getCell('C6')->getValue());
+ }
+
+ public function testContiguous2(): void
+ {
+ // Create a new Reader of the type defined in $inputFileType
+ $reader = new Csv();
+
+ // Create a new Instance of our Read Filter
+ $chunkFilter = new CsvContiguousFilter();
+ $chunkFilter->setFilterType(1);
+
+ // Tell the Reader that we want to use the Read Filter that we've Instantiated
+ // and that we want to store it in contiguous rows/columns
+ $reader->setReadFilter($chunkFilter)
+ ->setContiguous(true);
+
+ // Instantiate a new PhpSpreadsheet object manually
+ $spreadsheet = new Spreadsheet();
+
+ // Loop to read our worksheet in "chunk size" blocks
+ $reader->loadIntoExisting($this->inputFileName, $spreadsheet);
+
+ $sheet = $spreadsheet->getActiveSheet();
+ self::assertEquals('Kabul', $sheet->getCell('A2')->getValue());
+ self::assertEquals('Kuwait', $sheet->getCell('B11')->getValue());
+ }
+}
diff --git a/tests/PhpSpreadsheetTests/Reader/CsvTest.php b/tests/PhpSpreadsheetTests/Reader/CsvTest.php
index be08f6a6..cb2b6196 100644
--- a/tests/PhpSpreadsheetTests/Reader/CsvTest.php
+++ b/tests/PhpSpreadsheetTests/Reader/CsvTest.php
@@ -3,6 +3,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Reader;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
+use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
use PHPUnit\Framework\TestCase;
class CsvTest extends TestCase
@@ -15,7 +16,7 @@ class CsvTest extends TestCase
* @param string $cell
* @param float|int|string $expectedValue
*/
- public function testDelimiterDetection($filename, $expectedDelimiter, $cell, $expectedValue)
+ public function testDelimiterDetection($filename, $expectedDelimiter, $cell, $expectedValue): void
{
$reader = new Csv();
self::assertNull($reader->getDelimiter());
@@ -32,55 +33,55 @@ class CsvTest extends TestCase
{
return [
[
- __DIR__ . '/../../data/Reader/CSV/enclosure.csv',
+ 'tests/data/Reader/CSV/enclosure.csv',
',',
'C4',
'username2',
],
[
- __DIR__ . '/../../data/Reader/CSV/semicolon_separated.csv',
+ 'tests/data/Reader/CSV/semicolon_separated.csv',
';',
'C2',
'25,5',
],
[
- __DIR__ . '/../../data/Reader/CSV/line_break_in_enclosure.csv',
+ 'tests/data/Reader/CSV/line_break_in_enclosure.csv',
',',
'A3',
'Test',
],
[
- __DIR__ . '/../../data/Reader/CSV/line_break_in_enclosure_with_escaped_quotes.csv',
+ 'tests/data/Reader/CSV/line_break_in_enclosure_with_escaped_quotes.csv',
',',
'A3',
'Test',
],
[
- __DIR__ . '/../../data/Reader/HTML/csv_with_angle_bracket.csv',
+ 'tests/data/Reader/HTML/csv_with_angle_bracket.csv',
',',
'B1',
'Number of items with weight <= 50kg',
],
[
- __DIR__ . '/../../../samples/Reader/sampleData/example1.csv',
+ 'samples/Reader/sampleData/example1.csv',
',',
'I4',
'100%',
],
[
- __DIR__ . '/../../../samples/Reader/sampleData/example2.csv',
+ 'samples/Reader/sampleData/example2.csv',
',',
'D8',
-58.373161,
],
[
- 'data/Reader/CSV/empty.csv',
+ 'tests/data/Reader/CSV/empty.csv',
',',
'A1',
null,
],
[
- 'data/Reader/CSV/no_delimiter.csv',
+ 'tests/data/Reader/CSV/no_delimiter.csv',
',',
'A1',
'SingleLine',
@@ -94,7 +95,7 @@ class CsvTest extends TestCase
* @param bool $expected
* @param string $filename
*/
- public function testCanLoad($expected, $filename)
+ public function testCanLoad($expected, $filename): void
{
$reader = new Csv();
self::assertSame($expected, $reader->canRead($filename));
@@ -103,23 +104,23 @@ class CsvTest extends TestCase
public function providerCanLoad()
{
return [
- [false, 'data/Reader/Ods/data.ods'],
- [false, 'data/Reader/Xml/WithoutStyle.xml'],
- [true, 'data/Reader/CSV/enclosure.csv'],
- [true, 'data/Reader/CSV/semicolon_separated.csv'],
- [true, 'data/Reader/CSV/contains_html.csv'],
- [true, 'data/Reader/CSV/csv_without_extension'],
- [true, 'data/Reader/HTML/csv_with_angle_bracket.csv'],
- [true, 'data/Reader/CSV/empty.csv'],
- [true, '../samples/Reader/sampleData/example1.csv'],
- [true, '../samples/Reader/sampleData/example2.csv'],
+ [false, 'tests/data/Reader/Ods/data.ods'],
+ [false, 'tests/data/Reader/Xml/WithoutStyle.xml'],
+ [true, 'tests/data/Reader/CSV/enclosure.csv'],
+ [true, 'tests/data/Reader/CSV/semicolon_separated.csv'],
+ [true, 'tests/data/Reader/CSV/contains_html.csv'],
+ [true, 'tests/data/Reader/CSV/csv_without_extension'],
+ [true, 'tests/data/Reader/HTML/csv_with_angle_bracket.csv'],
+ [true, 'tests/data/Reader/CSV/empty.csv'],
+ [true, 'samples/Reader/sampleData/example1.csv'],
+ [true, 'samples/Reader/sampleData/example2.csv'],
];
}
- public function testEscapeCharacters()
+ public function testEscapeCharacters(): void
{
$reader = (new Csv())->setEscapeCharacter('"');
- $worksheet = $reader->load(__DIR__ . '/../../data/Reader/CSV/backslash.csv')
+ $worksheet = $reader->load('tests/data/Reader/CSV/backslash.csv')
->getActiveSheet();
$expected = [
@@ -127,7 +128,116 @@ class CsvTest extends TestCase
['field 3\\', 'field 4'],
];
- $this->assertSame('"', $reader->getEscapeCharacter());
- $this->assertSame($expected, $worksheet->toArray());
+ self::assertSame('"', $reader->getEscapeCharacter());
+ self::assertSame($expected, $worksheet->toArray());
+ }
+
+ /**
+ * @dataProvider providerEncodings
+ *
+ * @param string $filename
+ * @param string $encoding
+ */
+ public function testEncodings($filename, $encoding): void
+ {
+ $reader = new Csv();
+ $reader->setInputEncoding($encoding);
+ $spreadsheet = $reader->load($filename);
+ $sheet = $spreadsheet->getActiveSheet();
+ self::assertEquals('Ã…', $sheet->getCell('A1')->getValue());
+ }
+
+ public function testInvalidWorkSheetInfo(): void
+ {
+ $this->expectException(ReaderException::class);
+ $reader = new Csv();
+ $reader->listWorksheetInfo('');
+ }
+
+ /**
+ * @dataProvider providerEncodings
+ *
+ * @param string $filename
+ * @param string $encoding
+ */
+ public function testWorkSheetInfo($filename, $encoding): void
+ {
+ $reader = new Csv();
+ $reader->setInputEncoding($encoding);
+ $info = $reader->listWorksheetInfo($filename);
+ self::assertEquals('Worksheet', $info[0]['worksheetName']);
+ self::assertEquals('B', $info[0]['lastColumnLetter']);
+ self::assertEquals(1, $info[0]['lastColumnIndex']);
+ self::assertEquals(2, $info[0]['totalRows']);
+ self::assertEquals(2, $info[0]['totalColumns']);
+ }
+
+ public function providerEncodings()
+ {
+ return [
+ ['tests/data/Reader/CSV/encoding.iso88591.csv', 'ISO-8859-1'],
+ ['tests/data/Reader/CSV/encoding.utf8.csv', 'UTF-8'],
+ ['tests/data/Reader/CSV/encoding.utf8bom.csv', 'UTF-8'],
+ ['tests/data/Reader/CSV/encoding.utf16be.csv', 'UTF-16BE'],
+ ['tests/data/Reader/CSV/encoding.utf16le.csv', 'UTF-16LE'],
+ ['tests/data/Reader/CSV/encoding.utf32be.csv', 'UTF-32BE'],
+ ['tests/data/Reader/CSV/encoding.utf32le.csv', 'UTF-32LE'],
+ ];
+ }
+
+ public function testUtf16LineBreak(): void
+ {
+ $reader = new Csv();
+ $reader->setInputEncoding('UTF-16BE');
+ $spreadsheet = $reader->load('tests/data/Reader/CSV/utf16be.line_break_in_enclosure.csv');
+ $sheet = $spreadsheet->getActiveSheet();
+ $expected = <<