Passed
Push — master ( 7288b4...f131ca )
by Adrien
13:07
created

XmlScannerTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 56
c 0
b 0
f 0
dl 0
loc 129
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A providerValidXMLForCallback() 0 10 2
A providerValidXML() 0 13 2
A testGetSecurityScannerForNonXmlBasedReader2() 0 6 1
A testValidXML() 0 5 1
A testLibxmlDisableEntityLoaderIsRestoredWithoutShutdown() 0 8 1
A providerInvalidXML() 0 12 2
A testGetSecurityScannerForNonXmlBasedReader() 0 6 1
A testEncodingAllowsMixedCase() 0 5 1
A testGetSecurityScannerForXmlBasedReader() 0 9 1
A testSecurityScanWithCallback() 0 8 1
A testInvalidXML() 0 8 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheetTests\Reader\Security;
4
5
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
6
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
7
use PhpOffice\PhpSpreadsheet\Reader\Xls;
8
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
9
use PHPUnit\Framework\TestCase;
10
use XMLReader;
11
12
class XmlScannerTest extends TestCase
13
{
14
    /**
15
     * @dataProvider providerValidXML
16
     *
17
     * @param mixed $filename
18
     * @param mixed $expectedResult
19
     */
20
    public function testValidXML($filename, $expectedResult, bool $libxmlDisableEntityLoader): void
0 ignored issues
show
Unused Code introduced by
The parameter $libxmlDisableEntityLoader 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

20
    public function testValidXML($filename, $expectedResult, /** @scrutinizer ignore-unused */ bool $libxmlDisableEntityLoader): 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...
21
    {
22
        $reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml());
23
        $result = $reader->scanFile($filename);
24
        self::assertEquals($expectedResult, $result);
25
    }
26
27
    public static function providerValidXML(): array
28
    {
29
        $tests = [];
30
        $glob = glob('tests/data/Reader/Xml/XEETestValid*.xml');
31
        self::assertNotFalse($glob);
32
        foreach ($glob as $file) {
33
            $filename = realpath($file);
34
            $expectedResult = file_get_contents($file);
35
            $tests[basename($file) . '_libxml_entity_loader_disabled'] = [$filename, $expectedResult, true];
36
            $tests[basename($file) . '_libxml_entity_loader_enabled'] = [$filename, $expectedResult, false];
37
        }
38
39
        return $tests;
40
    }
41
42
    /**
43
     * @dataProvider providerInvalidXML
44
     *
45
     * @param mixed $filename
46
     */
47
    public function testInvalidXML($filename, bool $libxmlDisableEntityLoader): void
0 ignored issues
show
Unused Code introduced by
The parameter $libxmlDisableEntityLoader 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

47
    public function testInvalidXML($filename, /** @scrutinizer ignore-unused */ bool $libxmlDisableEntityLoader): 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...
48
    {
49
        $this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
50
51
        $reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml());
52
        $expectedResult = 'FAILURE: Should throw an Exception rather than return a value';
53
        $result = $reader->scanFile($filename);
54
        self::assertEquals($expectedResult, $result);
55
    }
56
57
    public static function providerInvalidXML(): array
58
    {
59
        $tests = [];
60
        $glob = glob('tests/data/Reader/Xml/XEETestInvalidUTF*.xml');
61
        self::assertNotFalse($glob);
62
        foreach ($glob as $file) {
63
            $filename = realpath($file);
64
            $tests[basename($file) . '_libxml_entity_loader_disabled'] = [$filename, true];
65
            $tests[basename($file) . '_libxml_entity_loader_enabled'] = [$filename, false];
66
        }
67
68
        return $tests;
69
    }
70
71
    public function testGetSecurityScannerForXmlBasedReader(): void
72
    {
73
        $fileReader = new Xlsx();
74
        $scanner = $fileReader->getSecurityScanner();
75
76
        //    Must return an object...
77
        self::assertIsObject($scanner);
78
        //    ... of the correct type
79
        self::assertInstanceOf(XmlScanner::class, $scanner);
80
    }
81
82
    public function testGetSecurityScannerForNonXmlBasedReader(): void
83
    {
84
        $fileReader = new Xls();
85
        $scanner = $fileReader->getSecurityScanner();
86
        //    Must return a null...
87
        self::assertNull($scanner);
88
    }
89
90
    public function testGetSecurityScannerForNonXmlBasedReader2(): void
91
    {
92
        $this->expectException(ReaderException::class);
93
        $this->expectExceptionMessage('Security scanner is unexpectedly null');
94
        $fileReader = new Xls();
95
        $fileReader->getSecurityScannerOrThrow();
96
    }
97
98
    /**
99
     * @dataProvider providerValidXMLForCallback
100
     *
101
     * @param mixed $filename
102
     * @param mixed $expectedResult
103
     */
104
    public function testSecurityScanWithCallback($filename, $expectedResult): void
105
    {
106
        $fileReader = new Xlsx();
107
        $scanner = $fileReader->getSecurityScannerOrThrow();
108
        $scanner->setAdditionalCallback('strrev');
109
        $xml = $scanner->scanFile($filename);
110
111
        self::assertEquals(strrev($expectedResult), $xml);
112
    }
113
114
    public static function providerValidXMLForCallback(): array
115
    {
116
        $tests = [];
117
        $glob = glob('tests/data/Reader/Xml/SecurityScannerWithCallback*.xml');
118
        self::assertNotFalse($glob);
119
        foreach ($glob as $file) {
120
            $tests[basename($file)] = [realpath($file), file_get_contents($file)];
121
        }
122
123
        return $tests;
124
    }
125
126
    public function testLibxmlDisableEntityLoaderIsRestoredWithoutShutdown(): void
127
    {
128
        $reader = new Xlsx();
129
        unset($reader);
130
131
        $reader = new XMLReader();
132
        $opened = $reader->open('tests/data/Reader/Xml/SecurityScannerWithCallbackExample.xml');
133
        self::assertTrue($opened);
134
    }
135
136
    public function testEncodingAllowsMixedCase(): void
137
    {
138
        $scanner = new XmlScanner();
139
        $output = $scanner->scan($input = '<?xml version="1.0" encoding="utf-8"?><foo>bar</foo>');
140
        self::assertSame($input, $output);
141
    }
142
}
143