Passed
Push — master ( cd5836...8e3417 )
by
unknown
27:01 queued 16:09
created

ExportArrayTest::testStackedRotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 17
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpOffice\PhpSpreadsheetTests\Style;
6
7
use PhpOffice\PhpSpreadsheet\Cell\DataType;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheet\Style\Alignment;
10
use PhpOffice\PhpSpreadsheet\Style\Border;
11
use PhpOffice\PhpSpreadsheet\Style\Color;
12
use PhpOffice\PhpSpreadsheet\Style\Fill;
13
use PhpOffice\PhpSpreadsheet\Style\Font;
14
use PhpOffice\PhpSpreadsheet\Style\Protection;
15
use PHPUnit\Framework\TestCase;
16
17
class ExportArrayTest extends TestCase
18
{
19
    public function testStyleCopy(): void
20
    {
21
        $spreadsheet = new Spreadsheet();
22
        $sheet = $spreadsheet->getActiveSheet();
23
        $cell1 = $sheet->getCell('A1');
24
        $cell1->setValue('Cell A1');
25
        $cell1style = $cell1->getStyle();
26
        $cell1style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
27
        $cell1style->getFont()->getColor()->setARGB('FFFF0000');
28
        $cell1style->getFont()->setBold(true);
29
        $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
30
        $cell1style->getFill()->setStartColor(new Color('FF0000FF'));
31
        $cell1style->getFill()->setEndColor(new Color('FF00FF00'));
32
        $cell1style->getFont()->setUnderline(true);
33
        self::assertEquals(Font::UNDERLINE_SINGLE, $cell1style->getFont()->getUnderline());
34
        $cell1style->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
35
        $cell1style->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
36
        $styleArray = $cell1style->exportArray();
37
        $cell2 = $sheet->getCell('B1');
38
        $cell2->setValue('Cell B1');
39
        $cell2style = $cell2->getStyle();
40
        $cell2style->applyFromArray($styleArray);
41
42
        self::AssertEquals($cell1style->getAlignment()->getHorizontal(), $cell2style->getAlignment()->getHorizontal());
43
        self::AssertEquals($cell1style->getFont()->getColor()->getARGB(), $cell2style->getFont()->getColor()->getARGB());
44
        self::AssertEquals($cell1style->getFont()->getBold(), $cell2style->getFont()->getBold());
45
        self::AssertEquals($cell1style->getFont()->getUnderline(), $cell2style->getFont()->getUnderline());
46
        self::AssertEquals($cell1style->getFill()->getFillType(), $cell2style->getFill()->getFillType());
47
        self::AssertEquals($cell1style->getFill()->getStartColor()->getARGB(), $cell2style->getFill()->getStartColor()->getARGB());
48
        self::AssertEquals($cell1style->getFill()->getEndColor()->getARGB(), $cell2style->getFill()->getEndColor()->getARGB());
49
        self::AssertEquals($cell1style->getProtection()->getLocked(), $cell2style->getProtection()->getLocked());
50
        self::AssertEquals($cell1style->getProtection()->getHidden(), $cell2style->getProtection()->getHidden());
51
52
        self::AssertEquals($cell1style->getHashCode(), $cell2style->getHashCode());
53
        self::AssertEquals($cell1style->getAlignment()->getHashCode(), $cell2style->getAlignment()->getHashCode());
54
        self::AssertEquals($cell1style->getFont()->getHashCode(), $cell2style->getFont()->getHashCode());
55
        self::AssertEquals($cell1style->getFill()->getHashCode(), $cell2style->getFill()->getHashCode());
56
        self::AssertEquals($cell1style->getProtection()->getHashCode(), $cell2style->getProtection()->getHashCode());
57
        $spreadsheet->disconnectWorksheets();
58
    }
59
60
    public function testStyleFromArrayCopy(): void
61
    {
62
        $spreadsheet = new Spreadsheet();
63
        $sheet = $spreadsheet->getActiveSheet();
64
        $cell1 = $sheet->getCell('A1');
65
        $cell1->setValue('Cell A1');
66
        $cell1style = $cell1->getStyle();
67
        $cell1style->getAlignment()->applyFromArray(['horizontal' => Alignment::HORIZONTAL_RIGHT]);
68
        $cell1style->getFont()->getColor()->setARGB('FFFF0000');
69
        $cell1style->getFont()->applyFromArray(['bold' => true]);
70
        $cell1style->getFill()->applyFromArray(['fillType' => Fill::FILL_PATTERN_GRAY125]);
71
        $cell1style->getFill()->getStartColor()->applyFromArray(['argb' => 'FF0000FF']);
72
        $cell1style->getFill()->getEndColor()->setRGB('00FF00');
73
        $cell1style->getFill()->setRotation(45);
74
        $cell1style->getFont()->setUnderline(true);
75
        self::assertEquals(Font::UNDERLINE_SINGLE, $cell1style->getFont()->getUnderline());
76
        $cell1style->getProtection()->applyFromArray(['hidden' => Protection::PROTECTION_UNPROTECTED, 'locked' => Protection::PROTECTION_UNPROTECTED]);
77
        $styleArray = $cell1style->exportArray();
78
        $cell2 = $sheet->getCell('B1');
79
        $cell2->setValue('Cell B1');
80
        $cell2style = $cell2->getStyle();
81
        $cell2style->applyFromArray($styleArray);
82
83
        self::AssertEquals($cell1style->getAlignment()->getHorizontal(), $cell2style->getAlignment()->getHorizontal());
84
        self::AssertEquals($cell1style->getFont()->getColor()->getARGB(), $cell2style->getFont()->getColor()->getARGB());
85
        self::AssertEquals($cell1style->getFont()->getBold(), $cell2style->getFont()->getBold());
86
        self::AssertEquals($cell1style->getFont()->getUnderline(), $cell2style->getFont()->getUnderline());
87
        self::AssertEquals($cell1style->getFill()->getFillType(), $cell2style->getFill()->getFillType());
88
        self::AssertEquals($cell1style->getFill()->getRotation(), $cell2style->getFill()->getRotation());
89
        self::AssertEquals($cell1style->getFill()->getStartColor()->getARGB(), $cell2style->getFill()->getStartColor()->getARGB());
90
        self::AssertEquals($cell1style->getFill()->getEndColor()->getARGB(), $cell2style->getFill()->getEndColor()->getARGB());
91
        self::AssertEquals($cell1style->getProtection()->getLocked(), $cell2style->getProtection()->getLocked());
92
        self::AssertEquals($cell1style->getProtection()->getHidden(), $cell2style->getProtection()->getHidden());
93
94
        self::AssertEquals($cell1style->getFill()->getStartColor()->getHashCode(), $cell2style->getFill()->getStartColor()->getHashCode());
95
        self::AssertEquals($cell1style->getFill()->getEndColor()->getHashCode(), $cell2style->getFill()->getEndColor()->getHashCode());
96
        $spreadsheet->disconnectWorksheets();
97
    }
98
99
    public function testNumberFormat(): void
100
    {
101
        $spreadsheet = new Spreadsheet();
102
        $sheet = $spreadsheet->getActiveSheet();
103
        $cell1 = $sheet->getCell('A1');
104
        $cell1style = $cell1->getStyle();
105
        $fmt2 = '$ #,##0.000';
106
        $cell1style->getNumberFormat()->setFormatCode($fmt2);
107
        $cell1style->getFont()->setUnderline('');
108
        self::assertEquals(Font::UNDERLINE_NONE, $cell1style->getFont()->getUnderline());
109
        $cell1->setValue(2345.679);
110
        $styleArray = $cell1style->exportArray();
111
        self::assertEquals('$ 2,345.679', $cell1->getFormattedValue());
112
113
        $cell2 = $sheet->getCell('B1');
114
        $cell2->setValue(12345.679);
115
        $cell2style = $cell2->getStyle();
116
        $cell2style->applyFromArray($styleArray);
117
        self::assertEquals('$ 12,345.679', $cell2->getFormattedValue());
118
119
        self::AssertEquals($cell1style->getNumberFormat()->getHashCode(), $cell2style->getNumberFormat()->getHashCode());
120
        $spreadsheet->disconnectWorksheets();
121
    }
122
123
    public function testNumberFormatFromArray(): void
124
    {
125
        $spreadsheet = new Spreadsheet();
126
        $sheet = $spreadsheet->getActiveSheet();
127
        $cell1 = $sheet->getCell('A1');
128
        $cell1style = $cell1->getStyle();
129
        $fmt2 = '$ #,##0.000';
130
        $cell1style->getNumberFormat()->applyFromArray(['formatCode' => $fmt2]);
131
        $cell1style->getFont()->setUnderline('');
132
        self::assertEquals(Font::UNDERLINE_NONE, $cell1style->getFont()->getUnderline());
133
        $cell1style->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN);
134
        $cell1->setValue(2345.679);
135
        $styleArray = $cell1style->exportArray();
136
        self::assertEquals('$ 2,345.679', $cell1->getFormattedValue());
137
138
        $cell2 = $sheet->getCell('B1');
139
        $cell2->setValue(12345.679);
140
        $cell2style = $cell2->getStyle();
141
        $cell2style->applyFromArray($styleArray);
142
        self::assertEquals('$ 12,345.679', $cell2->getFormattedValue());
143
144
        self::AssertEquals($cell1style->getNumberFormat()->getHashCode(), $cell2style->getNumberFormat()->getHashCode());
145
        self::AssertEquals($cell1style->getBorders()->getHashCode(), $cell2style->getBorders()->getHashCode());
146
        self::AssertEquals($cell1style->getBorders()->getTop()->getHashCode(), $cell2style->getBorders()->getTop()->getHashCode());
147
        self::AssertEquals($cell1style->getBorders()->getTop()->getBorderStyle(), $cell2style->getBorders()->getTop()->getBorderStyle());
148
        $spreadsheet->disconnectWorksheets();
149
    }
