1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\BaseReader; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xml; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class XmlTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @dataProvider providerInvalidXML |
14
|
|
|
* |
15
|
|
|
* @param mixed $filename |
16
|
|
|
*/ |
17
|
|
|
public function testInvalidXML($filename) |
18
|
|
|
{ |
19
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class); |
20
|
|
|
|
21
|
|
|
$reader = $this->getMockForAbstractClass(BaseReader::class); |
22
|
|
|
$expectedResult = 'FAILURE: Should throw an Exception rather than return a value'; |
23
|
|
|
$result = $reader->securityScanFile($filename); |
24
|
|
|
self::assertEquals($expectedResult, $result); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function providerInvalidXML() |
28
|
|
|
{ |
29
|
|
|
$tests = []; |
30
|
|
|
foreach (glob(__DIR__ . '/../../data/Reader/Xml/XEETestInvalidUTF*.xml') as $file) { |
31
|
|
|
$tests[basename($file)] = [realpath($file)]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $tests; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @dataProvider providerInvalidSimpleXML |
39
|
|
|
* |
40
|
|
|
* @param $filename |
41
|
|
|
*/ |
42
|
|
|
public function testInvalidSimpleXML($filename) |
43
|
|
|
{ |
44
|
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class); |
45
|
|
|
|
46
|
|
|
$xmlReader = new Xml(); |
47
|
|
|
$xmlReader->trySimpleXMLLoadString($filename); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function providerInvalidSimpleXML() |
51
|
|
|
{ |
52
|
|
|
$tests = []; |
53
|
|
|
foreach (glob(__DIR__ . '/../../data/Reader/Xml/XEETestInvalidSimpleXML*.xml') as $file) { |
54
|
|
|
$tests[basename($file)] = [realpath($file)]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $tests; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @dataProvider providerValidXML |
62
|
|
|
* |
63
|
|
|
* @param mixed $filename |
64
|
|
|
* @param mixed $expectedResult |
65
|
|
|
*/ |
66
|
|
|
public function testValidXML($filename, $expectedResult) |
67
|
|
|
{ |
68
|
|
|
$reader = $this->getMockForAbstractClass(BaseReader::class); |
69
|
|
|
$result = $reader->securityScanFile($filename); |
70
|
|
|
self::assertEquals($expectedResult, $result); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function providerValidXML() |
74
|
|
|
{ |
75
|
|
|
$tests = []; |
76
|
|
|
foreach (glob(__DIR__ . '/../../data/Reader/Xml/XEETestValid*.xml') as $file) { |
77
|
|
|
$tests[basename($file)] = [realpath($file), file_get_contents($file)]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $tests; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check if it can read XML Hyperlink correctly. |
85
|
|
|
*/ |
86
|
|
|
public function testReadHyperlinks() |
87
|
|
|
{ |
88
|
|
|
$reader = new Xml(); |
89
|
|
|
$spreadsheet = $reader->load('../samples/templates/Excel2003XMLTest.xml'); |
90
|
|
|
$firstSheet = $spreadsheet->getSheet(0); |
91
|
|
|
|
92
|
|
|
$hyperlink = $firstSheet->getCell('L1'); |
93
|
|
|
|
94
|
|
|
self::assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType()); |
95
|
|
|
self::assertEquals('PhpSpreadsheet', $hyperlink->getValue()); |
96
|
|
|
self::assertEquals('http://phpspreadsheet.readthedocs.io/', $hyperlink->getHyperlink()->getUrl()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testReadWithoutStyle() |
100
|
|
|
{ |
101
|
|
|
$reader = new Xml(); |
102
|
|
|
$spreadsheet = $reader->load(__DIR__ . '/../../data/Reader/Xml/WithoutStyle.xml'); |
103
|
|
|
self::assertSame('Test String 1', $spreadsheet->getActiveSheet()->getCell('A1')->getValue()); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|