1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Document\Properties; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\File; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Style; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
class XlsxTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function testLoadXlsxWorkbookProperties() |
18
|
|
|
{ |
19
|
|
|
$customPropertySet = [ |
20
|
|
|
'Publisher' => ['type' => Properties::PROPERTY_TYPE_STRING, 'value' => 'PHPOffice Suite'], |
21
|
|
|
'Tested' => ['type' => Properties::PROPERTY_TYPE_BOOLEAN, 'value' => true], |
22
|
|
|
'Counter' => ['type' => Properties::PROPERTY_TYPE_INTEGER, 'value' => 15], |
23
|
|
|
'Rate' => ['type' => Properties::PROPERTY_TYPE_FLOAT, 'value' => 1.15], |
24
|
|
|
'Refactor Date' => ['type' => Properties::PROPERTY_TYPE_DATE, 'value' => '2019-06-10'], |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
$filename = './data/Reader/XLSX/propertyTest.xlsx'; |
28
|
|
|
$reader = new Xlsx(); |
29
|
|
|
$spreadsheet = $reader->load($filename); |
30
|
|
|
|
31
|
|
|
$properties = $spreadsheet->getProperties(); |
32
|
|
|
// Core Properties |
33
|
|
|
$this->assertSame('Mark Baker', $properties->getCreator()); |
34
|
|
|
$this->assertSame('Unit Testing', $properties->getTitle()); |
35
|
|
|
$this->assertSame('Property Test', $properties->getSubject()); |
36
|
|
|
|
37
|
|
|
// Extended Properties |
38
|
|
|
$this->assertSame('PHPOffice', $properties->getCompany()); |
39
|
|
|
$this->assertSame('The Big Boss', $properties->getManager()); |
40
|
|
|
|
41
|
|
|
// Custom Properties |
42
|
|
|
$customProperties = $properties->getCustomProperties(); |
43
|
|
|
$this->assertInternalType('array', $customProperties); |
44
|
|
|
$customProperties = array_flip($customProperties); |
45
|
|
|
$this->assertArrayHasKey('Publisher', $customProperties); |
46
|
|
|
|
47
|
|
|
foreach ($customPropertySet as $propertyName => $testData) { |
48
|
|
|
$this->assertTrue($properties->isCustomPropertySet($propertyName)); |
49
|
|
|
$this->assertSame($testData['type'], $properties->getCustomPropertyType($propertyName)); |
50
|
|
|
if ($properties->getCustomPropertyType($propertyName) == Properties::PROPERTY_TYPE_DATE) { |
51
|
|
|
$this->assertSame($testData['value'], date('Y-m-d', $properties->getCustomPropertyValue($propertyName))); |
52
|
|
|
} else { |
53
|
|
|
$this->assertSame($testData['value'], $properties->getCustomPropertyValue($propertyName)); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testLoadXlsxRowColumnAttributes() |
59
|
|
|
{ |
60
|
|
|
$filename = './data/Reader/XLSX/rowColumnAttributeTest.xlsx'; |
61
|
|
|
$reader = new Xlsx(); |
62
|
|
|
$spreadsheet = $reader->load($filename); |
63
|
|
|
|
64
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
65
|
|
|
for ($row = 1; $row <= 4; ++$row) { |
66
|
|
|
$this->assertEquals($row * 5 + 10, floor($worksheet->getRowDimension($row)->getRowHeight())); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->assertFalse($worksheet->getRowDimension(5)->getVisible()); |
70
|
|
|
|
71
|
|
|
for ($column = 1; $column <= 4; ++$column) { |
72
|
|
|
$columnAddress = Coordinate::stringFromColumnIndex($column); |
73
|
|
|
$this->assertEquals( |
74
|
|
|
$column * 2 + 2, |
75
|
|
|
floor($worksheet->getColumnDimension($columnAddress)->getWidth()) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->assertFalse($worksheet->getColumnDimension('E')->getVisible()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testLoadXlsxWithStyles() |
83
|
|
|
{ |
84
|
|
|
$expectedColours = [ |
85
|
|
|
1 => ['A' => 'C00000', 'C' => 'FF0000', 'E' => 'FFC000'], |
86
|
|
|
3 => ['A' => '7030A0', 'C' => '000000', 'E' => 'FFFF00'], |
87
|
|
|
5 => ['A' => '002060', 'C' => '000000', 'E' => '92D050'], |
88
|
|
|
7 => ['A' => '0070C0', 'C' => '00B0F0', 'E' => '00B050'], |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
$filename = './data/Reader/XLSX/stylesTest.xlsx'; |
92
|
|
|
$reader = new Xlsx(); |
93
|
|
|
$spreadsheet = $reader->load($filename); |
94
|
|
|
|
95
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
96
|
|
|
for ($row = 1; $row <= 8; $row += 2) { |
97
|
|
|
for ($column = 'A'; $column !== 'G'; ++$column, ++$column) { |
98
|
|
|
$this->assertEquals( |
99
|
|
|
$expectedColours[$row][$column], |
100
|
|
|
$worksheet->getStyle($column . $row)->getFill()->getStartColor()->getRGB() |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testLoadXlsxAutofilter() |
107
|
|
|
{ |
108
|
|
|
$filename = './data/Reader/XLSX/autofilterTest.xlsx'; |
109
|
|
|
$reader = new Xlsx(); |
110
|
|
|
$spreadsheet = $reader->load($filename); |
111
|
|
|
|
112
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
113
|
|
|
|
114
|
|
|
$autofilter = $worksheet->getAutoFilter(); |
115
|
|
|
$this->assertInstanceOf(AutoFilter::class, $autofilter); |
116
|
|
|
$this->assertEquals('A1:D57', $autofilter->getRange()); |
117
|
|
|
$this->assertEquals( |
118
|
|
|
AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER, |
119
|
|
|
$autofilter->getColumn('A')->getFilterType() |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testLoadXlsxPageSetup() |
124
|
|
|
{ |
125
|
|
|
$filename = './data/Reader/XLSX/pageSetupTest.xlsx'; |
126
|
|
|
$reader = new Xlsx(); |
127
|
|
|
$spreadsheet = $reader->load($filename); |
128
|
|
|
|
129
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
130
|
|
|
|
131
|
|
|
$pageMargins = $worksheet->getPageMargins(); |
132
|
|
|
// Convert from inches to cm for testing |
133
|
|
|
$this->assertEquals(2.5, $pageMargins->getTop() * 2.54); |
134
|
|
|
$this->assertEquals(3.3, $pageMargins->getLeft() * 2.54); |
135
|
|
|
$this->assertEquals(3.3, $pageMargins->getRight() * 2.54); |
136
|
|
|
$this->assertEquals(1.3, $pageMargins->getHeader() * 2.54); |
137
|
|
|
|
138
|
|
|
$this->assertEquals(PageSetup::PAPERSIZE_A4, $worksheet->getPageSetup()->getPaperSize()); |
139
|
|
|
$this->assertEquals(['A10', 'A20', 'A30', 'A40', 'A50'], array_keys($worksheet->getBreaks())); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testLoadXlsxConditionalFormatting() |
143
|
|
|
{ |
144
|
|
|
$filename = './data/Reader/XLSX/conditionalFormattingTest.xlsx'; |
145
|
|
|
$reader = new Xlsx(); |
146
|
|
|
$spreadsheet = $reader->load($filename); |
147
|
|
|
|
148
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
149
|
|
|
|
150
|
|
|
$conditionalStyle = $worksheet->getCell('B2')->getStyle()->getConditionalStyles(); |
151
|
|
|
|
152
|
|
|
$this->assertNotEmpty($conditionalStyle); |
153
|
|
|
$conditionalRule = $conditionalStyle[0]; |
154
|
|
|
$this->assertNotEmpty($conditionalRule->getConditions()); |
155
|
|
|
$this->assertEquals(Conditional::CONDITION_CELLIS, $conditionalRule->getConditionType()); |
156
|
|
|
$this->assertEquals(Conditional::OPERATOR_BETWEEN, $conditionalRule->getOperatorType()); |
157
|
|
|
$this->assertEquals(['200', '400'], $conditionalRule->getConditions()); |
158
|
|
|
$this->assertInstanceOf(Style::class, $conditionalRule->getStyle()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testLoadXlsxDataValidation() |
162
|
|
|
{ |
163
|
|
|
$filename = './data/Reader/XLSX/dataValidationTest.xlsx'; |
164
|
|
|
$reader = new Xlsx(); |
165
|
|
|
$spreadsheet = $reader->load($filename); |
166
|
|
|
|
167
|
|
|
$worksheet = $spreadsheet->getActiveSheet(); |
168
|
|
|
|
169
|
|
|
$this->assertTrue($worksheet->getCell('B3')->hasDataValidation()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Test load Xlsx file without cell reference. |
174
|
|
|
* |
175
|
|
|
* @doesNotPerformAssertions |
176
|
|
|
*/ |
177
|
|
|
public function testLoadXlsxWithoutCellReference() |
178
|
|
|
{ |
179
|
|
|
$filename = './data/Reader/XLSX/without_cell_reference.xlsx'; |
180
|
|
|
$reader = new Xlsx(); |
181
|
|
|
$reader->load($filename); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Test load Xlsx file and use a read filter. |
186
|
|
|
*/ |
187
|
|
|
public function testLoadWithReadFilter() |
188
|
|
|
{ |
189
|
|
|
$filename = './data/Reader/XLSX/without_cell_reference.xlsx'; |
190
|
|
|
$reader = new Xlsx(); |
191
|
|
|
$reader->setReadFilter(new OddColumnReadFilter()); |
192
|
|
|
$data = $reader->load($filename)->getActiveSheet()->toArray(); |
193
|
|
|
$ref = [1.0, null, 3.0, null, 5.0, null, 7.0, null, 9.0, null]; |
194
|
|
|
|
195
|
|
|
for ($i = 0; $i < 10; ++$i) { |
196
|
|
|
$this->assertEquals($ref, \array_slice($data[$i], 0, 10, true)); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Test load Xlsx file with drawing having double attributes. |
202
|
|
|
* |
203
|
|
|
* @doesNotPerformAssertions |
204
|
|
|
*/ |
205
|
|
|
public function testLoadXlsxWithDoubleAttrDrawing() |
206
|
|
|
{ |
207
|
|
|
if (version_compare(PHP_VERSION, '7.0.0', '<')) { |
208
|
|
|
$this->markTestSkipped('Only handled in PHP version >= 7.0.0'); |
209
|
|
|
} |
210
|
|
|
$filename = './data/Reader/XLSX/double_attr_drawing.xlsx'; |
211
|
|
|
$reader = new Xlsx(); |
212
|
|
|
$reader->load($filename); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Test correct save and load xlsx files with empty drawings. |
217
|
|
|
* Such files can be generated by Google Sheets. |
218
|
|
|
*/ |
219
|
|
|
public function testLoadSaveWithEmptyDrawings() |
220
|
|
|
{ |
221
|
|
|
$filename = __DIR__ . '/../../data/Reader/XLSX/empty_drawing.xlsx'; |
222
|
|
|
$reader = new Xlsx(); |
223
|
|
|
$excel = $reader->load($filename); |
224
|
|
|
$resultFilename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test'); |
225
|
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($excel); |
226
|
|
|
$writer->save($resultFilename); |
227
|
|
|
$excel = $reader->load($resultFilename); |
228
|
|
|
// Fake assert. The only thing we need is to ensure the file is loaded without exception |
229
|
|
|
$this->assertNotNull($excel); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|