<?php
namespace Tests\Unit\Excel;
use PHPUnit\Framework\TestCase;
use YouHuJun\Tool\App\Facades\V1\Excel\ExcelFacade;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as WriteXlsx;
class ExcelFacadeReadTest extends TestCase
{
private $tempFile;
protected function setUp(): void
{
parent::setUp();
$this->tempFile = tempnam(sys_get_temp_dir(), 'test_excel_read_') . '.xlsx';
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', '姓名');
$sheet->setCellValue('B1', '年龄');
$sheet->setCellValue('A2', '王五');
$sheet->setCellValue('B2', 28);
$writer = new WriteXlsx($spreadsheet);
$writer->save($this->tempFile);
}
protected function tearDown(): void
{
parent::tearDown();
if (file_exists($this->tempFile)) {
unlink($this->tempFile);
}
}
public function testReadExcelData()
{
ExcelFacade::initReadExcel($this->tempFile);
ExcelFacade::setWorkSheet(0);
$result = ExcelFacade::getDataByRow();
array_shift($result);
$expectedData = [
0 => [0 => '王五', 1 => '28'],
];
$this->assertEquals($expectedData, $result, '通过门面读取 Excel 数据与预期不符。');
}
}