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