-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
OdsTest.php
318 lines (241 loc) · 12 KB
/
OdsTest.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Document\Properties;
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
use PhpOffice\PhpSpreadsheet\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PHPUnit\Framework\TestCase;
/**
* @TODO The class doesn't read the bold/italic/underline properties (rich text)
*/
class OdsTest extends TestCase
{
private $timeZone;
protected function setUp(): void
{
$this->timeZone = date_default_timezone_get();
date_default_timezone_set('UTC');
}
protected function tearDown(): void
{
date_default_timezone_set($this->timeZone);
}
/**
* @var Spreadsheet
*/
private $spreadsheetOdsTest;
/**
* @var Spreadsheet
*/
private $spreadsheetData;
/**
* @return Spreadsheet
*/
private function loadOdsTestFile()
{
if (!$this->spreadsheetOdsTest) {
$filename = 'samples/templates/OOCalcTest.ods';
// Load into this instance
$reader = new Ods();
$this->spreadsheetOdsTest = $reader->loadIntoExisting($filename, new Spreadsheet());
}
return $this->spreadsheetOdsTest;
}
/**
* @return Spreadsheet
*/
protected function loadDataFile()
{
if (!$this->spreadsheetData) {
$filename = 'tests/data/Reader/Ods/data.ods';
// Load into this instance
$reader = new Ods();
$this->spreadsheetData = $reader->load($filename);
}
return $this->spreadsheetData;
}
public function testLoadWorksheets(): void
{
$spreadsheet = $this->loadDataFile();
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Spreadsheet', $spreadsheet);
self::assertEquals(2, $spreadsheet->getSheetCount());
$firstSheet = $spreadsheet->getSheet(0);
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $firstSheet);
$secondSheet = $spreadsheet->getSheet(1);
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $secondSheet);
self::assertEquals('Sheet1', $spreadsheet->getSheet(0)->getTitle());
self::assertEquals('Second Sheet', $spreadsheet->getSheet(1)->getTitle());
}
public function testLoadOneWorksheet(): void
{
$filename = 'tests/data/Reader/Ods/data.ods';
// Load into this instance
$reader = new Ods();
$reader->setLoadSheetsOnly(['Sheet1']);
$spreadsheet = $reader->load($filename);
self::assertEquals(1, $spreadsheet->getSheetCount());
self::assertEquals('Sheet1', $spreadsheet->getSheet(0)->getTitle());
}
public function testLoadBadFile(): void
{
$this->expectException(ReaderException::class);
$reader = new Ods();
$spreadsheet = $reader->load(__FILE__);
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Spreadsheet', $spreadsheet);
self::assertEquals(2, $spreadsheet->getSheetCount());
$firstSheet = $spreadsheet->getSheet(0);
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $firstSheet);
$secondSheet = $spreadsheet->getSheet(1);
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $secondSheet);
}
public function testLoadCorruptFile(): void
{
$this->expectException(ReaderException::class);
$filename = 'tests/data/Reader/Ods/corruptMeta.ods';
$reader = new Ods();
$spreadsheet = $reader->load($filename);
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Spreadsheet', $spreadsheet);
self::assertEquals(2, $spreadsheet->getSheetCount());
$firstSheet = $spreadsheet->getSheet(0);
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $firstSheet);
$secondSheet = $spreadsheet->getSheet(1);
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $secondSheet);
}
public function testReadValueAndComments(): void
{
$spreadsheet = $this->loadOdsTestFile();
$firstSheet = $spreadsheet->getSheet(0);
self::assertEquals(29, $firstSheet->getHighestRow());
self::assertEquals('N', $firstSheet->getHighestColumn());
// Simple cell value
self::assertEquals('Test String 1', $firstSheet->getCell('A1')->getValue());
// Merged cell
self::assertEquals('BOX', $firstSheet->getCell('B18')->getValue());
// Comments/Annotations
self::assertEquals(
'Test for a simple colour-formatted string',
$firstSheet->getComment('A1')->getText()->getPlainText()
);
// Data types
self::assertEquals(DataType::TYPE_STRING, $firstSheet->getCell('A1')->getDataType());
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B1')->getDataType()); // Int
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B6')->getDataType()); // Float
self::assertEquals(1.23, $firstSheet->getCell('B6')->getValue());
self::assertEquals(0, $firstSheet->getCell('G10')->getValue());
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A10')->getDataType()); // Date
self::assertEquals('19-Dec-60', $firstSheet->getCell('A10')->getFormattedValue());
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A13')->getDataType()); // Time
self::assertEquals('2:30:00', $firstSheet->getCell('A13')->getFormattedValue());
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A15')->getDataType()); // Date + Time
self::assertEquals('19-Dec-60 1:30:00', $firstSheet->getCell('A15')->getFormattedValue());
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A11')->getDataType()); // Fraction
self::assertEquals(DataType::TYPE_BOOL, $firstSheet->getCell('D6')->getDataType());
self::assertTrue($firstSheet->getCell('D6')->getValue());
self::assertEquals(DataType::TYPE_FORMULA, $firstSheet->getCell('C6')->getDataType()); // Formula
self::assertEquals('=TRUE()', $firstSheet->getCell('C6')->getValue()); // Formula
// Percentage, Currency
$spreadsheet = $this->loadDataFile();
$firstSheet = $spreadsheet->getSheet(0);
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A1')->getDataType()); // Percentage (10%)
self::assertEquals(0.1, $firstSheet->getCell('A1')->getValue());
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A2')->getDataType()); // Percentage (10.00%)
self::assertEquals(0.1, $firstSheet->getCell('A2')->getValue());
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A4')->getDataType()); // Currency (€10.00)
self::assertEquals(10, $firstSheet->getCell('A4')->getValue());
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A5')->getDataType()); // Currency ($20)
self::assertEquals(20, $firstSheet->getCell('A5')->getValue());
}
public function testReadColors(): void
{
$spreadsheet = $this->loadOdsTestFile();
$firstSheet = $spreadsheet->getSheet(0);
// Background color
$style = $firstSheet->getCell('K3')->getStyle();
self::assertEquals('none', $style->getFill()->getFillType());
self::assertEquals('FFFFFFFF', $style->getFill()->getStartColor()->getARGB());
self::assertEquals('FF000000', $style->getFill()->getEndColor()->getARGB());
}
public function testReadRichText(): void
{
$spreadsheet = $this->loadOdsTestFile();
$firstSheet = $spreadsheet->getSheet(0);
self::assertEquals(
"I don't know if OOCalc supports Rich Text in the same way as Excel, " .
'And this row should be autofit height with text wrap',
$firstSheet->getCell('A28')->getValue()
);
}
public function testReadCellsWithRepeatedSpaces(): void
{
$spreadsheet = $this->loadDataFile();
$firstSheet = $spreadsheet->getSheet(0);
self::assertEquals('This has 4 spaces before and 2 after ', $firstSheet->getCell('A8')->getValue());
self::assertEquals('This only one after ', $firstSheet->getCell('A9')->getValue());
self::assertEquals('Test with DIFFERENT styles and multiple spaces: ', $firstSheet->getCell('A10')->getValue());
self::assertEquals("test with new \nLines", $firstSheet->getCell('A11')->getValue());
}
public function testReadHyperlinks(): void
{
$spreadsheet = $this->loadOdsTestFile();
$firstSheet = $spreadsheet->getSheet(0);
$hyperlink = $firstSheet->getCell('A29');
self::assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType());
self::assertEquals('PhpSpreadsheet', $hyperlink->getValue());
self::assertEquals('https://github.com/PHPOffice/phpspreadsheet', $hyperlink->getHyperlink()->getUrl());
}
// Below some test for features not implemented yet
public function testReadBoldItalicUnderline(): void
{
self::markTestIncomplete('Features not implemented yet');
$spreadsheet = $this->loadOdsTestFile();
$firstSheet = $spreadsheet->getSheet(0);
// Font styles
$style = $firstSheet->getCell('A1')->getStyle();
self::assertEquals('FF000000', $style->getFont()->getColor()->getARGB());
self::assertEquals(11, $style->getFont()->getSize());
self::assertEquals(Font::UNDERLINE_NONE, $style->getFont()->getUnderline());
$style = $firstSheet->getCell('E3')->getStyle();
self::assertEquals(Font::UNDERLINE_SINGLE, $style->getFont()->getUnderline());
$style = $firstSheet->getCell('E1')->getStyle();
self::assertTrue($style->getFont()->getBold());
self::assertTrue($style->getFont()->getItalic());
}
public function testLoadOdsWorkbookProperties(): void
{
$customPropertySet = [
'Owner' => ['type' => Properties::PROPERTY_TYPE_STRING, 'value' => 'PHPOffice'],
'Tested' => ['type' => Properties::PROPERTY_TYPE_BOOLEAN, 'value' => true],
'Counter' => ['type' => Properties::PROPERTY_TYPE_FLOAT, 'value' => 10.0],
'TestDate' => ['type' => Properties::PROPERTY_TYPE_DATE, 'value' => '2019-06-30'],
'HereAndNow' => ['type' => Properties::PROPERTY_TYPE_DATE, 'value' => '2019-06-30'],
];
$filename = 'tests/data/Reader/Ods/propertyTest.ods';
$reader = new Ods();
$spreadsheet = $reader->load($filename);
$properties = $spreadsheet->getProperties();
// Core Properties
// self::assertSame('Mark Baker', $properties->getCreator());
self::assertSame('Property Test File', $properties->getTitle());
self::assertSame('Testing for Properties', $properties->getSubject());
self::assertSame('TEST ODS PHPSpreadsheet', $properties->getKeywords());
// Extended Properties
// self::assertSame('PHPOffice', $properties->getCompany());
// self::assertSame('The Big Boss', $properties->getManager());
// Custom Properties
$customProperties = $properties->getCustomProperties();
self::assertIsArray($customProperties);
$customProperties = array_flip($customProperties);
self::assertArrayHasKey('TestDate', $customProperties);
foreach ($customPropertySet as $propertyName => $testData) {
self::assertTrue($properties->isCustomPropertySet($propertyName));
self::assertSame($testData['type'], $properties->getCustomPropertyType($propertyName));
if ($properties->getCustomPropertyType($propertyName) == Properties::PROPERTY_TYPE_DATE) {
self::assertSame($testData['value'], date('Y-m-d', $properties->getCustomPropertyValue($propertyName)));
} else {
self::assertSame($testData['value'], $properties->getCustomPropertyValue($propertyName));
}
}
}
}