150
151
    public function testStackedRotation(): void
152
    {
153
        $spreadsheet = new Spreadsheet();
154
        $sheet = $spreadsheet->getActiveSheet();
155
        $cell1 = $sheet->getCell('A1');
156
        $cell1->setValue('Cell A1');
157
        $cell1style = $cell1->getStyle();
158
        $cell1style->getAlignment()->setTextRotation(Alignment::TEXTROTATION_STACK_EXCEL);
159
        self::assertEquals(Alignment::TEXTROTATION_STACK_PHPSPREADSHEET, $cell1style->getAlignment()->getTextRotation());
160
        $styleArray = $cell1style->exportArray();
161
        $cell2 = $sheet->getCell('B1');
162
        $cell2->setValue('Cell B1');
163
        $cell2style = $cell2->getStyle();
164
        $cell2style->applyFromArray($styleArray);
165
166
        self::AssertEquals($cell1style->getAlignment()->getTextRotation(), $cell2style->getAlignment()->getTextRotation());
167
        $spreadsheet->disconnectWorksheets();
168
    }
169
170
    public function testFillColors(): void
171
    {
172
        $spreadsheet = new Spreadsheet();
173
        $sheet = $spreadsheet->getActiveSheet();
174
175
        $cell1 = $sheet->getCell('A2');
176
        $cell1style = $cell1->getStyle();
177
        $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
178
        $cell1style->getFill()->getStartColor()->setArgb('FF112233');
179
        $styleArray = $cell1style->exportArray();
180
        self::assertEquals(
181
            [
182
                'fillType' => Fill::FILL_PATTERN_GRAY125,
183
                'rotation' => 0.0,
184
                'endColor' => ['argb' => 'FF000000'],
185
                'startColor' => ['argb' => 'FF112233'],
186
            ],
187
            $styleArray['fill'],
188
            'changed start color with setArgb'
189
        );
190
191
        $cell1 = $sheet->getCell('A1');
192
        $cell1style = $cell1->getStyle();
193
        $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
194
        $styleArray = $cell1style->exportArray();
195
        self::assertEquals(
196
            [
197
                'fillType' => Fill::FILL_PATTERN_GRAY125,
198
                'rotation' => 0.0,
199
            ],
200
            $styleArray['fill'],
201
            'default colors'
202
        );
203
204
        $cell1 = $sheet->getCell('A3');
205
        $cell1style = $cell1->getStyle();
206
        $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
207
        $cell1style->getFill()->getEndColor()->setArgb('FF112233');
208
        $styleArray = $cell1style->exportArray();
209
        self::assertEquals(
210
            [
211
                'fillType' => Fill::FILL_PATTERN_GRAY125,
212
                'rotation' => 0.0,
213
                'endColor' => ['argb' => 'FF112233'],
214
                'startColor' => ['argb' => 'FFFFFFFF'],
215
            ],
216
            $styleArray['fill'],
217
            'changed end color with setArgb'
218
        );
219
220
        $cell1 = $sheet->getCell('A4');
221
        $cell1style = $cell1->getStyle();
222
        $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
223
        $cell1style->getFill()->setEndColor(new Color('FF0000FF'));
224
        $styleArray = $cell1style->exportArray();
225
        self::assertEquals(
226
            [
227
                'fillType' => Fill::FILL_PATTERN_GRAY125,
228
                'rotation' => 0.0,
229
                'endColor' => ['argb' => 'FF0000FF'],
230
                'startColor' => ['argb' => 'FFFFFFFF'],
231
            ],
232
            $styleArray['fill'],
233
            'changed end color with setEndColor'
234
        );
235
236
        $cell1 = $sheet->getCell('A5');
237
        $cell1style = $cell1->getStyle();
238
        $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125);
