1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MewesK\TwigExcelBundle\Tests\Twig; |
4
|
|
|
|
5
|
|
|
use Twig_Error_Runtime; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class XlsxTwigTest |
9
|
|
|
* @package MewesK\TwigExcelBundle\Tests\Twig |
10
|
|
|
*/ |
11
|
|
|
class XlsxTwigTest extends AbstractTwigTest |
12
|
|
|
{ |
13
|
|
|
protected static $TEMP_PATH = '/../../tmp/xlsx/'; |
14
|
|
|
|
15
|
|
|
// |
16
|
|
|
// PhpUnit |
17
|
|
|
// |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
public function formatProvider() |
23
|
|
|
{ |
24
|
|
|
return [['xlsx']]; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// |
28
|
|
|
// Tests |
29
|
|
|
// |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $format |
34
|
|
|
* |
35
|
|
|
* @throws \PHPExcel_Exception |
36
|
|
|
* |
37
|
|
|
* @dataProvider formatProvider |
38
|
|
|
*/ |
39
|
|
|
public function testCellProperties($format) |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
|
|
$document = $this->getDocument('cellProperties', $format); |
43
|
|
|
$sheet = $document->getSheetByName('Test'); |
44
|
|
|
$cell = $sheet->getCell('A1'); |
45
|
|
|
$dataValidation = $cell->getDataValidation(); |
46
|
|
|
|
47
|
|
|
static::assertTrue($dataValidation->getAllowBlank(), 'Unexpected value in allowBlank'); |
48
|
|
|
static::assertEquals('Test error', $dataValidation->getError(), 'Unexpected value in error'); |
49
|
|
|
static::assertEquals('information', $dataValidation->getErrorStyle(), 'Unexpected value in errorStyle'); |
50
|
|
|
static::assertEquals('Test errorTitle', $dataValidation->getErrorTitle(), 'Unexpected value in errorTitle'); |
51
|
|
|
static::assertEquals('', $dataValidation->getFormula1(), 'Unexpected value in formula1'); |
52
|
|
|
static::assertEquals('', $dataValidation->getFormula2(), 'Unexpected value in formula2'); |
53
|
|
|
static::assertEquals('', $dataValidation->getOperator(), 'Unexpected value in operator'); |
54
|
|
|
static::assertEquals('Test prompt', $dataValidation->getPrompt(), 'Unexpected value in prompt'); |
55
|
|
|
static::assertEquals('Test promptTitle', $dataValidation->getPromptTitle(), 'Unexpected value in promptTitle'); |
56
|
|
|
static::assertTrue($dataValidation->getShowDropDown(), 'Unexpected value in showDropDown'); |
57
|
|
|
static::assertTrue($dataValidation->getShowErrorMessage(), 'Unexpected value in showErrorMessage'); |
58
|
|
|
static::assertTrue($dataValidation->getShowInputMessage(), 'Unexpected value in showInputMessage'); |
59
|
|
|
static::assertEquals('custom', $dataValidation->getType(), 'Unexpected value in type'); |
60
|
|
|
} catch (Twig_Error_Runtime $e) { |
61
|
|
|
static::fail($e->getMessage()); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The following attributes are not supported by the readers and therefore cannot be tested: |
67
|
|
|
* $security->getLockRevision() -> true |
68
|
|
|
* $security->getLockStructure() -> true |
69
|
|
|
* $security->getLockWindows() -> true |
70
|
|
|
* $security->getRevisionsPassword() -> 'test' |
71
|
|
|
* $security->getWorkbookPassword() -> 'test' |
72
|
|
|
* |
73
|
|
|
* @param string $format |
74
|
|
|
* |
75
|
|
|
* @throws \PHPExcel_Exception |
76
|
|
|
* |
77
|
|
|
* @dataProvider formatProvider |
78
|
|
|
*/ |
79
|
|
|
public function testDocumentProperties($format) |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
|
|
$document = $this->getDocument('documentProperties', $format); |
83
|
|
|
$properties = $document->getProperties(); |
84
|
|
|
|
85
|
|
|
static::assertEquals('Test company', $properties->getCompany(), 'Unexpected value in company'); |
86
|
|
|
static::assertEquals('Test manager', $properties->getManager(), 'Unexpected value in manager'); |
87
|
|
|
} catch (Twig_Error_Runtime $e) { |
88
|
|
|
static::fail($e->getMessage()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $format |
94
|
|
|
* |
95
|
|
|
* @throws \PHPExcel_Exception |
96
|
|
|
* |
97
|
|
|
* @dataProvider formatProvider |
98
|
|
|
*/ |
99
|
|
|
public function testDocumentTemplate($format) |
100
|
|
|
{ |
101
|
|
|
try { |
102
|
|
|
$document = $this->getDocument('documentTemplateAdvanced', $format); |
103
|
|
|
static::assertNotNull($document, 'Document does not exist'); |
104
|
|
|
|
105
|
|
|
$sheet = $document->getSheet(0); |
106
|
|
|
static::assertNotNull($sheet, 'Sheet does not exist'); |
107
|
|
|
|
108
|
|
|
static::assertEquals('Hello2', $sheet->getCell('A1')->getValue(), 'Unexpected value in A1'); |
109
|
|
|
static::assertEquals('World', $sheet->getCell('B1')->getValue(), 'Unexpected value in B1'); |
110
|
|
|
static::assertEquals('Foo', $sheet->getCell('A2')->getValue(), 'Unexpected value in A2'); |
111
|
|
|
static::assertEquals('Bar2', $sheet->getCell('B2')->getValue(), 'Unexpected value in B2'); |
112
|
|
|
|
113
|
|
|
static::assertTrue($sheet->getCell('A1')->getStyle()->getFont()->getBold(), 'Unexpected value in bold'); |
114
|
|
|
static::assertTrue($sheet->getCell('B1')->getStyle()->getFont()->getItalic(), 'Unexpected value in italic'); |
115
|
|
|
static::assertEquals('single', $sheet->getCell('A2')->getStyle()->getFont()->getUnderline(), 'Unexpected value in underline'); |
116
|
|
|
static::assertEquals('FFFF3333', $sheet->getCell('B2')->getStyle()->getFont()->getColor()->getARGB(), 'Unexpected value in color'); |
117
|
|
|
|
118
|
|
|
$headerFooter = $sheet->getHeaderFooter(); |
119
|
|
|
static::assertNotNull($headerFooter, 'HeaderFooter does not exist'); |
120
|
|
|
static::assertContains('Left area header', |
121
|
|
|
$headerFooter->getOddHeader(), |
122
|
|
|
'Unexpected value in oddHeader'); |
123
|
|
|
static::assertContains('12Center area header', |
124
|
|
|
$headerFooter->getOddHeader(), |
125
|
|
|
'Unexpected value in oddHeader'); |
126
|
|
|
static::assertContains('12Right area header', |
127
|
|
|
$headerFooter->getOddHeader(), |
128
|
|
|
'Unexpected value in oddHeader'); |
129
|
|
|
static::assertContains('Left area footer', |
130
|
|
|
$headerFooter->getOddFooter(), |
131
|
|
|
'Unexpected value in oddFooter'); |
132
|
|
|
static::assertContains('12Center area footer', |
133
|
|
|
$headerFooter->getOddFooter(), |
134
|
|
|
'Unexpected value in oddFooter'); |
135
|
|
|
static::assertContains('12Right area footer', |
136
|
|
|
$headerFooter->getOddFooter(), |
137
|
|
|
'Unexpected value in oddFooter'); |
138
|
|
|
|
139
|
|
|
$drawings = $sheet->getDrawingCollection(); |
140
|
|
|
static::assertCount(1, $drawings, 'Not enough drawings exist'); |
141
|
|
|
|
142
|
|
|
$drawing = $drawings[0]; |
143
|
|
|
static::assertEquals(196, $drawing->getWidth(), 'Unexpected value in width'); |
144
|
|
|
static::assertEquals(187, $drawing->getHeight(), 'Unexpected value in height'); |
145
|
|
|
} catch (Twig_Error_Runtime $e) { |
146
|
|
|
static::fail($e->getMessage()); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string $format |
152
|
|
|
* |
153
|
|
|
* @dataProvider formatProvider |
154
|
|
|
*/ |
155
|
|
|
public function testDrawingProperties($format) |
156
|
|
|
{ |
157
|
|
|
try { |
158
|
|
|
$document = $this->getDocument('drawingProperties', $format); |
159
|
|
|
static::assertNotNull($document, 'Document does not exist'); |
160
|
|
|
|
161
|
|
|
$sheet = $document->getSheetByName('Test'); |
162
|
|
|
static::assertNotNull($sheet, 'Sheet does not exist'); |
163
|
|
|
|
164
|
|
|
$drawings = $sheet->getDrawingCollection(); |
165
|
|
|
static::assertCount(1, $drawings, 'Not enough drawings exist'); |
166
|
|
|
|
167
|
|
|
$drawing = $drawings[0]; |
168
|
|
|
static::assertEquals('Test Description', $drawing->getDescription(), 'Unexpected value in description'); |
169
|
|
|
static::assertEquals('Test Name', $drawing->getName(), 'Unexpected value in name'); |
170
|
|
|
static::assertEquals(30, $drawing->getOffsetX(), 'Unexpected value in offsetX'); |
171
|
|
|
static::assertEquals(20, $drawing->getOffsetY(), 'Unexpected value in offsetY'); |
172
|
|
|
static::assertEquals(45, $drawing->getRotation(), 'Unexpected value in rotation'); |
173
|
|
|
|
174
|
|
|
$shadow = $drawing->getShadow(); |
175
|
|
|
static::assertEquals('ctr', $shadow->getAlignment(), 'Unexpected value in alignment'); |
176
|
|
|
static::assertEquals(100, $shadow->getAlpha(), 'Unexpected value in alpha'); |
177
|
|
|
static::assertEquals(11, $shadow->getBlurRadius(), 'Unexpected value in blurRadius'); |
178
|
|
|
static::assertEquals('0000cc', $shadow->getColor()->getRGB(), 'Unexpected value in color'); |
179
|
|
|
static::assertEquals(30, $shadow->getDirection(), 'Unexpected value in direction'); |
180
|
|
|
static::assertEquals(4, $shadow->getDistance(), 'Unexpected value in distance'); |
181
|
|
|
static::assertTrue($shadow->getVisible(), 'Unexpected value in visible'); |
182
|
|
|
} catch (Twig_Error_Runtime $e) { |
183
|
|
|
static::fail($e->getMessage()); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $format |
189
|
|
|
* |
190
|
|
|
* @dataProvider formatProvider |
191
|
|
|
*/ |
192
|
|
|
public function testHeaderFooterComplex($format) |
193
|
|
|
{ |
194
|
|
|
try { |
195
|
|
|
$document = $this->getDocument('headerFooterComplex', $format); |
196
|
|
|
static::assertNotNull($document, 'Document does not exist'); |
197
|
|
|
|
198
|
|
|
$sheet = $document->getSheetByName('Test'); |
199
|
|
|
static::assertNotNull($sheet, 'Sheet does not exist'); |
200
|
|
|
|
201
|
|
|
$headerFooter = $sheet->getHeaderFooter(); |
202
|
|
|
static::assertNotNull($headerFooter, 'HeaderFooter does not exist'); |
203
|
|
|
static::assertEquals('&LfirstHeader left&CfirstHeader center&RfirstHeader right', |
204
|
|
|
$headerFooter->getFirstHeader(), |
205
|
|
|
'Unexpected value in firstHeader'); |
206
|
|
|
static::assertEquals('&LevenHeader left&CevenHeader center&RevenHeader right', |
207
|
|
|
$headerFooter->getEvenHeader(), |
208
|
|
|
'Unexpected value in evenHeader'); |
209
|
|
|
static::assertEquals('&LfirstFooter left&CfirstFooter center&RfirstFooter right', |
210
|
|
|
$headerFooter->getFirstFooter(), |
211
|
|
|
'Unexpected value in firstFooter'); |
212
|
|
|
static::assertEquals('&LevenFooter left&CevenFooter center&RevenFooter right', |
213
|
|
|
$headerFooter->getEvenFooter(), |
214
|
|
|
'Unexpected value in evenFooter'); |
215
|
|
|
} catch (Twig_Error_Runtime $e) { |
216
|
|
|
static::fail($e->getMessage()); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $format |
222
|
|
|
* |
223
|
|
|
* @dataProvider formatProvider |
224
|
|
|
*/ |
225
|
|
|
public function testHeaderFooterDrawing($format) |
226
|
|
|
{ |
227
|
|
|
try { |
228
|
|
|
$document = $this->getDocument('headerFooterDrawing', $format); |
229
|
|
|
static::assertNotNull($document, 'Document does not exist'); |
230
|
|
|
|
231
|
|
|
$sheet = $document->getSheetByName('Test'); |
232
|
|
|
static::assertNotNull($sheet, 'Sheet does not exist'); |
233
|
|
|
|
234
|
|
|
$headerFooter = $sheet->getHeaderFooter(); |
235
|
|
|
static::assertNotNull($headerFooter, 'HeaderFooter does not exist'); |
236
|
|
|
static::assertEquals('&L&G&CHeader', $headerFooter->getFirstHeader(), 'Unexpected value in firstHeader'); |
237
|
|
|
static::assertEquals('&L&G&CHeader', $headerFooter->getEvenHeader(), 'Unexpected value in evenHeader'); |
238
|
|
|
static::assertEquals('&L&G&CHeader', $headerFooter->getOddHeader(), 'Unexpected value in oddHeader'); |
239
|
|
|
static::assertEquals('&LFooter&R&G', $headerFooter->getFirstFooter(), 'Unexpected value in firstFooter'); |
240
|
|
|
static::assertEquals('&LFooter&R&G', $headerFooter->getEvenFooter(), 'Unexpected value in evenFooter'); |
241
|
|
|
static::assertEquals('&LFooter&R&G', $headerFooter->getOddFooter(), 'Unexpected value in oddFooter'); |
242
|
|
|
|
243
|
|
|
$drawings = $headerFooter->getImages(); |
244
|
|
|
static::assertCount(2, $drawings, 'Sheet has not exactly 2 drawings'); |
245
|
|
|
static::assertArrayHasKey('LH', $drawings, 'Header drawing does not exist'); |
246
|
|
|
static::assertArrayHasKey('RF', $drawings, 'Footer drawing does not exist'); |
247
|
|
|
|
248
|
|
|
$drawing = $drawings['LH']; |
249
|
|
|
static::assertNotNull($drawing, 'Header drawing is null'); |
250
|
|
|
static::assertEquals(40, $drawing->getWidth(), 'Unexpected value in width'); |
251
|
|
|
static::assertEquals(40, $drawing->getHeight(), 'Unexpected value in height'); |
252
|
|
|
|
253
|
|
|
$drawing = $drawings['RF']; |
254
|
|
|
static::assertNotNull($drawing, 'Footer drawing is null'); |
255
|
|
|
static::assertEquals(20, $drawing->getWidth(), 'Unexpected value in width'); |
256
|
|
|
static::assertEquals(20, $drawing->getHeight(), 'Unexpected value in height'); |
257
|
|
|
} catch (Twig_Error_Runtime $e) { |
258
|
|
|
static::fail($e->getMessage()); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param string $format |
264
|
|
|
* |
265
|
|
|
* @dataProvider formatProvider |
266
|
|
|
*/ |
267
|
|
|
public function testHeaderFooterProperties($format) |
268
|
|
|
{ |
269
|
|
|
try { |
270
|
|
|
$document = $this->getDocument('headerFooterProperties', $format); |
271
|
|
|
static::assertNotNull($document, 'Document does not exist'); |
272
|
|
|
|
273
|
|
|
$sheet = $document->getSheetByName('Test'); |
274
|
|
|
static::assertNotNull($sheet, 'Sheet does not exist'); |
275
|
|
|
|
276
|
|
|
$headerFooter = $sheet->getHeaderFooter(); |
277
|
|
|
static::assertNotNull($headerFooter, 'HeaderFooter does not exist'); |
278
|
|
|
|
279
|
|
|
static::assertEquals('&CHeader', $headerFooter->getFirstHeader(), 'Unexpected value in firstHeader'); |
280
|
|
|
static::assertEquals('&CHeader', $headerFooter->getEvenHeader(), 'Unexpected value in evenHeader'); |
281
|
|
|
static::assertEquals('&CHeader', $headerFooter->getOddHeader(), 'Unexpected value in oddHeader'); |
282
|
|
|
static::assertEquals('&CFooter', $headerFooter->getFirstFooter(), 'Unexpected value in firstFooter'); |
283
|
|
|
static::assertEquals('&CFooter', $headerFooter->getEvenFooter(), 'Unexpected value in evenFooter'); |
284
|
|
|
static::assertEquals('&CFooter', $headerFooter->getOddFooter(), 'Unexpected value in oddFooter'); |
285
|
|
|
|
286
|
|
|
static::assertFalse($headerFooter->getAlignWithMargins(), 'Unexpected value in alignWithMargins'); |
287
|
|
|
static::assertFalse($headerFooter->getScaleWithDocument(), 'Unexpected value in scaleWithDocument'); |
288
|
|
|
} catch (Twig_Error_Runtime $e) { |
289
|
|
|
static::fail($e->getMessage()); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|