Completed
Push — develop ( c46008...bae234 )
by Adrien
26:32
created

OdsTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 210
rs 10
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testReadFileProperties() 0 13 1
A testLoadWorksheets() 0 13 1
A testReadRichText() 0 9 1
A testReadCellsWithRepeatedSpaces() 0 9 1
A testReadColors() 0 12 1
A testReadValueAndComments() 0 63 1
A loadDataFile() 0 11 2
A testReadBoldItalicUnderline() 0 20 1
A testReadHyperlinks() 0 10 1
A loadOdsTestFile() 0 11 2
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\Spreadsheet;
8
use PhpOffice\PhpSpreadsheet\Style\Font;
9
use PHPUnit\Framework\TestCase;
10
11
/**
12
 * @todo The class doesn't read the bold/italic/underline properties (rich text)
13
 */
14
class OdsTest extends TestCase
15
{
16
    /**
17
     * @var Spreadsheet
18
     */
19
    private $spreadsheetOdsTest;
20
21
    /**
22
     * @var Spreadsheet
23
     */
24
    private $spreadsheetData;
25
26
    /**
27
     * @return Spreadsheet
28
     */
29
    private function loadOdsTestFile()
30
    {
31
        if (!$this->spreadsheetOdsTest) {
32
            $filename = __DIR__ . '/../../../samples/templates/OOCalcTest.ods';
33
34
            // Load into this instance
35
            $reader = new Ods();
36
            $this->spreadsheetOdsTest = $reader->loadIntoExisting($filename, new Spreadsheet());
37
        }
38
39
        return $this->spreadsheetOdsTest;
40
    }
41
42
    /**
43
     * @return Spreadsheet
44
     */
45
    protected function loadDataFile()
46
    {
47
        if (!$this->spreadsheetData) {
48
            $filename = __DIR__ . '/../../data/Reader/Ods/data.ods';
49
50
            // Load into this instance
51
            $reader = new Ods();
52
            $this->spreadsheetData = $reader->loadIntoExisting($filename, new Spreadsheet());
53
        }
54
55
        return $this->spreadsheetData;
56
    }
57
58
    public function testReadFileProperties()
59
    {
60
        $filename = __DIR__ . '/../../data/Reader/Ods/data.ods';
61
62
        // Load into this instance
63
        $reader = new Ods();
64
65
        // Test "listWorksheetNames" method
66
67
        self::assertEquals([
68
            'Sheet1',
69
            'Second Sheet',
70
        ], $reader->listWorksheetNames($filename));
71
    }
72
73
    public function testLoadWorksheets()
74
    {
75
        $spreadsheet = $this->loadDataFile();
76
77
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Spreadsheet', $spreadsheet);
78
79
        self::assertEquals(2, $spreadsheet->getSheetCount());
80
81
        $firstSheet = $spreadsheet->getSheet(0);
82
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $firstSheet);
83
84
        $secondSheet = $spreadsheet->getSheet(1);
85
        self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $secondSheet);
86
    }
87
88
    public function testReadValueAndComments()
