Skip to content

Commit 5d35fcf

Browse files
authored
Merge pull request #17 from troosan/add_register_namespace_to_xml_reader
add possibility to register namespaces to DOMXpath
2 parents bb41d13 + 75b7a6f commit 5d35fcf

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

src/Common/XMLReader.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ public function getElements($path, \DOMElement $contextNode = null)
100100
return $this->xpath->query($path, $contextNode);
101101
}
102102

103+
/**
104+
* Registers the namespace with the DOMXPath object
105+
*
106+
* @param string $prefix The prefix
107+
* @param string $namespaceURI The URI of the namespace
108+
* @return bool true on success or false on failure
109+
* @throws \InvalidArgumentException If called before having loaded the DOM document
110+
*/
111+
public function registerNamespace($prefix, $namespaceURI)
112+
{
113+
if ($this->dom === null) {
114+
throw new \InvalidArgumentException('Dom needs to be loaded before registering a namespace');
115+
}
116+
if ($this->xpath === null) {
117+
$this->xpath = new \DOMXpath($this->dom);
118+
}
119+
return $this->xpath->registerNamespace($prefix, $namespaceURI);
120+
}
121+
103122
/**
104123
* Get element
105124
*
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
/**
72+
* Test elements count
73+
*/
74+
public function testCountElements()
75+
{
76+
$reader = new XMLReader();
77+
$reader->getDomFromString('<element attr="test"><child>AAA</child><child>BBB</child></element>');
78+
79+
$this->assertEquals(2, $reader->countElements('/element/child'));
80+
}
81+
82+
/**
83+
* Test read non existing elements
84+
*/
85+
public function testReturnNullOnNonExistingNode()
86+
{
87+
$reader = new XMLReader();
88+
$this->assertEmpty($reader->getElements('/element/children'));
89+
$reader->getDomFromString('<element><child>AAA</child></element>');
90+
91+
$this->assertNull($reader->getElement('/element/children'));
92+
$this->assertNull($reader->getValue('/element/children'));
93+
}
94+
95+
/**
96+
* Test that xpath fails if custom namespace is not registered
97+
*
98+
* @expectedException Exception
99+
*/
100+
public function testShouldThrowExceptionIfNamespaceIsNotKnown()
101+
{
102+
$reader = new XMLReader();
103+
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
104+
105+
$this->assertTrue($reader->elementExists('/element/test:child'));
106+
$this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
107+
}
108+
109+
/**
110+
* Test reading XML with manually registered namespace
111+
*/
112+
public function testShouldParseXmlWithCustomNamespace()
113+
{
114+
$reader = new XMLReader();
115+
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
116+
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
117+
118+
$this->assertTrue($reader->elementExists('/element/test:child'));
119+
$this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
120+
}
121+
122+
/**
123+
* Test that xpath fails if custom namespace is not registered
124+
*
125+
* @expectedException InvalidArgumentException
126+
*/
127+
public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc()
128+
{
129+
$reader = new XMLReader();
130+
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
131+
}
132+
}

tests/resources/files/reader.zip

272 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)