Completed
Push — master ( ba1ce8...40abd1 )
by Adrien
22s queued 19s
created

IOFactoryTest::testCreateReaderNoExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests;
4
5
use InvalidArgumentException;
6
use PhpOffice\PhpSpreadsheet\IOFactory;
7
use PhpOffice\PhpSpreadsheet\Reader;
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::assertInstanceOf($expected, $actual);
25
    }
26
27
    public function providerCreateWriter()
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::assertInstanceOf($expected, $actual);
59
    }
60
61
    public function providerCreateReader()
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
     * @param string $file
86
     * @param string $expectedName
87
     * @param string $expectedClass
88
     */
89
    public function testIdentify($file, $expectedName, $expectedClass): void
0 ignored issues
show
Unused Code introduced by
The parameter $expectedClass is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

89
    public function testIdentify($file, $expectedName, /** @scrutinizer ignore-unused */ $expectedClass): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        $actual = IOFactory::identify($file);
92
        self::assertSame($expectedName, $actual);
93
    }
94
95
    /**
96
     * @dataProvider providerIdentify
97
     *
98
     * @param string $file
99
     * @param string $expectedName
100
     * @param string $expectedClass
101
     */
102
    public function testCreateReaderForFile($file, $expectedName, $expectedClass): void
0 ignored issues
show
Unused Code introduced by
The parameter $expectedName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

102
    public function testCreateReaderForFile($file, /** @scrutinizer ignore-unused */ $expectedName, $expectedClass): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104
        $actual = IOFactory::createReaderForFile($file);
105
        self::assertInstanceOf($expectedClass, $actual);
106
    }
107
108
    /**
109
     * @dataProvider providerIdentify
110
     *
111
     * @param string $file
112
     * @param string $expectedName
113
     * @param string $expectedClass
114
     */
115
    public function testLoad($file, $expectedName, $expectedClass): void
0 ignored issues
show
Unused Code introduced by
The parameter $expectedName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

115
    public function testLoad($file, /** @scrutinizer ignore-unused */ $expectedName, $expectedClass): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expectedClass is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

115
    public function testLoad($file, $expectedName, /** @scrutinizer ignore-unused */ $expectedClass): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
    {
117
        $actual = IOFactory::load($file);
118
        self::assertInstanceOf(Spreadsheet::class, $actual);
119
    }
120
121
    public function providerIdentify()
122
    {
123
        return [
124
            ['samples/templates/26template.xlsx', 'Xlsx', Reader\Xlsx::class],
125
            ['samples/templates/GnumericTest.gnumeric', 'Gnumeric', Reader\Gnumeric::class],
126
            ['samples/templates/old.gnumeric', 'Gnumeric', Reader\Gnumeric::class],
127
            ['samples/templates/30template.xls', 'Xls', Reader\Xls::class],
128
            ['samples/templates/OOCalcTest.ods', 'Ods', Reader\Ods::class],
129
            ['samples/templates/SylkTest.slk', 'Slk', Reader\Slk::class],
130
            ['samples/templates/excel2003.xml', 'Xml', Reader\Xml::class],
131
            // Following not readable by Excel.
132
            //['samples/templates/Excel2003XMLTest.xml', 'Xml', Reader\Xml::class],
133
            ['samples/templates/46readHtml.html', 'Html', Reader\Html::class],
134
        ];
135
    }
136
137
    public function testIdentifyNonExistingFileThrowException(): void
138
    {
139
        $this->expectException(InvalidArgumentException::class);
140
141
        IOFactory::identify('/non/existing/file');
142
    }
143
144
    public function testIdentifyExistingDirectoryThrowExceptions(): void
145
    {
146
        $this->expectException(InvalidArgumentException::class);
147
148
        IOFactory::identify('.');
149
    }
150
151
    public function testRegisterInvalidWriter(): void
152
    {
153
        $this->expectException(\PhpOffice\PhpSpreadsheet\Writer\Exception::class);
154
155
        IOFactory::registerWriter('foo', 'bar');
156
    }
157
158
    public function testRegisterInvalidReader(): void
159
    {
160
        $this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
161
162
        IOFactory::registerReader('foo', 'bar');
163
    }
164
165
    public function testCreateInvalidWriter(): void
166
    {
167
        $this->expectException(\PhpOffice\PhpSpreadsheet\Writer\Exception::class);
168
        $spreadsheet = new Spreadsheet();
169
        IOFactory::createWriter($spreadsheet, 'bad');
170
    }
171
172
    public function testCreateInvalidReader(): void
173
    {
174
        $this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
175
        IOFactory::createReader('bad');
176
    }
177
178
    public function testCreateReaderUnknownExtension(): void
179
    {
180
        $filename = 'samples/Reader/sampleData/example1.tsv';
181
        $reader = IOFactory::createReaderForFile($filename);
182
        self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Csv', get_class($reader));
183
    }
184
185
    public function testCreateReaderCsvExtension(): void
186
    {
187
        $filename = 'samples/Reader/sampleData/example1.csv';
188
        $reader = IOFactory::createReaderForFile($filename);
189
        self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Csv', get_class($reader));
190
    }
191
192
    public function testCreateReaderNoExtension(): void
193
    {
194
        $filename = 'samples/Reader/sampleData/example1xls';
195
        $reader = IOFactory::createReaderForFile($filename);
196
        self::assertEquals('PhpOffice\\PhpSpreadsheet\\Reader\\Xls', get_class($reader));
197
    }
198
199
    public function testCreateReaderNotSpreadsheet(): void
200
    {
201
        $this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
202
        $filename = __FILE__;
203
        IOFactory::createReaderForFile($filename);
204
    }
205
}
206