1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Ods; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
8
|
|
|
|
9
|
|
|
/* |
10
|
|
|
* @todo The class doesn't read the bold/italic/underline properties (rich text) |
11
|
|
|
*/ |
12
|
|
|
class OdsTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \PhpOffice\PhpSpreadsheet\Spreadsheet |
16
|
|
|
*/ |
17
|
|
|
public $spreadsheetOOCalcTest; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \PhpOffice\PhpSpreadsheet\Spreadsheet |
21
|
|
|
*/ |
22
|
|
|
public $spreadsheetData; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
protected function loadOOCalcTestFile() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
if (!$this->spreadsheetOOCalcTest) { |
30
|
|
|
$filename = __DIR__ . '/../../../samples/templates/OOCalcTest.ods'; |
31
|
|
|
|
32
|
|
|
// Load into this instance |
33
|
|
|
$reader = new Ods(); |
34
|
|
|
$this->spreadsheetOOCalcTest = $reader->loadIntoExisting($filename, new \PhpOffice\PhpSpreadsheet\Spreadsheet()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $this->spreadsheetOOCalcTest; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet |
42
|
|
|
*/ |
43
|
|
View Code Duplication |
protected function loadDataFile() |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
if (!$this->spreadsheetData) { |
46
|
|
|
$filename = __DIR__ . '/../../data/Reader/Ods/data.ods'; |
47
|
|
|
|
48
|
|
|
// Load into this instance |
49
|
|
|
$reader = new Ods(); |
50
|
|
|
$this->spreadsheetData = $reader->loadIntoExisting($filename, new \PhpOffice\PhpSpreadsheet\Spreadsheet()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->spreadsheetData; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testReadFileProperties() |
57
|
|
|
{ |
58
|
|
|
$filename = __DIR__ . '/../../data/Reader/Ods/data.ods'; |
59
|
|
|
|
60
|
|
|
// Load into this instance |
61
|
|
|
$reader = new Ods(); |
62
|
|
|
|
63
|
|
|
// Test "listWorksheetNames" method |
64
|
|
|
|
65
|
|
|
$this->assertEquals([ |
66
|
|
|
'Sheet1', |
67
|
|
|
'Second Sheet', |
68
|
|
|
], $reader->listWorksheetNames($filename)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testLoadWorksheets() |
72
|
|
|
{ |
73
|
|
|
$spreadsheet = $this->loadDataFile(); |
74
|
|
|
|
75
|
|
|
$this->assertInstanceOf('PhpOffice\PhpSpreadsheet\Spreadsheet', $spreadsheet); |
76
|
|
|
|
77
|
|
|
$this->assertEquals(2, $spreadsheet->getSheetCount()); |
78
|
|
|
|
79
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
80
|
|
|
$this->assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet', $firstSheet); |
81
|
|
|
|
82
|
|
|
$secondSheet = $spreadsheet->getSheet(1); |
83
|
|
|
$this->assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet', $secondSheet); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testReadValueAndComments() |
87
|
|
|
{ |
88
|
|
|
$spreadsheet = $this->loadOOCalcTestFile(); |
89
|
|
|
|
90
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
91
|
|
|
|
92
|
|
|
$this->assertEquals(29, $firstSheet->getHighestRow()); |
93
|
|
|
$this->assertEquals('N', $firstSheet->getHighestColumn()); |
94
|
|
|
|
95
|
|
|
// Simple cell value |
96
|
|
|
$this->assertEquals('Test String 1', $firstSheet->getCell('A1')->getValue()); |
97
|
|
|
|
98
|
|
|
// Merged cell |
99
|
|
|
$this->assertEquals('BOX', $firstSheet->getCell('B18')->getValue()); |
100
|
|
|
|
101
|
|
|
// Comments/Annotations |
102
|
|
|
$this->assertEquals( |
103
|
|
|
'Test for a simple colour-formatted string', |
104
|
|
|
$firstSheet->getComment('A1')->getText()->getPlainText() |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
// Data types |
108
|
|
|
$this->assertEquals(DataType::TYPE_STRING, $firstSheet->getCell('A1')->getDataType()); |
109
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B1')->getDataType()); // Int |
110
|
|
|
|
111
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B6')->getDataType()); // Float |
112
|
|
|
$this->assertEquals(1.23, $firstSheet->getCell('B6')->getValue()); |
113
|
|
|
$this->assertEquals(0, $firstSheet->getCell('G10')->getValue()); |
114
|
|
|
|
115
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A10')->getDataType()); // Date |
116
|
|
|
$this->assertEquals(22269.0, $firstSheet->getCell('A10')->getValue()); |
117
|
|
|
|
118
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A13')->getDataType()); // Time |
119
|
|
|
$this->assertEquals(25569.0625, $firstSheet->getCell('A13')->getValue()); |
120
|
|
|
|
121
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A15')->getDataType()); // Date + Time |
122
|
|
|
$this->assertEquals(22269.0625, $firstSheet->getCell('A15')->getValue()); |
123
|
|
|
|
124
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A11')->getDataType()); // Fraction |
125
|
|
|
|
126
|
|
|
$this->assertEquals(DataType::TYPE_BOOL, $firstSheet->getCell('D6')->getDataType()); |
127
|
|
|
$this->assertTrue($firstSheet->getCell('D6')->getValue()); |
128
|
|
|
|
129
|
|
|
$this->assertEquals(DataType::TYPE_FORMULA, $firstSheet->getCell('C6')->getDataType()); // Formula |
130
|
|
|
$this->assertEquals('=TRUE()', $firstSheet->getCell('C6')->getValue()); // Formula |
131
|
|
|
|
132
|
|
|
/* |
133
|
|
|
* Percentage, Currency |
134
|
|
|
*/ |
135
|
|
|
|
136
|
|
|
$spreadsheet = $this->loadDataFile(); |
137
|
|
|
|
138
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
139
|
|
|
|
140
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A1')->getDataType()); // Percentage (10%) |
|
|
|
|
141
|
|
|
$this->assertEquals(0.1, $firstSheet->getCell('A1')->getValue()); |
142
|
|
|
|
143
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A2')->getDataType()); // Percentage (10.00%) |
|
|
|
|
144
|
|
|
$this->assertEquals(0.1, $firstSheet->getCell('A2')->getValue()); |
145
|
|
|
|
146
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A4')->getDataType()); // Currency (€10.00) |
|
|
|
|
147
|
|
|
$this->assertEquals(10, $firstSheet->getCell('A4')->getValue()); |
148
|
|
|
|
149
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A5')->getDataType()); // Currency ($20) |
|
|
|
|
150
|
|
|
$this->assertEquals(20, $firstSheet->getCell('A5')->getValue()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testReadColors() |
154
|
|
|
{ |
155
|
|
|
$spreadsheet = $this->loadOOCalcTestFile(); |
156
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
157
|
|
|
|
158
|
|
|
// Background color |
159
|
|
|
|
160
|
|
|
$style = $firstSheet->getCell('K3')->getStyle(); |
161
|
|
|
|
162
|
|
|
$this->assertEquals('none', $style->getFill()->getFillType()); |
163
|
|
|
$this->assertEquals('FFFFFFFF', $style->getFill()->getStartColor()->getARGB()); |
164
|
|
|
$this->assertEquals('FF000000', $style->getFill()->getEndColor()->getARGB()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testReadRichText() |
168
|
|
|
{ |
169
|
|
|
$spreadsheet = $this->loadOOCalcTestFile(); |
170
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
171
|
|
|
|
172
|
|
|
$this->assertEquals( |
173
|
|
|
"I don't know if OOCalc supports Rich Text in the same way as Excel, " . |
174
|
|
|
'And this row should be autofit height with text wrap', |
175
|
|
|
$firstSheet->getCell('A28')->getValue() |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testReadCellsWithRepeatedSpaces() |
180
|
|
|
{ |
181
|
|
|
$spreadsheet = $this->loadDataFile(); |
182
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
183
|
|
|
|
184
|
|
|
$this->assertEquals('This has 4 spaces before and 2 after ', $firstSheet->getCell('A8')->getValue()); |
185
|
|
|
$this->assertEquals('This only one after ', $firstSheet->getCell('A9')->getValue()); |
186
|
|
|
$this->assertEquals('Test with DIFFERENT styles and multiple spaces: ', $firstSheet->getCell('A10')->getValue()); |
187
|
|
|
$this->assertEquals("test with new \nLines", $firstSheet->getCell('A11')->getValue()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testReadHyperlinks() |
191
|
|
|
{ |
192
|
|
|
$spreadsheet = $this->loadOOCalcTestFile(); |
193
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
194
|
|
|
|
195
|
|
|
$hyperlink = $firstSheet->getCell('A29'); |
196
|
|
|
|
197
|
|
|
$this->assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType()); |
198
|
|
|
$this->assertEquals('PHPExcel', $hyperlink->getValue()); |
199
|
|
|
$this->assertEquals('http://www.phpexcel.net/', $hyperlink->getHyperlink()->getUrl()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/* |
203
|
|
|
* Below some test for features not implemented yet |
204
|
|
|
*/ |
205
|
|
|
|
206
|
|
|
public function testReadBoldItalicUnderline() |
207
|
|
|
{ |
208
|
|
|
$this->markTestSkipped('Features not implemented yet'); |
209
|
|
|
|
210
|
|
|
$spreadsheet = $this->loadOOCalcTestFile(); |
211
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
212
|
|
|
|
213
|
|
|
// Font styles |
214
|
|
|
|
215
|
|
|
$style = $firstSheet->getCell('A1')->getStyle(); |
216
|
|
|
$this->assertEquals('FF000000', $style->getFont()->getColor()->getARGB()); |
217
|
|
|
$this->assertEquals(11, $style->getFont()->getSize()); |
218
|
|
|
$this->assertEquals(Font::UNDERLINE_NONE, $style->getFont()->getUnderline()); |
219
|
|
|
|
220
|
|
|
$style = $firstSheet->getCell('E3')->getStyle(); |
221
|
|
|
$this->assertEquals(Font::UNDERLINE_SINGLE, $style->getFont()->getUnderline()); |
222
|
|
|
|
223
|
|
|
$style = $firstSheet->getCell('E1')->getStyle(); |
224
|
|
|
$this->assertTrue($style->getFont()->getBold()); |
225
|
|
|
$this->assertTrue($style->getFont()->getItalic()); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.