ExcelHelperTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Tests\Unit\Helpers;
  3. use App\Helpers\ExcelHelper;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PHPUnit\Framework\TestCase;
  6. class ExcelHelperTest extends TestCase
  7. {
  8. public function test_copy_rows_copies_cell_values(): void
  9. {
  10. $spreadsheet = new Spreadsheet();
  11. $sheet = $spreadsheet->getActiveSheet();
  12. $sheet->setCellValue('A1', 'Hello');
  13. $sheet->setCellValue('B1', 'World');
  14. $sheet->setCellValue('A2', 'Foo');
  15. $sheet->setCellValue('B2', 'Bar');
  16. ExcelHelper::copyRows($sheet, 'A1:B2', 'C1');
  17. $this->assertEquals('Hello', $sheet->getCell('C1')->getValue());
  18. $this->assertEquals('World', $sheet->getCell('D1')->getValue());
  19. $this->assertEquals('Foo', $sheet->getCell('C2')->getValue());
  20. $this->assertEquals('Bar', $sheet->getCell('D2')->getValue());
  21. }
  22. public function test_copy_rows_invalid_src_range_does_nothing(): void
  23. {
  24. $spreadsheet = new Spreadsheet();
  25. $sheet = $spreadsheet->getActiveSheet();
  26. $sheet->setCellValue('A1', 'Test');
  27. // Should not throw, just return silently
  28. ExcelHelper::copyRows($sheet, 'INVALID_RANGE', 'C1');
  29. $this->assertNull($sheet->getCell('C1')->getValue());
  30. }
  31. public function test_copy_rows_invalid_dst_cell_does_nothing(): void
  32. {
  33. $spreadsheet = new Spreadsheet();
  34. $sheet = $spreadsheet->getActiveSheet();
  35. $sheet->setCellValue('A1', 'Test');
  36. // Should not throw, just return silently
  37. ExcelHelper::copyRows($sheet, 'A1:B1', 'INVALID');
  38. $this->assertNull($sheet->getCell('C1')->getValue());
  39. }
  40. public function test_copy_rows_to_different_sheet(): void
  41. {
  42. $spreadsheet = new Spreadsheet();
  43. $srcSheet = $spreadsheet->getActiveSheet();
  44. $spreadsheet->createSheet();
  45. $dstSheet = $spreadsheet->getSheet(1);
  46. $srcSheet->setCellValue('A1', 'Source');
  47. $srcSheet->setCellValue('B1', 'Data');
  48. ExcelHelper::copyRows($srcSheet, 'A1:B1', 'A1', $dstSheet);
  49. $this->assertEquals('Source', $dstSheet->getCell('A1')->getValue());
  50. $this->assertEquals('Data', $dstSheet->getCell('B1')->getValue());
  51. }
  52. public function test_copy_style_xf_collection_transfers_styles(): void
  53. {
  54. $src = new Spreadsheet();
  55. $dst = new Spreadsheet();
  56. $srcSheet = $src->getActiveSheet();
  57. $srcSheet->getStyle('A1')->getFont()->setBold(true);
  58. $srcCount = count($src->getCellXfCollection());
  59. ExcelHelper::copyStyleXFCollection($src, $dst);
  60. $dstCount = count($dst->getCellXfCollection());
  61. $this->assertGreaterThanOrEqual($srcCount, $dstCount);
  62. }
  63. public function test_copy_rows_copies_single_row(): void
  64. {
  65. $spreadsheet = new Spreadsheet();
  66. $sheet = $spreadsheet->getActiveSheet();
  67. $sheet->setCellValue('A1', 100);
  68. $sheet->setCellValue('B1', 200);
  69. ExcelHelper::copyRows($sheet, 'A1:B1', 'D1');
  70. $this->assertEquals(100, $sheet->getCell('D1')->getValue());
  71. $this->assertEquals(200, $sheet->getCell('E1')->getValue());
  72. }
  73. }