| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace Tests\Unit\Helpers;
- use App\Helpers\ExcelHelper;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PHPUnit\Framework\TestCase;
- class ExcelHelperTest extends TestCase
- {
- public function test_copy_rows_copies_cell_values(): void
- {
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $sheet->setCellValue('A1', 'Hello');
- $sheet->setCellValue('B1', 'World');
- $sheet->setCellValue('A2', 'Foo');
- $sheet->setCellValue('B2', 'Bar');
- ExcelHelper::copyRows($sheet, 'A1:B2', 'C1');
- $this->assertEquals('Hello', $sheet->getCell('C1')->getValue());
- $this->assertEquals('World', $sheet->getCell('D1')->getValue());
- $this->assertEquals('Foo', $sheet->getCell('C2')->getValue());
- $this->assertEquals('Bar', $sheet->getCell('D2')->getValue());
- }
- public function test_copy_rows_invalid_src_range_does_nothing(): void
- {
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $sheet->setCellValue('A1', 'Test');
- // Should not throw, just return silently
- ExcelHelper::copyRows($sheet, 'INVALID_RANGE', 'C1');
- $this->assertNull($sheet->getCell('C1')->getValue());
- }
- public function test_copy_rows_invalid_dst_cell_does_nothing(): void
- {
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $sheet->setCellValue('A1', 'Test');
- // Should not throw, just return silently
- ExcelHelper::copyRows($sheet, 'A1:B1', 'INVALID');
- $this->assertNull($sheet->getCell('C1')->getValue());
- }
- public function test_copy_rows_to_different_sheet(): void
- {
- $spreadsheet = new Spreadsheet();
- $srcSheet = $spreadsheet->getActiveSheet();
- $spreadsheet->createSheet();
- $dstSheet = $spreadsheet->getSheet(1);
- $srcSheet->setCellValue('A1', 'Source');
- $srcSheet->setCellValue('B1', 'Data');
- ExcelHelper::copyRows($srcSheet, 'A1:B1', 'A1', $dstSheet);
- $this->assertEquals('Source', $dstSheet->getCell('A1')->getValue());
- $this->assertEquals('Data', $dstSheet->getCell('B1')->getValue());
- }
- public function test_copy_style_xf_collection_transfers_styles(): void
- {
- $src = new Spreadsheet();
- $dst = new Spreadsheet();
- $srcSheet = $src->getActiveSheet();
- $srcSheet->getStyle('A1')->getFont()->setBold(true);
- $srcCount = count($src->getCellXfCollection());
- ExcelHelper::copyStyleXFCollection($src, $dst);
- $dstCount = count($dst->getCellXfCollection());
- $this->assertGreaterThanOrEqual($srcCount, $dstCount);
- }
- public function test_copy_rows_copies_single_row(): void
- {
- $spreadsheet = new Spreadsheet();
- $sheet = $spreadsheet->getActiveSheet();
- $sheet->setCellValue('A1', 100);
- $sheet->setCellValue('B1', 200);
- ExcelHelper::copyRows($sheet, 'A1:B1', 'D1');
- $this->assertEquals(100, $sheet->getCell('D1')->getValue());
- $this->assertEquals(200, $sheet->getCell('E1')->getValue());
- }
- }
|