CsvContiguousTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Reader\Csv;
  3. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PHPUnit\Framework\TestCase;
  6. class CsvContiguousTest extends TestCase
  7. {
  8. /**
  9. * @var string
  10. */
  11. private $inputFileName = 'samples/Reader/sampleData/example2.csv';
  12. public function testContiguous(): void
  13. {
  14. // Create a new Reader of the type defined in $inputFileType
  15. $reader = new Csv();
  16. // Define how many rows we want to read for each "chunk"
  17. $chunkSize = 100;
  18. // Create a new Instance of our Read Filter
  19. $chunkFilter = new CsvContiguousFilter();
  20. // Tell the Reader that we want to use the Read Filter that we've Instantiated
  21. // and that we want to store it in contiguous rows/columns
  22. self::assertFalse($reader->getContiguous());
  23. $reader->setReadFilter($chunkFilter);
  24. $reader->setContiguous(true);
  25. // Instantiate a new PhpSpreadsheet object manually
  26. $spreadsheet = new Spreadsheet();
  27. // Set a sheet index
  28. $sheet = 0;
  29. // Loop to read our worksheet in "chunk size" blocks
  30. /** $startRow is set to 2 initially because we always read the headings in row #1 * */
  31. for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) {
  32. // Tell the Read Filter, the limits on which rows we want to read this iteration
  33. $chunkFilter->setRows($startRow, $chunkSize);
  34. // Increment the worksheet index pointer for the Reader
  35. $reader->setSheetIndex($sheet);
  36. // Load only the rows that match our filter into a new worksheet in the PhpSpreadsheet Object
  37. $reader->loadIntoExisting($this->inputFileName, $spreadsheet);
  38. // Set the worksheet title (to reference the "sheet" of data that we've loaded)
  39. // and increment the sheet index as well
  40. $spreadsheet->getActiveSheet()->setTitle('Country Data #' . (++$sheet));
  41. }
  42. self::assertSame('Kabul', self::getCellValue($spreadsheet, 'Country Data #1', 'A2'));
  43. self::assertSame('Lesotho', self::getCellValue($spreadsheet, 'Country Data #2', 'B4'));
  44. self::assertSame('-20.1', self::getCellValue($spreadsheet, 'Country Data #3', 'C6'));
  45. }
  46. private static function getCellValue(Spreadsheet $spreadsheet, string $sheetName, string $cellAddress): string
  47. {
  48. $sheet = $spreadsheet->getSheetByNameOrThrow($sheetName);
  49. $result = '';
  50. $value = $sheet->getCell($cellAddress)->getValue();
  51. if (is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) {
  52. $result = (string) $value;
  53. }
  54. return $result;
  55. }
  56. public function testContiguous2(): void
  57. {
  58. // Create a new Reader of the type defined in $inputFileType
  59. $reader = new Csv();
  60. // Create a new Instance of our Read Filter
  61. $chunkFilter = new CsvContiguousFilter();
  62. $chunkFilter->setFilterType(1);
  63. // Tell the Reader that we want to use the Read Filter that we've Instantiated
  64. // and that we want to store it in contiguous rows/columns
  65. $reader->setReadFilter($chunkFilter);
  66. $reader->setContiguous(true);
  67. // Instantiate a new PhpSpreadsheet object manually
  68. $spreadsheet = new Spreadsheet();
  69. // Loop to read our worksheet in "chunk size" blocks
  70. $reader->loadIntoExisting($this->inputFileName, $spreadsheet);
  71. $sheet = $spreadsheet->getActiveSheet();
  72. self::assertEquals('Kabul', $sheet->getCell('A2')->getValue());
  73. self::assertEquals('Kuwait', $sheet->getCell('B11')->getValue());
  74. }
  75. }