Completed
Push — develop ( 069c66...25ff91 )
by Adrien
23:18
created

IOFactoryTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 141
rs 10
c 1
b 0
f 0
wmc 13
lcom 1
cbo 3

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateWriter() 0 6 1
A providerCreateWriter() 0 13 1
A testRegisterWriter() 0 7 1
A testCreateReader() 0 5 1
A providerCreateReader() 0 13 1
A testRegisterReader() 0 6 1
A testIdentify() 0 5 1
A testCreateReaderForFile() 0 5 1
A providerIdentify() 0 12 1
A testIdentifyNonExistingFileThrowException() 0 4 1
A testIdentifyExistingDirectoryThrowExceptions() 0 4 1
A testRegisterInvalidWriter() 0 4 1
A testRegisterInvalidReader() 0 4 1
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)
0 ignored issues
show
Unused Code introduced by
The parameter $expectedClass is not used and could be removed.

This check looks from 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.

This check looks from 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
    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