89
    {
90
        $spreadsheet = $this->loadOdsTestFile();
91
92
        $firstSheet = $spreadsheet->getSheet(0);
93
94
        self::assertEquals(29, $firstSheet->getHighestRow());
95
        self::assertEquals('N', $firstSheet->getHighestColumn());
96
97
        // Simple cell value
98
        self::assertEquals('Test String 1', $firstSheet->getCell('A1')->getValue());
99
100
        // Merged cell
101
        self::assertEquals('BOX', $firstSheet->getCell('B18')->getValue());
102
103
        // Comments/Annotations
104
        self::assertEquals(
105
            'Test for a simple colour-formatted string',
106
            $firstSheet->getComment('A1')->getText()->getPlainText()
107
        );
108
109
        // Data types
110
        self::assertEquals(DataType::TYPE_STRING, $firstSheet->getCell('A1')->getDataType());
111
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B1')->getDataType()); // Int
112
113
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B6')->getDataType()); // Float
114
        self::assertEquals(1.23, $firstSheet->getCell('B6')->getValue());
115
        self::assertEquals(0, $firstSheet->getCell('G10')->getValue());
116
117
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A10')->getDataType()); // Date
118
        self::assertEquals(22269.0, $firstSheet->getCell('A10')->getValue());
119
120
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A13')->getDataType()); // Time
121
        self::assertEquals(25569.0625, $firstSheet->getCell('A13')->getValue());
122
123
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A15')->getDataType()); // Date + Time
124
        self::assertEquals(22269.0625, $firstSheet->getCell('A15')->getValue());
125
126
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A11')->getDataType()); // Fraction
127
128
        self::assertEquals(DataType::TYPE_BOOL, $firstSheet->getCell('D6')->getDataType());
129
        self::assertTrue($firstSheet->getCell('D6')->getValue());
130
131
        self::assertEquals(DataType::TYPE_FORMULA, $firstSheet->getCell('C6')->getDataType()); // Formula
132
        self::assertEquals('=TRUE()', $firstSheet->getCell('C6')->getValue()); // Formula
133
134
        // Percentage, Currency
135
136
        $spreadsheet = $this->loadDataFile();
137
138
        $firstSheet = $spreadsheet->getSheet(0);
139
140
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A1')->getDataType()); // Percentage (10%)
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
141
        self::assertEquals(0.1, $firstSheet->getCell('A1')->getValue());
142
143
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A2')->getDataType()); // Percentage (10.00%)
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
144
        self::assertEquals(0.1, $firstSheet->getCell('A2')->getValue());
145
146
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A4')->getDataType()); // Currency (€10.00)
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
147
        self::assertEquals(10, $firstSheet->getCell('A4')->getValue());
148
149
        self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A5')->getDataType()); // Currency ($20)
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
150
        self::assertEquals(20, $firstSheet->getCell('A5')->getValue());
151
    }
152
153
    public function testReadColors()
154
    {
155
        $spreadsheet = $this->loadOdsTestFile();
156
        $firstSheet = $spreadsheet->getSheet(0);
157
158
        // Background color
159
160
        $style = $firstSheet->getCell('K3')->getStyle();
161
162
        self::assertEquals('none', $style->getFill()->getFillType());
163
        self::assertEquals('FFFFFFFF', $style->getFill()->getStartColor()->getARGB());
164
        self::assertEquals('FF000000', $style->getFill()->getEndColor()->getARGB());
165
    }
166
167
    public function testReadRichText()
168
    {
169
        $spreadsheet = $this->loadOdsTestFile();
170
        $firstSheet = $spreadsheet->getSheet(0);
171
172
        self::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
        self::assertEquals('This has    4 spaces before and 2 after  ', $firstSheet->getCell('A8')->getValue());
185
        self::assertEquals('This only one after ', $firstSheet->getCell('A9')->getValue());
186
        self::assertEquals('Test with DIFFERENT styles     and multiple spaces:  ', $firstSheet->getCell('A10')->getValue());
187
        self::assertEquals("test with new \nLines", $firstSheet->getCell('A11')->getValue());
188
    }
189
190
    public function testReadHyperlinks()
191
    {
192
        $spreadsheet = $this->loadOdsTestFile();
193
        $firstSheet = $spreadsheet->getSheet(0);
194
195
        $hyperlink = $firstSheet->getCell('A29');
196
197
        self::assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType());
198
        self::assertEquals('PhpSpreadsheet', $hyperlink->getValue());
199
        self::assertEquals('https://github.com/PHPOffice/phpspreadsheet', $hyperlink->getHyperlink()->getUrl());
200
    }
201
202
    // Below some test for features not implemented yet
203
204
    public function testReadBoldItalicUnderline()
205
    {
206
        $this->markTestIncomplete('Features not implemented yet');
207
208
        $spreadsheet = $this->loadOdsTestFile();
209
        $firstSheet = $spreadsheet->getSheet(0);
210
211
        // Font styles
212
213
        $style = $firstSheet->getCell('A1')->getStyle();
214
        self::assertEquals('FF000000', $style->getFont()->getColor()->getARGB());
215
        self::assertEquals(11, $style->getFont()->getSize());
216
        self::assertEquals(Font::UNDERLINE_NONE, $style->getFont()->getUnderline());
217
218
        $style = $firstSheet->getCell('E3')->getStyle();
219
        self::assertEquals(Font::UNDERLINE_SINGLE, $style->getFont()->getUnderline());
220
221
        $style = $firstSheet->getCell('E1')->getStyle();
222
        self::assertTrue($style->getFont()->getBold());
223
        self::assertTrue($style->getFont()->getItalic());
224
    }
225
}
226