Completed
Push — master ( 0b8854...d64c20 )
by Mewes
02:29
created

XlsTwigTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 249
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 8
c 4
b 0
f 1
lcom 1
cbo 6
dl 0
loc 249
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A formatProvider() 0 4 1
A testCellIndex() 0 11 1
A testCellProperties() 0 20 1
A testDocumentProperties() 0 15 1
A testDrawingProperties() 0 23 1
A testDrawingSimple() 0 17 1
A testHeaderFooterComplex() 0 14 1
A testSheetProperties() 0 54 1
1
<?php
2
3
namespace MewesK\TwigExcelBundle\Tests\Twig;
4
5
/**
6
 * Class XlsTwigTest
7
 * @package MewesK\TwigExcelBundle\Tests\Twig
8
 */
9
class XlsTwigTest extends AbstractTwigTest
10
{
11
    protected static $TEMP_PATH = '/../../tmp/xls/';
12
13
    //
14
    // PhpUnit
15
    //
16
17
    /**
18
     * @return array
19
     */
20
    public function formatProvider()
21
    {
22
        return [['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 testCellIndex($format)
36
    {
37
        $document = $this->getDocument('cellIndex', $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('A2:C2', $sheet->getCell('A2')->getMergeRange(), 'Unexpected value in mergeRange');
44
        static::assertEquals('A3:C3', $sheet->getCell('A3')->getMergeRange(), 'Unexpected value in mergeRange');
45
    }
46
47
    /**
48
     * @param string $format
49
     * @throws \Exception
50
     *
51
     * @dataProvider formatProvider
52
     */
53
    public function testCellProperties($format)
54
    {
55
        $document = $this->getDocument('cellProperties', $format);
56
        $sheet = $document->getSheetByName('Test');
57
        $cell = $sheet->getCell('A1');
58
        $style = $cell->getStyle();
59
60
        $breaks = $sheet->getBreaks();
61
        static::assertCount(1, $breaks, 'Unexpected break count');
62
        static::assertArrayHasKey('A1', $breaks, 'Break does not exist');
63
64
        $break = $breaks['A1'];
65
        static::assertNotNull($break, 'Break is null');
66
67
        $font = $style->getFont();
68
        static::assertNotNull($font, 'Font does not exist');
69
        static::assertEquals(18, $font->getSize(), 'Unexpected value in size');
70
71
        static::assertEquals('http://example.com/', $cell->getHyperlink()->getUrl(), 'Unexpected value in url');
72
    }
73
74
    /**
75
     * The following attributes are not supported by the readers and therefore cannot be tested:
76
     * $security->getLockRevision() -> true
77
     * $security->getLockStructure() -> true
78
     * $security->getLockWindows() -> true
79
     * $security->getRevisionsPassword() -> 'test'
80
     * $security->getWorkbookPassword() -> 'test'
81
     *
82
     * @param string $format
83
     * @throws \Exception
84
     *
85
     * @dataProvider formatProvider
86
     */
87
    public function testDocumentProperties($format)
88
    {
89
        $document = $this->getDocument('documentProperties', $format);
90
        $properties = $document->getProperties();
91
        $defaultStyle = $document->getDefaultStyle();
92
93
        static::assertEquals('Test category', $properties->getCategory(), 'Unexpected value in category');
94
95
        $font = $defaultStyle->getFont();
96
        static::assertNotNull($font, 'Font does not exist');
97
        static::assertEquals(18, $font->getSize(), 'Unexpected value in size');
98
99
        static::assertEquals('Test keywords', $properties->getKeywords(), 'Unexpected value in keywords');
100
        static::assertEquals('Test modifier', $properties->getLastModifiedBy(), 'Unexpected value in lastModifiedBy');
101
    }
102
103
    /**
104
     * @param string $format
105
     * @throws \Exception
106
     *
107
     * @dataProvider formatProvider
108
     */
109
    public function testDrawingProperties($format)
110
    {
111
        $document = $this->getDocument('drawingProperties', $format);
112
        static::assertNotNull($document, 'Document does not exist');
113
114
        $sheet = $document->getSheetByName('Test');
115
        static::assertNotNull($sheet, 'Sheet does not exist');
116
117
        $drawings = $sheet->getDrawingCollection();
118
        static::assertCount(1, $drawings, 'Unexpected drawing count');
119
        static::assertArrayHasKey(0, $drawings, 'Drawing does not exist');
120
121
        $drawing = $drawings[0];
122
        static::assertNotNull($drawing, 'Drawing is null');
123
124
        static::assertEquals('B2', $drawing->getCoordinates(), 'Unexpected value in coordinates');
125
        static::assertEquals(200, $drawing->getHeight(), 'Unexpected value in height');
126
        static::assertFalse($drawing->getResizeProportional(), 'Unexpected value in resizeProportional');
127
        static::assertEquals(300, $drawing->getWidth(), 'Unexpected value in width');
128
129
        $shadow = $drawing->getShadow();
130
        static::assertNotNull($shadow, 'Shadow is null');
131
    }
132
133
    /**
134
     * @param string $format
135
     * @throws \Exception
136
     *
137
     * @dataProvider formatProvider
138
     */
139
    public function testDrawingSimple($format)
140
    {
141
        $document = $this->getDocument('drawingSimple', $format);
142
        static::assertNotNull($document, 'Document does not exist');
143
144
        $sheet = $document->getSheetByName('Test');
145
        static::assertNotNull($sheet, 'Sheet does not exist');
146
147
        $drawings = $sheet->getDrawingCollection();
148
        static::assertCount(1, $drawings, 'Unexpected drawing count');
149
        static::assertArrayHasKey(0, $drawings, 'Drawing does not exist');
150
151
        $drawing = $drawings[0];
152
        static::assertNotNull($drawing, 'Drawing is null');
153
        static::assertEquals(100, $drawing->getWidth(), 'Unexpected value in width');
154
        static::assertEquals(100, $drawing->getHeight(), 'Unexpected value in height');
155
    }
156
157
    /**
158
     * @param string $format
159
     * @throws \Exception
160
     *
161
     * @dataProvider formatProvider
162
     */
163
    public function testHeaderFooterComplex($format)
164
    {
165
        $document = $this->getDocument('headerFooterComplex', $format);
166
        static::assertNotNull($document, 'Document does not exist');
167
168
        $sheet = $document->getSheetByName('Test');
169
        static::assertNotNull($sheet, 'Sheet does not exist');
170
171
        $headerFooter = $sheet->getHeaderFooter();
172
        static::assertNotNull($headerFooter, 'HeaderFooter does not exist');
173
174
        static::assertEquals('&LoddHeader left&CoddHeader center&RoddHeader right', $headerFooter->getOddHeader(), 'Unexpected value in oddHeader');
175
        static::assertEquals('&LoddFooter left&CoddFooter center&RoddFooter right', $headerFooter->getOddFooter(), 'Unexpected value in oddFooter');
176
    }
177
178
    /**
179
     * The following attributes are not supported by the readers and therefore cannot be tested:
180
     * $columnDimension->getAutoSize() -> false
181
     * $columnDimension->getCollapsed() -> true
182
     * $columnDimension->getColumnIndex() -> 1
183
     * $columnDimension->getVisible() -> false
184
     * $defaultColumnDimension->getAutoSize() -> true
185
     * $defaultColumnDimension->getCollapsed() -> false
186
     * $defaultColumnDimension->getColumnIndex() -> 1
187
     * $defaultColumnDimension->getVisible() -> true
188
     * $defaultRowDimension->getCollapsed() -> false
189
     * $defaultRowDimension->getRowIndex() -> 1
190
     * $defaultRowDimension->getVisible() -> true
191
     * $defaultRowDimension->getzeroHeight() -> false
192
     * $rowDimension->getCollapsed() -> true
193
     * $rowDimension->getRowIndex() -> 1
194
     * $rowDimension->getVisible() -> false
195
     * $rowDimension->getzeroHeight() -> true
196
     * $sheet->getShowGridlines() -> false
197
     *
198
     * @param string $format
199
     * @throws \Exception
200
     *
201
     * @dataProvider formatProvider
202
     */
203
    public function testSheetProperties($format)
204
    {
205
        $document = $this->getDocument('sheetProperties', $format);
206
        $sheet = $document->getSheetByName('Test');
207
        $columnDimension = $sheet->getColumnDimension('D');
208
        $pageSetup = $sheet->getPageSetup();
209
210
        static::assertEquals(1, $columnDimension->getOutlineLevel(), 'Unexpected value in outlineLevel');
211
        static::assertEquals(200, $columnDimension->getWidth(), 'Unexpected value in width');
212
213
        $pageMargins = $sheet->getPageMargins();
214
        static::assertNotNull($pageMargins, 'PageMargins does not exist');
215
        static::assertEquals(1, $pageMargins->getTop(), 'Unexpected value in top');
216
        static::assertEquals(1, $pageMargins->getBottom(), 'Unexpected value in bottom');
217
        static::assertEquals(0.75, $pageMargins->getLeft(), 'Unexpected value in left');
218
        static::assertEquals(0.75, $pageMargins->getRight(), 'Unexpected value in right');
219
        static::assertEquals(0.5, $pageMargins->getHeader(), 'Unexpected value in header');
220
        static::assertEquals(0.5, $pageMargins->getFooter(), 'Unexpected value in footer');
221
222
        static::assertEquals('landscape', $pageSetup->getOrientation(), 'Unexpected value in orientation');
223
        static::assertEquals(9, $pageSetup->getPaperSize(), 'Unexpected value in paperSize');
224
        static::assertEquals('A1:B1', $pageSetup->getPrintArea(), 'Unexpected value in printArea');
225
226
        $protection = $sheet->getProtection();
227
        static::assertTrue($protection->getAutoFilter(), 'Unexpected value in autoFilter');
228
        static::assertNotNull($protection, 'Protection does not exist');
229
        static::assertTrue($protection->getDeleteColumns(), 'Unexpected value in deleteColumns');
230
        static::assertTrue($protection->getDeleteRows(), 'Unexpected value in deleteRows');
231
        static::assertTrue($protection->getFormatCells(), 'Unexpected value in formatCells');
232
        static::assertTrue($protection->getFormatColumns(), 'Unexpected value in formatColumns');
233
        static::assertTrue($protection->getFormatRows(), 'Unexpected value in formatRows');
234
        static::assertTrue($protection->getInsertColumns(), 'Unexpected value in insertColumns');
235
        static::assertTrue($protection->getInsertHyperlinks(), 'Unexpected value in insertHyperlinks');
236
        static::assertTrue($protection->getInsertRows(), 'Unexpected value in insertRows');
237
        static::assertTrue($protection->getObjects(), 'Unexpected value in objects');
238
        static::assertEquals(\PHPExcel_Shared_PasswordHasher::hashPassword('testpassword'), $protection->getPassword(), 'Unexpected value in password');
239
        static::assertTrue($protection->getPivotTables(), 'Unexpected value in pivotTables');
240
        static::assertTrue($protection->getScenarios(), 'Unexpected value in scenarios');
241
        static::assertTrue($protection->getSelectLockedCells(), 'Unexpected value in selectLockedCells');
242
        static::assertTrue($protection->getSelectUnlockedCells(), 'Unexpected value in selectUnlockedCells');
243
        static::assertTrue($protection->getSheet(), 'Unexpected value in sheet');
244
        static::assertTrue($protection->getSort(), 'Unexpected value in sort');
245
246
        static::assertTrue($sheet->getPrintGridlines(), 'Unexpected value in printGridlines');
247
        static::assertTrue($sheet->getRightToLeft(), 'Unexpected value in rightToLeft');
248
        static::assertEquals('c0c0c0', strtolower($sheet->getTabColor()->getRGB()), 'Unexpected value in tabColor');
249
        static::assertEquals(75, $sheet->getSheetView()->getZoomScale(), 'Unexpected value in zoomScale');
250
251
        $rowDimension = $sheet->getRowDimension(2);
252
        static::assertNotNull($rowDimension, 'RowDimension does not exist');
253
        static::assertEquals(1, $rowDimension->getOutlineLevel(), 'Unexpected value in outlineLevel');
254
        static::assertEquals(30, $rowDimension->getRowHeight(), 'Unexpected value in rowHeight');
255
        static::assertEquals(0, $rowDimension->getXfIndex(), 'Unexpected value in xfIndex');
256
    }
257
}
258