Completed
Push — master ( 8581c9...e63334 )
by Mewes
9s
created

BasicTwigTest::testMacro()   B

Complexity

Conditions 2
Paths 17

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 29
rs 8.8571
cc 2
eloc 20
nc 17
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
183
            $sheet = $document->getSheetByName('Test3');
184
            static::assertNotNull($sheet, 'Sheet does not exist');
185
186
            static::assertEquals('Hello4', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
187
            static::assertEquals('World4', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
188
        } catch (Twig_Error_Runtime $e) {
189
            static::fail($e->getMessage());
190
        }
191
    }
192
193
    /**
194
     * @param string $format
195
     *
196
     * @throws \PHPExcel_Exception
197
     *
198
     * @dataProvider formatProvider
199
     */
200
    public function testCellIndex($format)
201
    {
202
        try {
203
            $document = $this->getDocument('cellIndex', $format);
204
            static::assertNotNull($document, 'Document does not exist');
205
206
            $sheet = $document->getSheetByName('Test');
207
            static::assertNotNull($sheet, 'Sheet does not exist');
208
209
            static::assertEquals('Foo', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
210
            static::assertNotEquals('Bar', $sheet->getCell('C1')->getValue(), 'Unexpected value in C1');
211
            static::assertEquals('Lorem', $sheet->getCell('C1')->getValue(), 'Unexpected value in C1');
212
            static::assertEquals('Ipsum', $sheet->getCell('D1')->getValue(), 'Unexpected value in D1');
213
            static::assertEquals('Hello', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
214
            static::assertEquals('World', $sheet->getCell('E1')->getValue(), 'Unexpected value in E1');
215
        } catch (Twig_Error_Runtime $e) {
216
            static::fail($e->getMessage());
217
        }
218
    }
219
220
    /**
221
     * @param string $format
222
     *
223
     * @throws \PHPExcel_Exception
224
     *
225
     * @dataProvider formatProvider
226
     */
227
    public function testCellProperties($format)
228
    {
229
        try {
230
            $document = $this->getDocument('cellProperties', $format);
231
            static::assertNotNull($document, 'Document does not exist');
232
233
            $sheet = $document->getSheetByName('Test');
234
            static::assertNotNull($sheet, 'Sheet does not exist');
235
236
            $cell = $sheet->getCell('A1');
237
            static::assertNotNull($cell, 'Cell does not exist');
238
239
            $dataValidation = $cell->getDataValidation();
240
            static::assertNotNull($dataValidation, 'DataValidation does not exist');
241
242
            $style = $cell->getStyle();
243
            static::assertNotNull($style, 'Style does not exist');
244
245
            static::assertEquals('s', $sheet->getCell('B1')->getDataType(), 'Unexpected value in dataType');
246
            static::assertEquals('n', $sheet->getCell('C1')->getDataType(), 'Unexpected value in dataType');
247
        } catch (Twig_Error_Runtime $e) {
248
            static::fail($e->getMessage());
249
        }
250
    }
251
252
    /**
253
     * @param string $format
254
     *
255
     * @throws \PHPExcel_Exception
256
     *
257
     * @dataProvider formatProvider
258
     */
259
    public function testCellFormula($format)
260
    {
261
        try {
262
            $document = $this->getDocument('cellFormula', $format);
263
            static::assertNotNull($document, 'Document does not exist');
264
265
            $sheet = $document->getSheetByName('Test');
266
            static::assertNotNull($sheet, 'Sheet does not exist');
267
268
            static::assertEquals('=A1*B1+2', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
269
            static::assertTrue($sheet->getCell('A2')->isFormula(), 'Unexpected value in isFormula');
270
            static::assertEquals(1337, $sheet->getCell('A2')->getCalculatedValue(), 'Unexpected calculated value in A2');
271
272
            static::assertEquals('=SUM(A1:B1)', $sheet->getCell('A3')->getValue(), 'Unexpected value in A3');
273
            static::assertTrue($sheet->getCell('A3')->isFormula(), 'Unexpected value in isFormula');
274
            static::assertEquals(669.5, $sheet->getCell('A3')->getCalculatedValue(), 'Unexpected calculated value in A3');
275
        } catch (Twig_Error_Runtime $e) {
276
            static::fail($e->getMessage());
277
        }
278
    }
279
280
    /**
281
     * The following attributes are not supported by the readers and therefore cannot be tested:
282
     * $security->getLockRevision() -> true
283
     * $security->getLockStructure() -> true
284
     * $security->getLockWindows() -> true
285
     * $security->getRevisionsPassword() -> 'test'
286
     * $security->getWorkbookPassword() -> 'test'
287
     *
288
     * @param string $format
289
     *
290
     * @throws \PHPExcel_Exception
291
     *
292
     * @dataProvider formatProvider
293
     */
294
    public function testDocumentProperties($format)
295
    {
296
        try {
297
            $document = $this->getDocument('documentProperties', $format);
298
            static::assertNotNull($document, 'Document does not exist');
299
300
            $properties = $document->getProperties();
301
            static::assertNotNull($properties, 'Properties do not exist');
302
303
            // +/- 24h range to allow possible timezone differences (946684800)
304
            static::assertGreaterThanOrEqual(946598400, $properties->getCreated(), 'Unexpected value in created');
305
            static::assertLessThanOrEqual(946771200, $properties->getCreated(), 'Unexpected value in created');
306
            static::assertEquals('Test creator', $properties->getCreator(), 'Unexpected value in creator');
307
308
            $defaultStyle = $document->getDefaultStyle();
309
            static::assertNotNull($defaultStyle, 'DefaultStyle does not exist');
310
311
            static::assertEquals('Test description', $properties->getDescription(), 'Unexpected value in description');
312
            // +/- 24h range to allow possible timezone differences (946684800)
313
            static::assertGreaterThanOrEqual(946598400, $properties->getModified(), 'Unexpected value in modified');
314
            static::assertLessThanOrEqual(946771200, $properties->getModified(), 'Unexpected value in modified');
315
316
            $security = $document->getSecurity();
317
            static::assertNotNull($security, 'Security does not exist');
318
319
            static::assertEquals('Test subject', $properties->getSubject(), 'Unexpected value in subject');
320
            static::assertEquals('Test title', $properties->getTitle(), 'Unexpected value in title');
321
        } catch (Twig_Error_Runtime $e) {
322
            static::fail($e->getMessage());
323
        }
324
    }
325
326
    /**
327
     * @param string $format
328
     *
329
     * @throws \PHPExcel_Exception
330
     *
331
     * @dataProvider formatProvider
332
     */
333
    public function testDocumentSimple($format)
334
    {
335
        try {
336
            $document = $this->getDocument('documentSimple', $format);
337
            static::assertNotNull($document, 'Document does not exist');
338
339
            $sheet = $document->getSheetByName('Test');
340
            static::assertNotNull($sheet, 'Sheet does not exist');
341
342
            static::assertEquals('Foo', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
343
            static::assertEquals('Bar', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
344
            static::assertEquals('Hello', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
345
            static::assertEquals('World', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
346
        } catch (Twig_Error_Runtime $e) {
347
            static::fail($e->getMessage());
348
        }
349
    }
350
351
    /**
352
     * @param string $format
353
     *
354
     * @throws \PHPExcel_Exception
355
     *
356
     * @dataProvider formatProvider
357
     */
358
    public function testDocumentTemplate($format)
359
    {
360
        try {
361
            $document = $this->getDocument('documentTemplate.' . $format, $format);
362
            static::assertNotNull($document, 'Document does not exist');
363
364
            $sheet = $document->getSheet(0);
365
            static::assertNotNull($sheet, 'Sheet does not exist');
366
367
            static::assertEquals('Hello2', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
368
            static::assertEquals('World', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
369
            static::assertEquals('Foo', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
370
            static::assertEquals('Bar2', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2');
371
        } catch (Twig_Error_Runtime $e) {
372
            static::fail($e->getMessage());
373
        }
374
    }
375
376
    /**
377
     * @param string $format
378
     *
379
     * @throws \PHPExcel_Exception
380
     *
381
     * @dataProvider formatProvider
382
     */
383
    public function testRowIndex($format)
384
    {
385
        try {
386
            $document = $this->getDocument('rowIndex', $format);
387
            static::assertNotNull($document, 'Document does not exist');
388
389
            $sheet = $document->getSheetByName('Test');
390
            static::assertNotNull($sheet, 'Sheet does not exist');
391
392
            static::assertEquals('Foo', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
393
            static::assertNotEquals('Bar', $sheet->getCell('A3')->getValue(), 'Unexpected value in A3');
394
            static::assertEquals('Lorem', $sheet->getCell('A3')->getValue(), 'Unexpected value in A3');
395
            static::assertEquals('Ipsum', $sheet->getCell('A4')->getValue(), 'Unexpected value in A4');
396
            static::assertEquals('Hello', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
397
            static::assertEquals('World', $sheet->getCell('A5')->getValue(), 'Unexpected value in A5');
398
        } catch (Twig_Error_Runtime $e) {
399
            static::fail($e->getMessage());
400
        }
401
    }
402
403
    /**
404
     * @param string $format
405
     *
406
     * @throws \PHPExcel_Exception
407
     *
408
     * @dataProvider formatProvider
409
     */
410
    public function testSheetComplex($format)
411
    {
412
        try {
413
            $document = $this->getDocument('sheetComplex', $format);
414
            static::assertNotNull($document, 'Document does not exist');
415
416
            $sheet = $document->getSheetByName('Test 1');
417
            static::assertNotNull($sheet, 'Sheet "Test 1" does not exist');
418
            static::assertEquals('Foo', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
419
            static::assertEquals('Bar', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
420
421
            $sheet = $document->getSheetByName('Test 2');
422
            static::assertNotNull($sheet, 'Sheet "Test 2" does not exist');
423
            static::assertEquals('Hello World', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
424
        } catch (Twig_Error_Runtime $e) {
425
            static::fail($e->getMessage());
426
        }
427
    }
428
429
    /**
430
     * The following attributes are not supported by the readers and therefore cannot be tested:
431
     * $columnDimension->getAutoSize() -> false
432
     * $columnDimension->getCollapsed() -> true
433
     * $columnDimension->getColumnIndex() -> 1
434
     * $columnDimension->getVisible() -> false
435
     * $defaultColumnDimension->getAutoSize() -> true
436
     * $defaultColumnDimension->getCollapsed() -> false
437
     * $defaultColumnDimension->getColumnIndex() -> 1
438
     * $defaultColumnDimension->getVisible() -> true
439
     * $defaultRowDimension->getCollapsed() -> false
440
     * $defaultRowDimension->getRowIndex() -> 1
441
     * $defaultRowDimension->getVisible() -> true
442
     * $defaultRowDimension->getzeroHeight() -> false
443
     * $rowDimension->getCollapsed() -> true
444
     * $rowDimension->getRowIndex() -> 1
445
     * $rowDimension->getVisible() -> false
446
     * $rowDimension->getzeroHeight() -> true
447
     * $sheet->getShowGridlines() -> false
448
     *
449
     * @param string $format
450
     *
451
     * @throws \PHPExcel_Exception
452
     *
453
     * @dataProvider formatProvider
454
     */
455
    public function testSheetProperties($format)
456
    {
457
        try {
458
            $document = $this->getDocument('sheetProperties', $format);
459
            static::assertNotNull($document, 'Document does not exist');
460
461
            $sheet = $document->getSheetByName('Test');
462
            static::assertNotNull($sheet, 'Sheet does not exist');
463
464
            $defaultColumnDimension = $sheet->getDefaultColumnDimension();
465
            static::assertNotNull($defaultColumnDimension, 'DefaultColumnDimension does not exist');
466
            static::assertEquals(0, $defaultColumnDimension->getOutlineLevel(), 'Unexpected value in outlineLevel');
467
            static::assertEquals(-1, $defaultColumnDimension->getWidth(), 'Unexpected value in width');
468
            static::assertEquals(0, $defaultColumnDimension->getXfIndex(), 'Unexpected value in xfIndex');
469
470
            $columnDimension = $sheet->getColumnDimension('D');
471
            static::assertNotNull($columnDimension, 'ColumnDimension does not exist');
472
            static::assertEquals(0, $columnDimension->getXfIndex(), 'Unexpected value in xfIndex');
473
474
            $pageSetup = $sheet->getPageSetup();
475
            static::assertNotNull($pageSetup, 'PageSetup does not exist');
476
            static::assertEquals(1, $pageSetup->getFitToHeight(), 'Unexpected value in fitToHeight');
477
            static::assertFalse($pageSetup->getFitToPage(), 'Unexpected value in fitToPage');
478
            static::assertEquals(1, $pageSetup->getFitToWidth(), 'Unexpected value in fitToWidth');
479
            static::assertFalse($pageSetup->getHorizontalCentered(), 'Unexpected value in horizontalCentered');
480
            static::assertEquals(100, $pageSetup->getScale(), 'Unexpected value in scale');
481
            static::assertFalse($pageSetup->getVerticalCentered(), 'Unexpected value in verticalCentered');
482
483
            $defaultRowDimension = $sheet->getDefaultRowDimension();
484
            static::assertNotNull($defaultRowDimension, 'DefaultRowDimension does not exist');
485
            static::assertEquals(0, $defaultRowDimension->getOutlineLevel(), 'Unexpected value in outlineLevel');
486
            static::assertEquals(-1, $defaultRowDimension->getRowHeight(), 'Unexpected value in rowHeight');
487
            static::assertEquals(0, $defaultRowDimension->getXfIndex(), 'Unexpected value in xfIndex');
488
        } catch (Twig_Error_Runtime $e) {
489
            static::fail($e->getMessage());
490
        }
491
    }
492
}
493