generated from krissss/package-skeleton-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExcelSheetSourceIteratorTest.php
111 lines (103 loc) · 4.08 KB
/
ExcelSheetSourceIteratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
use Kriss\DataExporter\DataExporter;
use Kriss\DataExporter\Source\ExcelSheetSourceIterator;
use Kriss\DataExporter\Source\GeneratorChainSourceIterator;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Sonata\Exporter\Source\ArraySourceIterator;
beforeEach(function () {
$this->filename = __DIR__ . '/../tmp/test';
});
it('Excel sheet: ExcelSheetSourceIterator', function () {
foreach ([
'xlsSpreadsheet', 'xlsxSpreadsheet', 'odsSpreadsheet',
'xlsxSpout', 'odsSpout',
] as $fn) {
$source1 = new ExcelSheetSourceIterator([
new ArrayIterator([
['aa', 'bb1'],
['aa', 'bb2'],
['aa', 'bb3'],
]),
new ArrayIterator([
['cc', 'dd4'],
['cc', 'dd5'],
['cc', 'dd6'],
]),
]);
$source2 = new ExcelSheetSourceIterator([
'CustomerSheetName1' => new ArrayIterator([
['aa', 'bb1'],
['aa', 'bb2'],
['aa', 'bb3'],
]),
'中文名' => new ArrayIterator([
['cc', 'dd4'],
['cc', 'dd5'],
['cc', 'dd6'],
]),
]);
$source3 = new ExcelSheetSourceIterator([
'withHeader' => new ArrayIterator([
['key1' => 'cc', 'name1' => 'aa', 'bb1'],
['aa', 'bb2'],
['aa', 'bb3'],
]),
'有表头' => new ArrayIterator([
['key2' => 'cc', 'name2' => 'dd4'],
['cc', 'dd5'],
['cc', 'dd6'],
]),
]);
$filename = DataExporter::$fn($source1)->saveAs($this->filename);
$factory = IOFactory::load($filename);
expect(count($factory->getAllSheets()))->toBe(2);
expect($factory->getActiveSheetIndex())->toBe(0);
expect((string)$factory->getActiveSheet()->getCell('B3')->getValue())->toBe('bb3');
$filename = DataExporter::$fn($source2)->saveAs($this->filename);
$factory = IOFactory::load($filename);
expect(count($factory->getAllSheets()))->toBe(2);
$factory->setActiveSheetIndexByName('中文名');
expect((string)$factory->getActiveSheet()->getCell('B3')->getValue())->toBe('dd6');
$filename = DataExporter::$fn($source3)->saveAs($this->filename);
$factory = IOFactory::load($filename);
expect(count($factory->getAllSheets()))->toBe(2);
$factory->setActiveSheetIndexByName('有表头');
expect((string)$factory->getActiveSheet()->getCell('A1')->getValue())->toBe('key2');
}
});
it('Excel sheet: GeneratorChainSourceIterator', function () {
foreach ([
'xlsSpreadsheet', 'xlsxSpreadsheet', 'odsSpreadsheet',
'xlsxSpout', 'odsSpout',
] as $fn) {
$source1 = new GeneratorChainSourceIterator(function () {
foreach ([
new ArrayIterator([
['aa', 'bb1'],
['aa', 'bb2'],
['aa', 'bb3'],
]),
new ArrayIterator([
['cc', 'dd4'],
['cc', 'dd5'],
['cc', 'dd6'],
]),
] as $iterator) {
yield $iterator;
}
});
$excelSource = new ExcelSheetSourceIterator([
'MySheet1' => new ArraySourceIterator([
['a'],
]),
'MySheet233' => $source1,
]);
$filename = DataExporter::$fn($excelSource)->saveAs($this->filename);
$factory = IOFactory::load($filename);
expect(count($factory->getAllSheets()))->toBe(2);
expect($factory->getActiveSheetIndex())->toBe(0);
$factory->setActiveSheetIndexByName('MySheet233');
expect($factory->getActiveSheet()->getHighestRow())->toBe(6);
expect((string)$factory->getActiveSheet()->getCell('B3')->getValue())->toBe('bb3');
}
});