Skip to content

Commit f60875f

Browse files
committed
Add tests for the XMLReader class
1 parent 291e2e0 commit f60875f

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* This file is part of PHPOffice Common
4+
*
5+
* PHPOffice Common is free software distributed under the terms of the GNU Lesser
6+
* General Public License version 3 as published by the Free Software Foundation.
7+
*
8+
* For the full copyright and license information, please read the LICENSE
9+
* file that was distributed with this source code. For the full list of
10+
* contributors, visit https://github.com/PHPOffice/Common/contributors.
11+
*
12+
* @link https://github.com/PHPOffice/Common
13+
* @copyright 2009-2017 PHPOffice Common contributors
14+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
15+
*/
16+
17+
namespace PhpOffice\Common\Tests;
18+
19+
use PhpOffice\Common\XMLReader;
20+
21+
/**
22+
* Test class for XMLReader
23+
*
24+
* @coversDefaultClass PhpOffice\Common\XMLReader
25+
*/
26+
class XMLReaderTest extends \PHPUnit_Framework_TestCase
27+
{
28+
/**
29+
* Test reading XML from string
30+
*/
31+
public function testDomFromString()
32+
{
33+
$reader = new XMLReader();
34+
$reader->getDomFromString('<element attr="test"><child attr="subtest">AAA</child></element>');
35+
36+
$this->assertTrue($reader->elementExists('/element/child'));
37+
$this->assertEquals('AAA', $reader->getElement('/element/child')->textContent);
38+
$this->assertEquals('AAA', $reader->getValue('/element/child'));
39+
$this->assertEquals('test', $reader->getAttribute('attr', $reader->getElement('/element')));
40+
$this->assertEquals('subtest', $reader->getAttribute('attr', $reader->getElement('/element'), 'child'));
41+
}
42+
43+
/**
44+
* Test reading XML from zip
45+
*/
46+
public function testDomFromZip()
47+
{
48+
$pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
49+
50+
$reader = new XMLReader();
51+
$reader->getDomFromZip($pathResources. 'reader.zip', 'test.xml');
52+
53+
$this->assertTrue($reader->elementExists('/element/child'));
54+
55+
$this->assertFalse($reader->getDomFromZip($pathResources. 'reader.zip', 'non_existing_xml_file.xml'));
56+
}
57+
58+
/**
59+
* Test that read from non existing archive throws exception
60+
*
61+
* @expectedException Exception
62+
*/
63+
public function testThrowsExceptionOnNonExistingArchive()
64+
{
65+
$pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
66+
67+
$reader = new XMLReader();
68+
$reader->getDomFromZip($pathResources. 'readers.zip', 'test.xml');
69+
}
70+
71+
public function testCountElements()
72+
{
73+
$reader = new XMLReader();
74+
$reader->getDomFromString('<element attr="test"><child>AAA</child><child>BBB</child></element>');
75+
76+
$this->assertEquals(2, $reader->countElements('/element/child'));
77+
}
78+
79+
public function testReturnNullOnNonExistingNode()
80+
{
81+
$reader = new XMLReader();
82+
$this->assertEmpty($reader->getElements('/element/children'));
83+
$reader->getDomFromString('<element><child>AAA</child></element>');
84+
85+
$this->assertNull($reader->getElement('/element/children'));
86+
$this->assertNull($reader->getValue('/element/children'));
87+
}
88+
89+
/**
90+
* Test that xpath fails if custom namespace is not registered
91+
*
92+
* @expectedException Exception
93+
*/
94+
public function testShouldThrowExceptionIfNamespaceIsNotKnown()
95+
{
96+
$reader = new XMLReader();
97+
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
98+
99+
$this->assertTrue($reader->elementExists('/element/test:child'));
100+
$this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
101+
}
102+
103+
/**
104+
* Test reading XML with manually registered namespace
105+
*/
106+
public function testShouldParseXmlWithCustomNamespace()
107+
{
108+
$reader = new XMLReader();
109+
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
110+
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
111+
112+
$this->assertTrue($reader->elementExists('/element/test:child'));
113+
$this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
114+
}
115+
116+
/**
117+
* Test that xpath fails if custom namespace is not registered
118+
*
119+
* @expectedException InvalidArgumentException
120+
*/
121+
public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc()
122+
{
123+
$reader = new XMLReader();
124+
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
125+
}
126+
}

tests/resources/files/reader.zip

272 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)