|
@@ -0,0 +1,101 @@
|
|
|
|
|
+<?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());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|