Completed
Push — master ( 3af88c...d76c98 )
by Mewes
05:10
created

BasicTwigTest::testBlock()   A

Complexity

Conditions 2
Paths 9

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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 testRowIndex($format)
353
    {
354
        try {
355
            $document = $this->getDocument('rowIndex', $format);
356
            static::assertNotNull($document, 'Document does not exist');
357
358
            $sheet = $document->getSheetByName('Test');
359
            static::assertNotNull($sheet, 'Sheet does not exist');
360
361
            static::assertEquals('Foo', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
362
            static::assertNotEquals('Bar', $sheet->getCell('A3')->getValue(), 'Unexpected value in A3');
363
            static::assertEquals('Lorem', $sheet->getCell('A3')->getValue(), 'Unexpected value in A3');
364
            static::assertEquals('Ipsum', $sheet->getCell('A4')->getValue(), 'Unexpected value in A4');
365
            static::assertEquals('Hello', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2');
366
            static::assertEquals('World', $sheet->getCell('A5')->getValue(), 'Unexpected value in A5');
367
        } catch (Twig_Error_Runtime $e) {
368
            static::fail($e->getMessage());
369
        }
370
    }
371
372
    /**
373
     * @param string $format
374
     *
375
     * @throws \PHPExcel_Exception
376
     *
377
     * @dataProvider formatProvider
378
     */
379
    public function testSheetComplex($format)
380
    {
381
        try {
382
            $document = $this->getDocument('sheetComplex', $format);
383
            static::assertNotNull($document, 'Document does not exist');
384
385
            $sheet = $document->getSheetByName('Test 1');
386
            static::assertNotNull($sheet, 'Sheet "Test 1" does not exist');
387
            static::assertEquals('Foo', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
388
            static::assertEquals('Bar', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1');
389
390
            $sheet = $document->getSheetByName('Test 2');
391
            static::assertNotNull($sheet, 'Sheet "Test 2" does not exist');
392
            static::assertEquals('Hello World', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1');
393
        } catch (Twig_Error_Runtime $e) {
394
            static::fail($e->getMessage());
395
        }
396
    }
397
398
    /**
399
     * The following attributes are not supported by the readers and therefore cannot be tested:
400
     * $columnDimension->getAutoSize() -> false
401
     * $columnDimension->getCollapsed() -> true
402
     * $columnDimension->getColumnIndex() -> 1
403
     * $columnDimension->getVisible() -> false
404
     * $defaultColumnDimension->getAutoSize() -> true
405
     * $defaultColumnDimension->getCollapsed() -> false
406
     * $defaultColumnDimension->getColumnIndex() -> 1
407
     * $defaultColumnDimension->getVisible() -> true
408
     * $defaultRowDimension->getCollapsed() -> false
409
     * $defaultRowDimension->getRowIndex() -> 1
410
     * $defaultRowDimension->getVisible() -> true
411
     * $defaultRowDimension->getzeroHeight() -> false
412
     * $rowDimension->getCollapsed() -> true
413
     * $rowDimension->getRowIndex() -> 1
414
     * $rowDimension->getVisible() -> false
415
     * $rowDimension->getzeroHeight() -> true
416
     * $sheet->getShowGridlines() -> false
417
     *
418
     * @param string $format
419
     *
420
     * @throws \PHPExcel_Exception
421
     *
422
     * @dataProvider formatProvider
423
     */
424
    public function testSheetProperties($format)
425
    {
426
        try {
427
            $document = $this->getDocument('sheetProperties', $format);
428
            static::assertNotNull($document, 'Document does not exist');
429
430
            $sheet = $document->getSheetByName('Test');
431
            static::assertNotNull($sheet, 'Sheet does not exist');
432
433
            $defaultColumnDimension = $sheet->getDefaultColumnDimension();
434
            static::assertNotNull($defaultColumnDimension, 'DefaultColumnDimension does not exist');
435
            static::assertEquals(0, $defaultColumnDimension->getOutlineLevel(), 'Unexpected value in outlineLevel');
436
            static::assertEquals(-1, $defaultColumnDimension->getWidth(), 'Unexpected value in width');
437
            static::assertEquals(0, $defaultColumnDimension->getXfIndex(), 'Unexpected value in xfIndex');
438
439
            $columnDimension = $sheet->getColumnDimension('D');
440
            static::assertNotNull($columnDimension, 'ColumnDimension does not exist');
441
            static::assertEquals(0, $columnDimension->getXfIndex(), 'Unexpected value in xfIndex');
442
443
            $pageSetup = $sheet->getPageSetup();
444
            static::assertNotNull($pageSetup, 'PageSetup does not exist');
445
            static::assertEquals(1, $pageSetup->getFitToHeight(), 'Unexpected value in fitToHeight');
446
            static::assertFalse($pageSetup->getFitToPage(), 'Unexpected value in fitToPage');
447
            static::assertEquals(1, $pageSetup->getFitToWidth(), 'Unexpected value in fitToWidth');
448
            static::assertFalse($pageSetup->getHorizontalCentered(), 'Unexpected value in horizontalCentered');
449
            static::assertEquals(100, $pageSetup->getScale(), 'Unexpected value in scale');
450
            static::assertFalse($pageSetup->getVerticalCentered(), 'Unexpected value in verticalCentered');
451
452
            $defaultRowDimension = $sheet->getDefaultRowDimension();
453
            static::assertNotNull($defaultRowDimension, 'DefaultRowDimension does not exist');
454
            static::assertEquals(0, $defaultRowDimension->getOutlineLevel(), 'Unexpected value in outlineLevel');
455
            static::assertEquals(-1, $defaultRowDimension->getRowHeight(), 'Unexpected value in rowHeight');
456
            static::assertEquals(0, $defaultRowDimension->getXfIndex(), 'Unexpected value in xfIndex');
457
        } catch (Twig_Error_Runtime $e) {
458
            static::fail($e->getMessage());
459
        }
460
    }
461
}
462