Completed
Push — master ( 763b20...735103 )
by Adrien
09:55
created

OdsTest::testReadRichText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
4
5
use PhpOffice\PhpSpreadsheet\Cell\DataType;
6
use PhpOffice\PhpSpreadsheet\Document\Properties;
7
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
8
use PhpOffice\PhpSpreadsheet\Reader\Ods;
9
use PhpOffice\PhpSpreadsheet\Spreadsheet;
10
use PhpOffice\PhpSpreadsheet\Style\Font;
11
use PHPUnit\Framework\TestCase;
12
13
/**
14
 * @TODO The class doesn't read the bold/italic/underline properties (rich text)
15
 */
16
class OdsTest extends TestCase
17
{
18
    /**
19
     * @var Spreadsheet
20
     */
21
    private $spreadsheetOdsTest;
22
23
    /**
24
     * @var Spreadsheet
25
     */
26
    private $spreadsheetData;
27
28
    /**
29
     * @return Spreadsheet
30
     */
31
    private function loadOdsTestFile()
32
    {
33
        if (!$this->spreadsheetOdsTest) {
34
            $filename = 'samples/templates/OOCalcTest.ods';
35
36
            // Load into this instance
37
            $reader = new Ods();
38
            $this->spreadsheetOdsTest = $reader->loadIntoExisting($filename, new Spreadsheet());
39
        }
40
41
        return $this->spreadsheetOdsTest;
42
    }
43
44
    /**
45
     * @return Spreadsheet
46
     */
47
    protected function loadDataFile()
48
    {
49
        if (!$this->spreadsheetData) {
50
            $filename = 'tests/data/Reader/Ods/data.ods';
51
52
            // Load into this instance
53
            $reader = new Ods();
54
            $this->spreadsheetData = $reader->load($filename);
55
        }
56
57
        return $this->spreadsheetData;
58
    }
59
60
    public function testLoadWorksheets(): void
61
    {
62
        $spreadsheet = $this->loadDataFile();
63
64
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Spreadsheet', $spreadsheet);
65
66
        self::assertEquals(2, $spreadsheet->getSheetCount());
67
68
        $firstSheet = $spreadsheet->getSheet(0);
69
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $firstSheet);
70
71
        $secondSheet = $spreadsheet->getSheet(1);
72
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $secondSheet);
73
        self::assertEquals('Sheet1', $spreadsheet->getSheet(0)->getTitle());
74
        self::assertEquals('Second Sheet', $spreadsheet->getSheet(1)->getTitle());
75
    }
76
77
    public function testLoadOneWorksheet(): void
78
    {
79
        $filename = 'tests/data/Reader/Ods/data.ods';
80
81
        // Load into this instance
82
        $reader = new Ods();
83
        $reader->setLoadSheetsOnly(['Sheet1']);
84
        $spreadsheet = $reader->load($filename);
85
86
        self::assertEquals(1, $spreadsheet->getSheetCount());
87
88
        self::assertEquals('Sheet1', $spreadsheet->getSheet(0)->getTitle());
89
    }
90
91
    public function testLoadBadFile(): void
92
    {
93
        $this->expectException(ReaderException::class);
94
        $reader = new Ods();
95
        $spreadsheet = $reader->load(__FILE__);
96
97
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Spreadsheet', $spreadsheet);
98
99
        self::assertEquals(2, $spreadsheet->getSheetCount());
100
101
        $firstSheet = $spreadsheet->getSheet(0);
102
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $firstSheet);
103
104
        $secondSheet = $spreadsheet->getSheet(1);
105
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $secondSheet);
106
    }
107
108
    public function testLoadCorruptFile(): void
109
    {
110
        $this->expectException(ReaderException::class);
111
        $filename = 'tests/data/Reader/Ods/corruptMeta.ods';
112
        $reader = new Ods();
113
        $spreadsheet = $reader->load($filename);
114
115
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Spreadsheet', $spreadsheet);
116
117
        self::assertEquals(2, $spreadsheet->getSheetCount());
118
119
        $firstSheet = $spreadsheet->getSheet(0);
120
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $firstSheet);
121
122
        $secondSheet = $spreadsheet->getSheet(1);
123
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $secondSheet);
124
    }
125
126
    public function testReadValueAndComments(): void