239
        $cell1style->getFill()->setStartColor(new Color('FF0000FF'));
240
        $styleArray = $cell1style->exportArray();
241
        self::assertEquals(
242
            [
243
                'fillType' => Fill::FILL_PATTERN_GRAY125,
244
                'rotation' => 0.0,
245
                'startColor' => ['argb' => 'FF0000FF'],
246
                'endColor' => ['argb' => 'FF000000'],
247
            ],
248
            $styleArray['fill'],
249
            'changed start color with setStartColor'
250
        );
251
252
        $cell1 = $sheet->getCell('A6');
253
        $cell1->getStyle()->getFill()->applyFromArray(
254
            [
255
                'fillType' => Fill::FILL_PATTERN_GRAY125,
256
                'rotation' => 45.0,
257
                'startColor' => ['argb' => 'FF00FFFF'],
258
            ]
259
        );
260
        $cell1style = $cell1->getStyle();
261
        $styleArray = $cell1style->exportArray();
262
        self::assertEquals(
263
            [
264
                'fillType' => Fill::FILL_PATTERN_GRAY125,
265
                'rotation' => 45.0,
266
                'startColor' => ['argb' => 'FF00FFFF'],
267
                'endColor' => ['argb' => 'FF000000'],
268
            ],
269
            $styleArray['fill'],
270
            'applyFromArray with startColor'
271
        );
