1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Reader; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Writer; |
9
|
|
|
use PHPUnit_Framework_TestCase; |
10
|
|
|
|
11
|
|
|
class IOFactoryTest extends PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @dataProvider providerCreateWriter |
15
|
|
|
* |
16
|
|
|
* @param string $name |
17
|
|
|
* @param string $expected |
18
|
|
|
*/ |
19
|
|
|
public function testCreateWriter($name, $expected) |
20
|
|
|
{ |
21
|
|
|
$spreadsheet = new Spreadsheet(); |
22
|
|
|
$actual = IOFactory::createWriter($spreadsheet, $name); |
23
|
|
|
self::assertInstanceOf($expected, $actual); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function providerCreateWriter() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
['Xls', Writer\Xls::class], |
30
|
|
|
['Xlsx', Writer\Xlsx::class], |
31
|
|
|
['Ods', Writer\Ods::class], |
32
|
|
|
['Csv', Writer\Csv::class], |
33
|
|
|
['Html', Writer\Html::class], |
34
|
|
|
['Mpdf', Writer\Pdf\Mpdf::class], |
35
|
|
|
['Tcpdf', Writer\Pdf\Tcpdf::class], |
36
|
|
|
['Dompdf', Writer\Pdf\Dompdf::class], |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testRegisterWriter() |
41
|
|
|
{ |
42
|
|
|
IOFactory::registerWriter('Pdf', Writer\Pdf\Mpdf::class); |
43
|
|
|
$spreadsheet = new Spreadsheet(); |
44
|
|
|
$actual = IOFactory::createWriter($spreadsheet, 'Pdf'); |
45
|
|
|
self::assertInstanceOf(Writer\Pdf\Mpdf::class, $actual); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider providerCreateReader |
50
|
|
|
* |
51
|
|
|
* @param string $name |
52
|
|
|
* @param string $expected |
53
|
|
|
*/ |
54
|
|
|
public function testCreateReader($name, $expected) |
55
|
|
|
{ |
56
|
|
|
$actual = IOFactory::createReader($name); |
57
|
|
|
self::assertInstanceOf($expected, $actual); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function providerCreateReader() |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
['Xls', Reader\Xls::class], |
64
|
|
|
['Xlsx', Reader\Xlsx::class], |
65
|
|
|
['Xml', Reader\Xml::class], |
66
|
|
|
['Ods', Reader\Ods::class], |
67
|
|
|
['Gnumeric', Reader\Gnumeric::class], |
68
|
|
|
['Csv', Reader\Csv::class], |
69
|
|
|
['Slk', Reader\Slk::class], |
70
|
|
|
['Html', Reader\Html::class], |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testRegisterReader() |
75
|
|
|
{ |
76
|
|
|
IOFactory::registerReader('Custom', Reader\Html::class); |
77
|
|
|
$actual = IOFactory::createReader('Custom'); |
78
|
|
|
self::assertInstanceOf(Reader\Html::class, $actual); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @dataProvider providerIdentify |
83
|
|
|
* |
84
|
|
|
* @param string $file |
85
|
|
|
* @param string $expectedName |
86
|
|
|
* @param string $expectedClass |
87
|
|
|
*/ |
88
|
|
|
public function testIdentify($file, $expectedName, $expectedClass) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$actual = IOFactory::identify($file); |
91
|
|
|
self::assertSame($expectedName, $actual); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @dataProvider providerIdentify |
96
|
|
|
* |
97
|
|
|
* @param string $file |
98
|
|
|
* @param string $expectedName |
99
|
|
|
* @param string $expectedClass |
100
|
|
|
*/ |
101
|
|
|
public function testCreateReaderForFile($file, $expectedName, $expectedClass) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$actual = IOFactory::createReaderForFile($file); |
104
|
|
|
self::assertInstanceOf($expectedClass, $actual); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function providerIdentify() |
108
|
|
|
{ |
109
|
|
|
return [ |
110
|
|
|
['../samples/templates/26template.xlsx', 'Xlsx', Reader\Xlsx::class], |
111
|
|
|
['../samples/templates/GnumericTest.gnumeric', 'Gnumeric', Reader\Gnumeric::class], |
112
|
|
|
['../samples/templates/30template.xls', 'Xls', Reader\Xls::class], |
113
|
|
|
['../samples/templates/OOCalcTest.ods', 'Ods', Reader\Ods::class], |
114
|
|
|
['../samples/templates/SylkTest.slk', 'Slk', Reader\Slk::class], |
115
|
|
|
['../samples/templates/Excel2003XMLTest.xml', 'Xml', Reader\Xml::class], |
116
|
|
|
['../samples/templates/46readHtml.html', 'Html', Reader\Html::class], |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @expectedException \InvalidArgumentException |
122
|
|
|
*/ |
123
|
|
|
public function testIdentifyNonExistingFileThrowException() |
124
|
|
|
{ |
125
|
|
|
IOFactory::identify('/non/existing/file'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @expectedException \InvalidArgumentException |
130
|
|
|
*/ |
131
|
|
|
public function testIdentifyExistingDirectoryThrowExceptions() |
132
|
|
|
{ |
133
|
|
|
IOFactory::identify('.'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @expectedException \PhpOffice\PhpSpreadsheet\Writer\Exception |
138
|
|
|
*/ |
139
|
|
|
public function testRegisterInvalidWriter() |
140
|
|
|
{ |
141
|
|
|
IOFactory::registerWriter('foo', 'bar'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @expectedException \PhpOffice\PhpSpreadsheet\Reader\Exception |
146
|
|
|
*/ |
147
|
|
|
public function testRegisterInvalidReader() |
148
|
|
|
{ |
149
|
|
|
IOFactory::registerReader('foo', 'bar'); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.