PHPWord/tests/PhpWordTests/Shared/XMLReaderTest.php

137 lines
4.7 KiB
PHP
Raw Normal View History

<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
2022-09-16 11:45:45 +02:00
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
2022-09-16 14:09:17 +02:00
namespace PhpOffice\PhpWordTests\Shared;
2022-09-16 11:45:45 +02:00
use Exception;
use InvalidArgumentException;
2022-09-16 14:09:17 +02:00
use PhpOffice\PhpWord\Shared\XMLReader;
2022-09-16 11:45:45 +02:00
/**
2022-09-16 11:45:45 +02:00
* Test class for XMLReader.
*
2021-01-10 14:06:19 +01:00
* @coversDefaultClass \PhpOffice\PhpWord\Shared\XMLReader
*/
class XMLReaderTest extends \PHPUnit\Framework\TestCase
{
/**
2022-09-16 11:45:45 +02:00
* Test reading XML from string.
*/
2022-09-16 11:45:45 +02:00
public function testDomFromString(): void
{
$reader = new XMLReader();
$reader->getDomFromString('<element attr="test"><child attr="subtest">AAA</child></element>');
2022-09-16 11:45:45 +02:00
self::assertTrue($reader->elementExists('/element/child'));
self::assertEquals('AAA', $reader->getElement('/element/child')->textContent);
self::assertEquals('AAA', $reader->getValue('/element/child'));
self::assertEquals('test', $reader->getAttribute('attr', $reader->getElement('/element')));
self::assertEquals('subtest', $reader->getAttribute('attr', $reader->getElement('/element'), 'child'));
}
/**
2022-09-16 11:45:45 +02:00
* Test reading XML from zip.
*/
2022-09-16 11:45:45 +02:00
public function testDomFromZip(): void
{
$archiveFile = __DIR__ . '/../_files/xml/reader.zip';
$reader = new XMLReader();
$reader->getDomFromZip($archiveFile, 'test.xml');
2022-09-16 11:45:45 +02:00
self::assertTrue($reader->elementExists('/element/child'));
2022-09-16 11:45:45 +02:00
self::assertFalse($reader->getDomFromZip($archiveFile, 'non_existing_xml_file.xml'));
}
/**
2022-09-16 11:45:45 +02:00
* Test that read from non existing archive throws exception.
*/
2022-09-16 11:45:45 +02:00
public function testThrowsExceptionOnNonExistingArchive(): void
{
2022-09-16 11:45:45 +02:00
$this->expectException(Exception::class);
$archiveFile = __DIR__ . '/../_files/xml/readers.zip';
$reader = new XMLReader();
$reader->getDomFromZip($archiveFile, 'test.xml');
}
/**
2022-09-16 11:45:45 +02:00
* Test elements count.
*/
2022-09-16 11:45:45 +02:00
public function testCountElements(): void
{
$reader = new XMLReader();
$reader->getDomFromString('<element attr="test"><child>AAA</child><child>BBB</child></element>');
2022-09-16 11:45:45 +02:00
self::assertEquals(2, $reader->countElements('/element/child'));
}
/**
2022-09-16 11:45:45 +02:00
* Test read non existing elements.
*/
2022-09-16 11:45:45 +02:00
public function testReturnNullOnNonExistingNode(): void
{
$reader = new XMLReader();
2022-09-16 11:45:45 +02:00
self::assertEmpty($reader->getElements('/element/children'));
$reader->getDomFromString('<element><child>AAA</child></element>');
2022-09-16 11:45:45 +02:00
self::assertNull($reader->getElement('/element/children'));
self::assertNull($reader->getValue('/element/children'));
}
/**
2022-09-16 11:45:45 +02:00
* Test that xpath fails if custom namespace is not registered.
*/
2022-09-16 11:45:45 +02:00
public function testShouldThrowExceptionIfNamespaceIsNotKnown(): void
{
try {
$reader = new XMLReader();
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
2022-09-16 11:45:45 +02:00
self::assertTrue($reader->elementExists('/element/test:child'));
self::assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
self::fail();
} catch (Exception $e) {
self::assertTrue(true);
}
}
/**
2022-09-16 11:45:45 +02:00
* Test reading XML with manually registered namespace.
*/
2022-09-16 11:45:45 +02:00
public function testShouldParseXmlWithCustomNamespace(): void
{
$reader = new XMLReader();
$reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
2022-09-16 11:45:45 +02:00
self::assertTrue($reader->elementExists('/element/test:child'));
self::assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
}
/**
2022-09-16 11:45:45 +02:00
* Test that xpath fails if custom namespace is not registered.
*/
2022-09-16 11:45:45 +02:00
public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc(): void
{
2022-09-16 11:45:45 +02:00
$this->expectException(InvalidArgumentException::class);
$reader = new XMLReader();
$reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
}
}