272
273
        $cell1 = $sheet->getCell('A7');
274
        $cell1->getStyle()->getFill()->applyFromArray(
275
            [
276
                'fillType' => Fill::FILL_PATTERN_GRAY125,
277
            ]
278
        );
279
        $cell1style = $cell1->getStyle();
280
        $styleArray = $cell1style->exportArray();
281
        self::assertEquals(
282
            [
283
                'fillType' => Fill::FILL_PATTERN_GRAY125,
284
                'rotation' => 0.0,
285
            ],
286
            $styleArray['fill'],
287
            'applyFromArray without start/endColor'
288
        );
289
        $spreadsheet->disconnectWorksheets();
290
    }
291
292
    public function testQuotePrefix(): void
293
    {
294
        $spreadsheet = new Spreadsheet();
295
        $sheet = $spreadsheet->getActiveSheet();
296
        $sheet->getCell('A1')
297
            ->setValueExplicit('=1+2', DataType::TYPE_STRING);
298
        self::assertSame('=1+2', $sheet->getCell('A1')->getCalculatedValue());
299
        self::assertTrue($sheet->getStyle('A1')->getQuotePrefix());
300
        $sheet->getCell('A2')->setValue('=1+2');
301
        self::assertSame(3, $sheet->getCell('A2')->getCalculatedValue());
302
        self::assertFalse($sheet->getStyle('A2')->getQuotePrefix());
303
        $styleArray1 = $sheet->getStyle('A1')->exportArray();
304
        $styleArray2 = $sheet->getStyle('A2')->exportArray();
305
        $sheet->getStyle('B1')->applyFromArray($styleArray1);
306
        $sheet->getStyle('B2')->applyFromArray($styleArray2);
307
        self::assertTrue($sheet->getStyle('B1')->getQuotePrefix());
308
        self::assertFalse($sheet->getStyle('B2')->getQuotePrefix());
309
        $spreadsheet->disconnectWorksheets();
310
    }
311
}
312