127
    {
128
        $spreadsheet = $this->loadOdsTestFile();
129
130
        $firstSheet = $spreadsheet->getSheet(0);
131
132
        self::assertEquals(29, $firstSheet->getHighestRow());
133
        self::assertEquals('N', $firstSheet->getHighestColumn());
134
135
        // Simple cell value
136
        self::assertEquals('Test String 1', $firstSheet->getCell('A1')->getValue());
137
138
        // Merged cell
139
        self::assertEquals('BOX', $firstSheet->getCell('B18')->getValue());
140
141
        // Comments/Annotations
142
        self::assertEquals(
143
            'Test for a simple colour-formatted string',
144
            $firstSheet->getComment('A1')->getText()->getPlainText()
145
        );
146
147
        // Data types
148
        self::assertEquals(DataType::TYPE_STRING, $firstSheet->getCell('A1')->getDataType());
149
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B1')->getDataType()); // Int
150
151
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B6')->getDataType()); // Float
152
        self::assertEquals(1.23, $firstSheet->getCell('B6')->getValue());
153
        self::assertEquals(0, $firstSheet->getCell('G10')->getValue());
154
155
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A10')->getDataType()); // Date
156
        self::assertEquals(22269.0, $firstSheet->getCell('A10')->getValue());
157
158
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A13')->getDataType()); // Time
159
        self::assertEquals(25569.0625, $firstSheet->getCell('A13')->getValue());
160
161
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A15')->getDataType()); // Date + Time
162
        self::assertEquals(22269.0625, $firstSheet->getCell('A15')->getValue());
163
164
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A11')->getDataType()); // Fraction
165
166
        self::assertEquals(DataType::TYPE_BOOL, $firstSheet->getCell('D6')->getDataType());
167
        self::assertTrue($firstSheet->getCell('D6')->getValue());
168
169
        self::assertEquals(DataType::TYPE_FORMULA, $firstSheet->getCell('C6')->getDataType()); // Formula
170
        self::assertEquals('=TRUE()', $firstSheet->getCell('C6')->getValue()); // Formula
171
172
        // Percentage, Currency
173
174
        $spreadsheet = $this->loadDataFile();
175
176
        $firstSheet = $spreadsheet->getSheet(0);
177
178
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A1')->getDataType()); // Percentage (10%)
179
        self::assertEquals(0.1, $firstSheet->getCell('A1')->getValue());
180
181
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A2')->getDataType()); // Percentage (10.00%)
182
        self::assertEquals(0.1, $firstSheet->getCell('A2')->getValue());
183
184
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A4')->getDataType()); // Currency (€10.00)
185
        self::assertEquals(10, $firstSheet->getCell('A4')->getValue());
186
187
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A5')->getDataType()); // Currency ($20)
188
        self::assertEquals(20, $firstSheet->getCell('A5')->getValue());
189
    }
190
191
    public function testReadColors(): void
192
    {
193
        $spreadsheet = $this->loadOdsTestFile();
194
        $firstSheet = $spreadsheet->getSheet(0);
195
196
        // Background color
197
198
        $style = $firstSheet->getCell('K3')->getStyle();
199
200
        self::assertEquals('none', $style->getFill()->getFillType());
201
        self::assertEquals('FFFFFFFF', $style->getFill()->getStartColor()->getARGB());
202
        self::assertEquals('FF000000', $style->getFill()->getEndColor()->getARGB());
203
    }
204
205
    public function testReadRichText(): void
206
    {
207
        $spreadsheet = $this->loadOdsTestFile();
208
        $firstSheet = $spreadsheet->getSheet(0);
209
210
        self::assertEquals(
211
            "I don't know if OOCalc supports Rich Text in the same way as Excel, " .
212
            'And this row should be autofit height with text wrap',
213
            $firstSheet->getCell('A28')->getValue()
214
        );
215
    }
216
217
    public function testReadCellsWithRepeatedSpaces(): void
218
    {
219
        $spreadsheet = $this->loadDataFile();
220
        $firstSheet = $spreadsheet->getSheet(0);
221
222
        self::assertEquals('This has    4 spaces before and 2 after  ', $firstSheet->getCell('A8')->getValue());
223
        self::assertEquals('This only one after ', $firstSheet->getCell('A9')->getValue());
224
        self::assertEquals('Test with DIFFERENT styles     and multiple spaces:  ', $firstSheet->getCell('A10')->getValue());
225
        self::assertEquals("test with new \nLines", $firstSheet->getCell('A11')->getValue());
226
    }
