|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace avadim\FastExcelTemplator; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use avadim\FastExcelTemplator\Excel; |
| 9 | + |
| 10 | +final class FastExcelTemplatorTest extends TestCase |
| 11 | +{ |
| 12 | + const DEMO_DIR = __DIR__ . '/../demo/files/'; |
| 13 | + |
| 14 | + public function test01() |
| 15 | + { |
| 16 | + // ===================== |
| 17 | + $tpl = self::DEMO_DIR . 'demo1-tpl.xlsx'; |
| 18 | + $out = __DIR__ . '/demo1-out.xlsx'; |
| 19 | + |
| 20 | + $excel = Excel::template($tpl, $out); |
| 21 | + $sheet = $excel->sheet(); |
| 22 | + |
| 23 | + $fillData = [ |
| 24 | + '{{COMPANY}}' => 'Comp Stock Shop', |
| 25 | + '{{ADDRESS}}' => '123 ABC Street', |
| 26 | + '{{CITY}}' => 'Peace City, TN', |
| 27 | + ]; |
| 28 | + $replaceData = ['{{BULK_QTY}}' => 12, '{{DATE}}' => date('m/d/Y')]; |
| 29 | + $list = [ |
| 30 | + [ |
| 31 | + 'number' => 'AA-8465-947563', |
| 32 | + 'description' => 'NEC Mate Type ML PC-MK29MLZD1FWG (Corei5-3470S/2GB/250GB/Multi/No OF/Win 11 Home)', |
| 33 | + 'price1' => 816, |
| 34 | + 'price2' => 683, |
| 35 | + ], |
| 36 | + [ |
| 37 | + 'number' => 'QR-7956-048914', |
| 38 | + 'description' => 'Acer Aspire TC-1760-UA92 (Core i5-12400/12GB 3200MHz DDR4/512GB SSD/Win 11 Home)', |
| 39 | + 'price1' => 414, |
| 40 | + 'price2' => 339, |
| 41 | + ], |
| 42 | + [ |
| 43 | + 'number' => 'BD-3966-285936', |
| 44 | + 'description' => 'Dell Optiplex 7050 SFF (Core i7-7700/32GB DDR4/1TB SSD/Win 10 Pro/Renewed)', |
| 45 | + 'price1' => 249, |
| 46 | + 'price2' => 198, |
| 47 | + ], |
| 48 | + ]; |
| 49 | + |
| 50 | + $sheet |
| 51 | + ->fill($fillData) |
| 52 | + ->replace($replaceData) |
| 53 | + ; |
| 54 | + $rowTemplate = $sheet->getRowTemplate(6); |
| 55 | + $count = 0; |
| 56 | + foreach ($list as $record) { |
| 57 | + $rowData = [ |
| 58 | + // In the column A wil be written value from field 'number' |
| 59 | + 'A' => $record['number'], |
| 60 | + // In the column B wil be written value from field 'description' |
| 61 | + 'B' => $record['description'], |
| 62 | + // And so on... |
| 63 | + 'C' => $record['price1'], |
| 64 | + 'D' => $record['price2'], |
| 65 | + ]; |
| 66 | + if ($count++ === 0) { |
| 67 | + // First we replace the row 6 (this is template row) |
| 68 | + $sheet->replaceRow(6, $rowTemplate, $rowData); |
| 69 | + } |
| 70 | + else { |
| 71 | + // Then we add new rows after last touched |
| 72 | + $sheet->insertRowAfterLast($rowTemplate, $rowData); |
| 73 | + } |
| 74 | + } |
| 75 | + $excel->save(); |
| 76 | + $this->assertTrue(is_file($out)); |
| 77 | + |
| 78 | + $excelReader = \avadim\FastExcelReader\Excel::open($out); |
| 79 | + $cells = $excelReader->readCells(); |
| 80 | + |
| 81 | + $this->assertEquals($fillData['{{COMPANY}}'], $cells['A1']); |
| 82 | + $this->assertEquals('*Bulk pricing applies to quantities of 12 or more', $cells['D3']); |
| 83 | + $this->assertEquals(249, $cells['C8']); |
| 84 | + |
| 85 | + unlink($out); |
| 86 | + } |
| 87 | + |
| 88 | +} |
| 89 | + |
0 commit comments