|
| 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 | +} |
0 commit comments