diff --git a/src/Common/XMLReader.php b/src/Common/XMLReader.php
index dc9ba74..aaf7c75 100644
--- a/src/Common/XMLReader.php
+++ b/src/Common/XMLReader.php
@@ -100,6 +100,25 @@ public function getElements($path, \DOMElement $contextNode = null)
}
}
+ /**
+ * Registers the namespace with the DOMXPath object
+ *
+ * @param string $prefix The prefix
+ * @param string $namespaceURI The URI of the namespace
+ * @return bool true on success or false on failure
+ * @throws \InvalidArgumentException If called before having loaded the DOM document
+ */
+ public function registerNamespace($prefix, $namespaceURI)
+ {
+ if ($this->dom === null) {
+ throw new \InvalidArgumentException('Dom needs to be loaded before registering a namespace');
+ }
+ if ($this->xpath === null) {
+ $this->xpath = new \DOMXpath($this->dom);
+ }
+ return $this->xpath->registerNamespace($prefix, $namespaceURI);
+ }
+
/**
* Get element
*
diff --git a/tests/Common/Tests/XMLReaderTest.php b/tests/Common/Tests/XMLReaderTest.php
new file mode 100644
index 0000000..44019fd
--- /dev/null
+++ b/tests/Common/Tests/XMLReaderTest.php
@@ -0,0 +1,132 @@
+getDomFromString('AAA');
+
+ $this->assertTrue($reader->elementExists('/element/child'));
+ $this->assertEquals('AAA', $reader->getElement('/element/child')->textContent);
+ $this->assertEquals('AAA', $reader->getValue('/element/child'));
+ $this->assertEquals('test', $reader->getAttribute('attr', $reader->getElement('/element')));
+ $this->assertEquals('subtest', $reader->getAttribute('attr', $reader->getElement('/element'), 'child'));
+ }
+
+ /**
+ * Test reading XML from zip
+ */
+ public function testDomFromZip()
+ {
+ $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
+
+ $reader = new XMLReader();
+ $reader->getDomFromZip($pathResources. 'reader.zip', 'test.xml');
+
+ $this->assertTrue($reader->elementExists('/element/child'));
+
+ $this->assertFalse($reader->getDomFromZip($pathResources. 'reader.zip', 'non_existing_xml_file.xml'));
+ }
+
+ /**
+ * Test that read from non existing archive throws exception
+ *
+ * @expectedException Exception
+ */
+ public function testThrowsExceptionOnNonExistingArchive()
+ {
+ $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
+
+ $reader = new XMLReader();
+ $reader->getDomFromZip($pathResources. 'readers.zip', 'test.xml');
+ }
+
+ /**
+ * Test elements count
+ */
+ public function testCountElements()
+ {
+ $reader = new XMLReader();
+ $reader->getDomFromString('AAABBB');
+
+ $this->assertEquals(2, $reader->countElements('/element/child'));
+ }
+
+ /**
+ * Test read non existing elements
+ */
+ public function testReturnNullOnNonExistingNode()
+ {
+ $reader = new XMLReader();
+ $this->assertEmpty($reader->getElements('/element/children'));
+ $reader->getDomFromString('AAA');
+
+ $this->assertNull($reader->getElement('/element/children'));
+ $this->assertNull($reader->getValue('/element/children'));
+ }
+
+ /**
+ * Test that xpath fails if custom namespace is not registered
+ *
+ * @expectedException Exception
+ */
+ public function testShouldThrowExceptionIfNamespaceIsNotKnown()
+ {
+ $reader = new XMLReader();
+ $reader->getDomFromString('AAA');
+
+ $this->assertTrue($reader->elementExists('/element/test:child'));
+ $this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
+ }
+
+ /**
+ * Test reading XML with manually registered namespace
+ */
+ public function testShouldParseXmlWithCustomNamespace()
+ {
+ $reader = new XMLReader();
+ $reader->getDomFromString('AAA');
+ $reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
+
+ $this->assertTrue($reader->elementExists('/element/test:child'));
+ $this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
+ }
+
+ /**
+ * Test that xpath fails if custom namespace is not registered
+ *
+ * @expectedException InvalidArgumentException
+ */
+ public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc()
+ {
+ $reader = new XMLReader();
+ $reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
+ }
+}
diff --git a/tests/resources/files/reader.zip b/tests/resources/files/reader.zip
new file mode 100644
index 0000000..dbe69cb
Binary files /dev/null and b/tests/resources/files/reader.zip differ