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
*/
public function testShouldThrowExceptionIfNamespaceIsNotKnown()
{
try {
$reader = new XMLReader();
$reader->getDomFromString('AAA');
$this->assertTrue($reader->elementExists('/element/test:child'));
$this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
$this->fail();
} catch (\Exception $e) {
$this->assertTrue(true);
}
}
/**
* 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');
}
}