Completed
Push — master ( e63334...a00c3a )
by Mewes
09:18
created

BasicTwigTest::testCellProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 1
eloc 13
nc 1
nop 1
1
<?php
2
3
namespace MewesK\TwigExcelBundle\Tests\Twig;
4
5
/**
6
 * Class BasicTwigTest
7
 * @package MewesK\TwigExcelBundle\Tests\Twig
8
 */
9
class BasicTwigTest extends AbstractTwigTest
10
{
11
    protected static $TEMP_PATH = '/../../tmp/basic/';
12
13
    //
14
    // PhpUnit
15
    //
16
17
    /**
18
     * @return array
19
     */
20
    public function formatProvider()
21
    {
22
        return [['ods'], ['xls'], ['xlsx']];
23
    }
24
25
    //
26
    // Tests
27
    //
28
29
    /**
30
     * @param string $format
31
     * @throws \Exception
32
     * 
33
     * @dataProvider formatProvider
34
     */
35
    public function testBlock($format)
36
    {
37
        $document = $this->getDocument('block', $format);
38
        static::assertNotNull($document, 'Document does not exist');
39
40
        $sheet = $document->getSheetByName('Test');
41
        static::assertNotNull($sheet, 'Sheet does not exist');
42
43
        static::assertEquals('Hello', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
44
        static::assertEquals('World', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
45
        static::assertEquals('Foo', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
46
        static::assertEquals('Bar', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
47
    }
48
49
    /**
50
     * @param string $format
51
     * @throws \Exception
52
     *
53
     * @dataProvider formatProvider
54
     */
55
    public function testBlockOverrideCell($format)
56
    {
57
        $document = $this->getDocument('blockOverrideCell', $format);
58
        static::assertNotNull($document, 'Document does not exist');
59
60
        $sheet = $document->getSheetByName('Test');
61
        static::assertNotNull($sheet, 'Sheet does not exist');
62
63
        static::assertEquals('Hello', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
64
        static::assertEquals('World', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
65
        static::assertEquals('Foo', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
66
        static::assertEquals('Bar2', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
67
    }
68
69
    /**
70
     * @param string $format
71
     * @throws \Exception
72
     *
73
     * @dataProvider formatProvider
74
     */
75
    public function testBlockOverrideContent($format)
76
    {
77
        $document = $this->getDocument('blockOverrideContent', $format);
78
        static::assertNotNull($document, 'Document does not exist');
79
80
        $sheet = $document->getSheetByName('Test');
81
        static::assertNotNull($sheet, 'Sheet does not exist');
82
83
        static::assertEquals('Hello', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
84
        static::assertEquals('World', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
85
        static::assertEquals('Foo2', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
86
        static::assertEquals('Bar', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
87
    }
88
89
    /**
90
     * @param string $format
91
     * @throws \Exception
92
     *
93
     * @dataProvider formatProvider
94
     */
95
    public function testBlockOverrideRow($format)
96
    {
97
        $document = $this->getDocument('blockOverrideRow', $format);
98
        static::assertNotNull($document, 'Document does not exist');
99
100
        $sheet = $document->getSheetByName('Test');
101
        static::assertNotNull($sheet, 'Sheet does not exist');
102
103
        static::assertEquals('Hello2', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
104
        static::assertEquals('World2', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
105
        static::assertEquals('Foo', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
106
        static::assertEquals('Bar', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
107
    }
108
109
    /**
110
     * @param string $format
111
     * @throws \Exception
112
     *
113
     * @dataProvider formatProvider
114
     */
115
    public function testBlockOverrideSheet($format)
116
    {
117
        $document = $this->getDocument('blockOverrideSheet', $format);
118
        static::assertNotNull($document, 'Document does not exist');
119
120
        $sheet = $document->getSheetByName('Test2');
121
        static::assertNotNull($sheet, 'Sheet does not exist');
122
123
        static::assertEquals('Hello3', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
124
        static::assertEquals('World3', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
125
        static::assertNotEquals('Foo', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
126
        static::assertNotEquals('Bar', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
127
    }
128
129
    /**
130
     * @param string $format
131
     * @throws \Exception
132
     *
133
     * @dataProvider formatProvider
134
     */
135
    public function testMacro($format)
136
    {
137
        $document = $this->getDocument('macro', $format);
138
        static::assertNotNull($document, 'Document does not exist');
139
140
        $sheet = $document->getSheetByName('Test');
141
        static::assertNotNull($sheet, 'Sheet does not exist');
142
143
        static::assertEquals('Hello1', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
144
        static::assertEquals('World1', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
145
        static::assertEquals('Hello2', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
146
        static::assertEquals('World2', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
147
148
        $sheet = $document->getSheetByName('Test2');
149
        static::assertNotNull($sheet, 'Sheet does not exist');
150
151
        static::assertEquals('Hello3', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
152
        static::assertEquals('World3', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
153
154
        $sheet = $document->getSheetByName('Test3');
155
        static::assertNotNull($sheet, 'Sheet does not exist');
156
157
        static::assertEquals('Hello4', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
158
        static::assertEquals('World4', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
159
    }
160
161
    /**
162
     * @param string $format
163
     * @throws \Exception
164
     *
165
     * @dataProvider formatProvider
166
     */
167
    public function testCellIndex($format)
168
    {
169
        $document = $this->getDocument('cellIndex', $format);
170
        static::assertNotNull($document, 'Document does not exist');
171
172
        $sheet = $document->getSheetByName('Test');
173
        static::assertNotNull($sheet, 'Sheet does not exist');
174
175
        static::assertEquals('Foo', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
176
        static::assertNotEquals('Bar', $sheet->getCell('C1')->getValue(), 'Unexpected value in C1');
177
        static::assertEquals('Lorem', $sheet->getCell('C1')->getValue(), 'Unexpected value in C1');
178
        static::assertEquals('Ipsum', $sheet->getCell('D1')->getValue(), 'Unexpected value in D1');
179
        static::assertEquals('Hello', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
180
        static::assertEquals('World', $sheet->getCell('E1')->getValue(), 'Unexpected value in E1');
181
    }
182
183
    /**
184
     * @param string $format
185
     * @throws \Exception
186
     *
187
     * @dataProvider formatProvider
188
     */
189
    public function testCellProperties($format)
190
    {
191
        $document = $this->getDocument('cellProperties', $format);
192
        static::assertNotNull($document, 'Document does not exist');
193
194
        $sheet = $document->getSheetByName('Test');
195
        static::assertNotNull($sheet, 'Sheet does not exist');
196
197
        $cell = $sheet->getCell('A1');
198
        static::assertNotNull($cell, 'Cell does not exist');
199
200
        $dataValidation = $cell->getDataValidation();
201
        static::assertNotNull($dataValidation, 'DataValidation does not exist');
202
203
        $style = $cell->getStyle();
204
        static::assertNotNull($style, 'Style does not exist');
205
206
        static::assertEquals('s', $sheet->getCell('B1')->getDataType(), 'Unexpected value in dataType');
207
        static::assertEquals('n', $sheet->getCell('C1')->getDataType(), 'Unexpected value in dataType');
208
    }
209
210
    /**
211
     * @param string $format
212
     * @throws \Exception
213
     *
214
     * @dataProvider formatProvider
215
     */
216
    public function testCellFormula($format)
217
    {
218
        $document = $this->getDocument('cellFormula', $format);
219
        static::assertNotNull($document, 'Document does not exist');
220
221
        $sheet = $document->getSheetByName('Test');
222
        static::assertNotNull($sheet, 'Sheet does not exist');
223
224
        static::assertEquals('=A1*B1+2', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
225
        static::assertTrue($sheet->getCell('A2')->isFormula(), 'Unexpected value in isFormula');
226
        static::assertEquals(1337, $sheet->getCell('A2')->getCalculatedValue(), 'Unexpected calculated value in A2');
227
228
        static::assertEquals('=SUM(A1:B1)', $sheet->getCell('A3')->getValue(), 'Unexpected value in A3');
229
        static::assertTrue($sheet->getCell('A3')->isFormula(), 'Unexpected value in isFormula');
230
        static::assertEquals(669.5, $sheet->getCell('A3')->getCalculatedValue(), 'Unexpected calculated value in A3');
231
    }
232
233
    /**
234
     * The following attributes are not supported by the readers and therefore cannot be tested:
235
     * $security->getLockRevision() -> true
236
     * $security->getLockStructure() -> true
237
     * $security->getLockWindows() -> true
238
     * $security->getRevisionsPassword() -> 'test'
239
     * $security->getWorkbookPassword() -> 'test'
240
     *
241
     * @param string $format
242
     * @throws \Exception
243
     *
244
     * @dataProvider formatProvider
245
     */
246
    public function testDocumentProperties($format)
247
    {
248
        $document = $this->getDocument('documentProperties', $format);
249
        static::assertNotNull($document, 'Document does not exist');
250
251
        $properties = $document->getProperties();
252
        static::assertNotNull($properties, 'Properties do not exist');
253
254
        // +/- 24h range to allow possible timezone differences (946684800)
255
        static::assertGreaterThanOrEqual(946598400, $properties->getCreated(), 'Unexpected value in created');
256
        static::assertLessThanOrEqual(946771200, $properties->getCreated(), 'Unexpected value in created');
257
        static::assertEquals('Test creator', $properties->getCreator(), 'Unexpected value in creator');
258
259
        $defaultStyle = $document->getDefaultStyle();
260
        static::assertNotNull($defaultStyle, 'DefaultStyle does not exist');
261
262
        static::assertEquals('Test description', $properties->getDescription(), 'Unexpected value in description');
263
        // +/- 24h range to allow possible timezone differences (946684800)
264
        static::assertGreaterThanOrEqual(946598400, $properties->getModified(), 'Unexpected value in modified');
265
        static::assertLessThanOrEqual(946771200, $properties->getModified(), 'Unexpected value in modified');
266
267
        $security = $document->getSecurity();
268
        static::assertNotNull($security, 'Security does not exist');
269
270
        static::assertEquals('Test subject', $properties->getSubject(), 'Unexpected value in subject');
271
        static::assertEquals('Test title', $properties->getTitle(), 'Unexpected value in title');
272
    }
273
274
    /**
275
     * @param string $format
276
     * @throws \Exception
277
     *
278
     * @dataProvider formatProvider
279
     */
280
    public function testDocumentSimple($format)
281
    {
282
        $document = $this->getDocument('documentSimple', $format);
283
        static::assertNotNull($document, 'Document does not exist');
284
285
        $sheet = $document->getSheetByName('Test');
286
        static::assertNotNull($sheet, 'Sheet does not exist');
287
288
        static::assertEquals('Foo', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
289
        static::assertEquals('Bar', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
290
        static::assertEquals('Hello', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
291
        static::assertEquals('World', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
292
    }
293
294
    /**
295
     * @param string $format
296
     * @throws \Exception
297
     *
298
     * @dataProvider formatProvider
299
     */
300
    public function testDocumentTemplate($format)
301
    {
302
        $document = $this->getDocument('documentTemplate.' . $format, $format);
303
        static::assertNotNull($document, 'Document does not exist');
304
305
        $sheet = $document->getSheet(0);
306
        static::assertNotNull($sheet, 'Sheet does not exist');
307
308
        static::assertEquals('Hello2', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
309
        static::assertEquals('World', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
310
        static::assertEquals('Foo', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
311
        static::assertEquals('Bar2', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
312
    }
313
314
    /**
315
     * @param string $format
316
     * @throws \Exception
317
     *
318
     * @dataProvider formatProvider
319
     */
320
    public function testRowIndex($format)
321
    {
322
        $document = $this->getDocument('rowIndex', $format);
323
        static::assertNotNull($document, 'Document does not exist');
324
325
        $sheet = $document->getSheetByName('Test');
326
        static::assertNotNull($sheet, 'Sheet does not exist');
327
328
        static::assertEquals('Foo', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
329
        static::assertNotEquals('Bar', $sheet->getCell('A3')->getValue(), 'Unexpected value in A3');
330
        static::assertEquals('Lorem', $sheet->getCell('A3')->getValue(), 'Unexpected value in A3');
331
        static::assertEquals('Ipsum', $sheet->getCell('A4')->getValue(), 'Unexpected value in A4');
332
        static::assertEquals('Hello', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
333
        static::assertEquals('World', $sheet->getCell('A5')->getValue(), 'Unexpected value in A5');
334
    }
335
336
    /**
337
     * @param string $format
338
     * @throws \Exception
339
     *
340
     * @dataProvider formatProvider
341
     */
342
    public function testSheetComplex($format)
343
    {
344
        $document = $this->getDocument('sheetComplex', $format);
345
        static::assertNotNull($document, 'Document does not exist');
346
347
        $sheet = $document->getSheetByName('Test 1');
348
        static::assertNotNull($sheet, 'Sheet "Test 1" does not exist');
349
        static::assertEquals('Foo', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
350
        static::assertEquals('Bar', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
351
352
        $sheet = $document->getSheetByName('Test 2');
353
        static::assertNotNull($sheet, 'Sheet "Test 2" does not exist');
354
        static::assertEquals('Hello World', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
355
    }
356
357
    /**
358
     * The following attributes are not supported by the readers and therefore cannot be tested:
359
     * $columnDimension->getAutoSize() -> false
360
     * $columnDimension->getCollapsed() -> true
361
     * $columnDimension->getColumnIndex() -> 1
362
     * $columnDimension->getVisible() -> false
363
     * $defaultColumnDimension->getAutoSize() -> true
364
     * $defaultColumnDimension->getCollapsed() -> false
365
     * $defaultColumnDimension->getColumnIndex() -> 1
366
     * $defaultColumnDimension->getVisible() -> true
367
     * $defaultRowDimension->getCollapsed() -> false
368
     * $defaultRowDimension->getRowIndex() -> 1
369
     * $defaultRowDimension->getVisible() -> true
370
     * $defaultRowDimension->getzeroHeight() -> false
371
     * $rowDimension->getCollapsed() -> true
372
     * $rowDimension->getRowIndex() -> 1
373
     * $rowDimension->getVisible() -> false
374
     * $rowDimension->getzeroHeight() -> true
375
     * $sheet->getShowGridlines() -> false
376
     *
377
     * @param string $format
378
     * @throws \Exception
379
     *
380
     * @dataProvider formatProvider
381
     */
382
    public function testSheetProperties($format)
383
    {
384
        $document = $this->getDocument('sheetProperties', $format);
385
        static::assertNotNull($document, 'Document does not exist');
386
387
        $sheet = $document->getSheetByName('Test');
388
        static::assertNotNull($sheet, 'Sheet does not exist');
389
390
        $defaultColumnDimension = $sheet->getDefaultColumnDimension();
391
        static::assertNotNull($defaultColumnDimension, 'DefaultColumnDimension does not exist');
392
        static::assertEquals(0, $defaultColumnDimension->getOutlineLevel(), 'Unexpected value in outlineLevel');
393
        static::assertEquals(-1, $defaultColumnDimension->getWidth(), 'Unexpected value in width');
394
        static::assertEquals(0, $defaultColumnDimension->getXfIndex(), 'Unexpected value in xfIndex');
395
396
        $columnDimension = $sheet->getColumnDimension('D');
397
        static::assertNotNull($columnDimension, 'ColumnDimension does not exist');
398
        static::assertEquals(0, $columnDimension->getXfIndex(), 'Unexpected value in xfIndex');
399
400
        $pageSetup = $sheet->getPageSetup();
401
        static::assertNotNull($pageSetup, 'PageSetup does not exist');
402
        static::assertEquals(1, $pageSetup->getFitToHeight(), 'Unexpected value in fitToHeight');
403
        static::assertFalse($pageSetup->getFitToPage(), 'Unexpected value in fitToPage');
404
        static::assertEquals(1, $pageSetup->getFitToWidth(), 'Unexpected value in fitToWidth');
405
        static::assertFalse($pageSetup->getHorizontalCentered(), 'Unexpected value in horizontalCentered');
406
        static::assertEquals(100, $pageSetup->getScale(), 'Unexpected value in scale');
407
        static::assertFalse($pageSetup->getVerticalCentered(), 'Unexpected value in verticalCentered');
408
409
        $defaultRowDimension = $sheet->getDefaultRowDimension();
410
        static::assertNotNull($defaultRowDimension, 'DefaultRowDimension does not exist');
411
        static::assertEquals(0, $defaultRowDimension->getOutlineLevel(), 'Unexpected value in outlineLevel');
412
        static::assertEquals(-1, $defaultRowDimension->getRowHeight(), 'Unexpected value in rowHeight');
413
        static::assertEquals(0, $defaultRowDimension->getXfIndex(), 'Unexpected value in xfIndex');
414
    }
415
}
416