Passed
Push — master ( 445cc1...f2bba7 )
by Adrien
29:09 queued 15:47
created

IOFactoryTest::testCreateWriter()   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
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
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 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)
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

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

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...
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)
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

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

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...
102
    {
103
        $actual = IOFactory::createReaderForFile($file);
104
        self::assertInstanceOf($expectedClass, $actual);
105
    }
106
107
    /**
108
     * @dataProvider providerIdentify
109
     *
110
     * @param string $file
111
     * @param string $expectedName
112
     * @param string $expectedClass
113
     */
114
    public function testLoad($file, $expectedName, $expectedClass)
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

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

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 $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

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

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...
115
    {
116
        $actual = IOFactory::load($file);
117
        self::assertInstanceOf(Spreadsheet::class, $actual);
118
    }
119
120
    public function providerIdentify()
121
    {
122
        return [
123
            ['../samples/templates/26template.xlsx', 'Xlsx', Reader\Xlsx::class],
124
            ['../samples/templates/GnumericTest.gnumeric', 'Gnumeric', Reader\Gnumeric::class],
125
            ['../samples/templates/30template.xls', 'Xls', Reader\Xls::class],
126
            ['../samples/templates/OOCalcTest.ods', 'Ods', Reader\Ods::class],
127
            ['../samples/templates/SylkTest.slk', 'Slk', Reader\Slk::class],
128
            ['../samples/templates/Excel2003XMLTest.xml', 'Xml', Reader\Xml::class],
129
            ['../samples/templates/46readHtml.html', 'Html', Reader\Html::class],
130
        ];
131
    }
132
133
    public function testIdentifyNonExistingFileThrowException()
134
    {
135
        $this->expectException(\InvalidArgumentException::class);
136
137
        IOFactory::identify('/non/existing/file');
138
    }
139
140
    public function testIdentifyExistingDirectoryThrowExceptions()
141
    {
142
        $this->expectException(\InvalidArgumentException::class);
143
144
        IOFactory::identify('.');
145
    }
146
147
    public function testRegisterInvalidWriter()
148
    {
149
        $this->expectException(\PhpOffice\PhpSpreadsheet\Writer\Exception::class);
150
151
        IOFactory::registerWriter('foo', 'bar');
152
    }
153
154
    public function testRegisterInvalidReader()
155
    {
156
        $this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
157
158
        IOFactory::registerReader('foo', 'bar');
159
    }
160
}
161