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