diff --git a/modules/EcommerceFinance/Services/PayablesWorkbookStreamReader.php b/modules/EcommerceFinance/Services/PayablesWorkbookStreamReader.php index d038b11d..3e0e29e3 100644 --- a/modules/EcommerceFinance/Services/PayablesWorkbookStreamReader.php +++ b/modules/EcommerceFinance/Services/PayablesWorkbookStreamReader.php @@ -136,6 +136,10 @@ class PayablesWorkbookStreamReader $depth = $reader->depth; $empty = $reader->isEmptyElement; if ($empty) { + // A self-closing must be consumed before the outer loop + // evaluates the current node again, otherwise this row repeats forever. + $reader->read(); + return $cells; } diff --git a/tests/Feature/Business/PayablesFinalImportRunnerTest.php b/tests/Feature/Business/PayablesFinalImportRunnerTest.php index 33f95624..bef09864 100644 --- a/tests/Feature/Business/PayablesFinalImportRunnerTest.php +++ b/tests/Feature/Business/PayablesFinalImportRunnerTest.php @@ -66,6 +66,27 @@ class PayablesFinalImportRunnerTest extends FeatureTestCase } } + public function test_stream_reader_advances_past_self_closing_empty_xlsx_rows(): void + { + $path = $this->makeXlsxFile([ + self::HEADER, + ['2026-10', 'RK-1', '921979', '2026-10-10', '亿之航', 'DSG004820', 'DSG004820N1019V01', '款式四', '30', '83.5', '2505'], + [], + ['2026-10', 'RK-2', '941828', '2026-10-10', '亿之航', 'DSG004820', 'DSG004820N1019V01', '款式四', '30', '83.5', '2505'], + ]); + + try { + $reader = PayablesWorkbookStreamReader::forFile($path, 'xlsx'); + $rows = iterator_to_array($reader->rows(0), true); + + $this->assertSame([1, 2, 3, 4], array_keys($rows)); + $this->assertSame([], $rows[3]); + $this->assertSame('RK-2', $rows[4][1]); + } finally { + @unlink($path); + } + } + public function test_stream_reader_reads_gbk_csv(): void { $content = mb_convert_encoding(implode(',', self::HEADER) . "\n2026-09,RK-9,CG-9,2026-09-08,供应商九,X-9,P-9,规格甲,2,10,20,9,18,SC-1,备注甲,华东仓,顺丰,SF-9,未发出,对账人甲\n", 'GBK', 'UTF-8'); @@ -246,6 +267,11 @@ class PayablesFinalImportRunnerTest extends FeatureTestCase $sheet = ''; foreach ($rows as $rowIndex => $row) { + if ($row === []) { + $sheet .= ''; + continue; + } + $sheet .= ''; foreach ($row as $columnIndex => $value) { $cell = chr(65 + $columnIndex) . ($rowIndex + 1);