227
228
    public function testReadHyperlinks(): void
229
    {
230
        $spreadsheet = $this->loadOdsTestFile();
231
        $firstSheet = $spreadsheet->getSheet(0);
232
233
        $hyperlink = $firstSheet->getCell('A29');
234
235
        self::assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType());
236
        self::assertEquals('PhpSpreadsheet', $hyperlink->getValue());
237
        self::assertEquals('https://github.com/PHPOffice/phpspreadsheet', $hyperlink->getHyperlink()->getUrl());
238
    }
239
240
    // Below some test for features not implemented yet
241
242
    public function testReadBoldItalicUnderline(): void
243
    {
244
        self::markTestIncomplete('Features not implemented yet');
245
246
        $spreadsheet = $this->loadOdsTestFile();
247
        $firstSheet = $spreadsheet->getSheet(0);
248
249
        // Font styles
250
251
        $style = $firstSheet->getCell('A1')->getStyle();
252
        self::assertEquals('FF000000', $style->getFont()->getColor()->getARGB());
253
        self::assertEquals(11, $style->getFont()->getSize());
254
        self::assertEquals(Font::UNDERLINE_NONE, $style->getFont()->getUnderline());
255
256
        $style = $firstSheet->getCell('E3')->getStyle();
257
        self::assertEquals(Font::UNDERLINE_SINGLE, $style->getFont()->getUnderline());
258
259
        $style = $firstSheet->getCell('E1')->getStyle();
260
        self::assertTrue($style->getFont()->getBold());
261
        self::assertTrue($style->getFont()->getItalic());
262
    }
263
264
    public function testLoadOdsWorkbookProperties(): void
265
    {
266
        $customPropertySet = [
267
            'Owner' => ['type' => Properties::PROPERTY_TYPE_STRING, 'value' => 'PHPOffice'],
268
            'Tested' => ['type' => Properties::PROPERTY_TYPE_BOOLEAN, 'value' => true],
269
            'Counter' => ['type' => Properties::PROPERTY_TYPE_FLOAT, 'value' => 10.0],
270
            'TestDate' => ['type' => Properties::PROPERTY_TYPE_DATE, 'value' => '2019-06-30'],
271
            'HereAndNow' => ['type' => Properties::PROPERTY_TYPE_DATE, 'value' => '2019-06-30'],
272
        ];
273
274
        $filename = 'tests/data/Reader/Ods/propertyTest.ods';
275
        $reader = new Ods();
276
        $spreadsheet = $reader->load($filename);
277
278
        $properties = $spreadsheet->getProperties();
279
        // Core Properties
280
//        self::assertSame('Mark Baker', $properties->getCreator());
281
        self::assertSame('Property Test File', $properties->getTitle());
282
        self::assertSame('Testing for Properties', $properties->getSubject());
283
        self::assertSame('TEST ODS PHPSpreadsheet', $properties->getKeywords());
284
285
        // Extended Properties
286
//        self::assertSame('PHPOffice', $properties->getCompany());
287
//        self::assertSame('The Big Boss', $properties->getManager());
288
289
        // Custom Properties
290
        $customProperties = $properties->getCustomProperties();
291
        self::assertIsArray($customProperties);
292
        $customProperties = array_flip($customProperties);
293
        self::assertArrayHasKey('TestDate', $customProperties);
294
295
        foreach ($customPropertySet as $propertyName => $testData) {
296
            self::assertTrue($properties->isCustomPropertySet($propertyName));
297
            self::assertSame($testData['type'], $properties->getCustomPropertyType($propertyName));
298
            if ($properties->getCustomPropertyType($propertyName) == Properties::PROPERTY_TYPE_DATE) {
299
                self::assertSame($testData['value'], date('Y-m-d', $properties->getCustomPropertyValue($propertyName)));
300
            } else {
301
                self::assertSame($testData['value'], $properties->getCustomPropertyValue($propertyName));
302
            }
303
        }
304
    }
305
}
306