Completed
Push — master ( 25fb08...38f401 )
by Mewes
02:25
created

BasicTwigTest::testDocumentTemplate()   A

Complexity

Conditions 2
Paths 9

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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