1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Reader; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Writer; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
class IOFactoryTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @dataProvider providerCreateWriter |
16
|
|
|
* |
17
|
|
|
* @param string $name |
18
|
|
|
* @param string $expected |
19
|
|
|
*/ |
20
|
|
|
public function testCreateWriter($name, $expected): void |
21
|
|
|
{ |
22
|
|
|
$spreadsheet = new Spreadsheet(); |
23
|
|
|
$actual = IOFactory::createWriter($spreadsheet, $name); |
24
|
|
|
self::assertSame($expected, get_class($actual)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function providerCreateWriter(): array |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
['Xls', Writer\Xls::class], |
31
|
|
|
['Xlsx', Writer\Xlsx::class], |
32
|
|
|
['Ods', Writer\Ods::class], |
33
|
|
|
['Csv', Writer\Csv::class], |
34
|
|
|
['Html', Writer\Html::class], |
35
|
|
|
['Mpdf', Writer\Pdf\Mpdf::class], |
36
|
|
|
['Tcpdf', Writer\Pdf\Tcpdf::class], |
37
|
|
|
['Dompdf', Writer\Pdf\Dompdf::class], |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testRegisterWriter(): void |
42
|
|
|
{ |
43
|
|
|
IOFactory::registerWriter('Pdf', Writer\Pdf\Mpdf::class); |
44
|
|
|
$spreadsheet = new Spreadsheet(); |
45
|
|
|
$actual = IOFactory::createWriter($spreadsheet, 'Pdf'); |
46
|
|
|
self::assertInstanceOf(Writer\Pdf\Mpdf::class, $actual); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @dataProvider providerCreateReader |
51
|
|
|
* |
52
|
|
|
* @param string $name |
53
|
|
|
* @param string $expected |
54
|
|
|
*/ |
55
|
|
|
public function testCreateReader($name, $expected): void |
56
|
|
|
{ |
57
|
|
|
$actual = IOFactory::createReader($name); |
58
|
|
|
self::assertSame($expected, get_class($actual)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function providerCreateReader(): array |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
['Xls', Reader\Xls::class], |
65
|
|
|
['Xlsx', Reader\Xlsx::class], |
66
|
|
|
['Xml', Reader\Xml::class], |
67
|
|
|
['Ods', Reader\Ods::class], |
68
|
|
|
['Gnumeric', Reader\Gnumeric::class], |
69
|
|
|
['Csv', Reader\Csv::class], |
70
|
|
|
['Slk', Reader\Slk::class], |
71
|
|
|
['Html', Reader\Html::class], |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testRegisterReader(): void |
76
|
|
|
{ |
77
|
|
|
IOFactory::registerReader('Custom', Reader\Html::class); |
78
|
|
|
$actual = IOFactory::createReader('Custom'); |
79
|
|
|
self::assertInstanceOf(Reader\Html::class, $actual); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @dataProvider providerIdentify |
84
|
|
|
*/ |
85
|
|
|
public function testIdentifyCreateLoad(string $file, string $expectedName, string $expectedClass): void |
86
|
|
|
{ |
87
|
|
|
$actual = IOFactory::identify($file); |
88
|
|
|
self::assertSame($expectedName, $actual); |
89
|
|
|
$actual = IOFactory::createReaderForFile($file); |
90
|
|
|
self::assertSame($expectedClass, get_class($actual)); |
91
|
|
|
$actual = IOFactory::load($file); |
92
|
|
|
self::assertInstanceOf(Spreadsheet::class, $actual); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function providerIdentify(): array |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
['samples/templates/26template.xlsx', 'Xlsx', Reader\Xlsx::class], |
99
|
|
|
['samples/templates/GnumericTest.gnumeric', 'Gnumeric', Reader\Gnumeric::class], |
100
|
|
|
['samples/templates/old.gnumeric', 'Gnumeric', Reader\Gnumeric::class], |
101
|
|
|
['samples/templates/30template.xls', 'Xls', Reader\Xls::class], |
102
|
|
|
['samples/templates/OOCalcTest.ods', 'Ods', Reader\Ods::class], |
103
|
|
|
['samples/templates/SylkTest.slk', 'Slk', Reader\Slk::class], |
104
|
|
|
['samples/templates/excel2003.xml', 'Xml', Reader\Xml::class], |
105
|
|
|
// Following not readable by Excel. |
106
|
|
|
//['samples/templates/Excel2003XMLTest.xml', 'Xml', Reader\Xml::class], |
107
|
|
|
['samples/templates/46readHtml.html', 'Html', Reader\Html::class], |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testIdentifyInvalid(): void |
112
|
|
|
{ |
113
|
|
|
$file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc'; |
114
|
|
|
|
115
|
|
|
$this->expectException(ReaderException::class); |
116
|
|
|
$this->expectExceptionMessage('Unable to identify a reader for this file'); |
117
|
|
|
IOFactory::identify($file); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testCreateInvalid(): void |
121
|
|
|
{ |
122
|
|
|
$file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc'; |
123
|
|
|
|
124
|
|
|
$this->expectException(ReaderException::class); |
125
|
|
|
$this->expectExceptionMessage('Unable to identify a reader for this file'); |
126
|
|
|
IOFactory::createReaderForFile($file); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testLoadInvalid(): void |
130
|
|
|
{ |
131
|
|
|
$file = __DIR__ . '/../data/Reader/NotASpreadsheetFile.doc'; |
132
|
|
|
|
133
|
|
|
$this->expectException(ReaderException::class); |
134
|
|
|
$this->expectExceptionMessage('Unable to identify a reader for this file'); |
135
|
|
|
IOFactory::load($file); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testFormatAsExpected(): void |
139
|
|
|
{ |
140
|
|
|
$fileName = 'samples/templates/30template.xls'; |
141
|
|
|
|
142
|
|
|
$actual = IOFactory::identify($fileName, [IOFactory::READER_XLS]); |
143
|
|
|
self::assertSame('Xls', $actual); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testFormatNotAsExpectedThrowsException(): void |
147
|
|
|
{ |
148
|
|
|
$fileName = 'samples/templates/30template.xls'; |
149
|
|
|
|
150
|
|
|
$this->expectException(ReaderException::class); |
151
|
|
|
IOFactory::identify($fileName, [IOFactory::READER_ODS]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testIdentifyNonExistingFileThrowException(): void |
155
|
|
|
{ |
156
|
|
|
$this->expectException(ReaderException::class); |
157
|
|
|
|
158
|
|
|
IOFactory::identify('/non/existing/file'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testIdentifyExistingDirectoryThrowExceptions(): void |
162
|
|
|
{ |
163
|
|
|
$this->expectException(ReaderException::class); |
164
|
|
|
|
165
|
|
|
IOFactory::identify('.'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testRegisterInvalidWriter(): void |
169
|
|
|
{ |
170
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Writer\Exception::class); |
171
|
|
|
|
172
|
|
|
IOFactory::registerWriter('foo', 'bar'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function testRegisterInvalidReader(): void |
176
|
|
|
{ |
177
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class); |
178
|
|
|
|
179
|
|
|
IOFactory::registerReader('foo', 'bar'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testCreateInvalidWriter(): void |
183
|
|
|
{ |
184
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Writer\Exception::class); |
185
|
|
|
$spreadsheet = new Spreadsheet(); |
186
|
|
|
IOFactory::createWriter($spreadsheet, 'bad'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testCreateInvalidReader(): void |
190
|
|
|
{ |
191
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class); |
192
|
|
|
IOFactory::createReader('bad'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testCreateReaderUnknownExtension(): void |
196
|
|
|
{ |
197
|
|
|
$filename = 'samples/Reader/sampleData/example1.tsv'; |
198
|
|
|
$reader = IOFactory::createReaderForFile($filename); |
199
|
|
|
self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Csv', get_class($reader)); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testCreateReaderCsvExtension(): void |
203
|
|
|
{ |
204
|
|
|
$filename = 'samples/Reader/sampleData/example1.csv'; |
205
|
|
|
$reader = IOFactory::createReaderForFile($filename); |
206
|
|
|
self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Csv', get_class($reader)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testCreateReaderNoExtension(): void |
210
|
|
|
{ |
211
|
|
|
$filename = 'samples/Reader/sampleData/example1xls'; |
212
|
|
|
$reader = IOFactory::createReaderForFile($filename); |
213
|
|
|
self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Xls', get_class($reader)); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function testCreateReaderNotSpreadsheet(): void |
217
|
|
|
{ |
218
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class); |
219
|
|
|
$filename = __FILE__; |
220
|
|
|
IOFactory::createReaderForFile($filename); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|