XMLReaderTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. namespace PhpOffice\Common\Tests;
  17. use PhpOffice\Common\XMLReader;
  18. /**
  19. * Test class for XMLReader
  20. *
  21. * @coversDefaultClass PhpOffice\Common\XMLReader
  22. */
  23. class XMLReaderTest extends \PHPUnit\Framework\TestCase
  24. {
  25. /**
  26. * Test reading XML from string
  27. */
  28. public function testDomFromString()
  29. {
  30. $reader = new XMLReader();
  31. $reader->getDomFromString('<element attr="test"><child attr="subtest">AAA</child></element>');
  32. $this->assertTrue($reader->elementExists('/element/child'));
  33. $this->assertEquals('AAA', $reader->getElement('/element/child')->textContent);
  34. $this->assertEquals('AAA', $reader->getValue('/element/child'));
  35. $this->assertEquals('test', $reader->getAttribute('attr', $reader->getElement('/element')));
  36. $this->assertEquals('subtest', $reader->getAttribute('attr', $reader->getElement('/element'), 'child'));
  37. }
  38. /**
  39. * Test reading XML from zip
  40. */
  41. public function testDomFromZip()
  42. {
  43. $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
  44. $reader = new XMLReader();
  45. $reader->getDomFromZip($pathResources. 'reader.zip', 'test.xml');
  46. $this->assertTrue($reader->elementExists('/element/child'));
  47. $this->assertFalse($reader->getDomFromZip($pathResources. 'reader.zip', 'non_existing_xml_file.xml'));
  48. }
  49. /**
  50. * Test that read from non existing archive throws exception
  51. *
  52. * @expectedException Exception
  53. */
  54. public function testThrowsExceptionOnNonExistingArchive()
  55. {
  56. $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR.DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'files'.DIRECTORY_SEPARATOR;
  57. $reader = new XMLReader();
  58. $reader->getDomFromZip($pathResources. 'readers.zip', 'test.xml');
  59. }
  60. /**
  61. * Test elements count
  62. */
  63. public function testCountElements()
  64. {
  65. $reader = new XMLReader();
  66. $reader->getDomFromString('<element attr="test"><child>AAA</child><child>BBB</child></element>');
  67. $this->assertEquals(2, $reader->countElements('/element/child'));
  68. }
  69. /**
  70. * Test read non existing elements
  71. */
  72. public function testReturnNullOnNonExistingNode()
  73. {
  74. $reader = new XMLReader();
  75. $this->assertEmpty($reader->getElements('/element/children'));
  76. $reader->getDomFromString('<element><child>AAA</child></element>');
  77. $this->assertNull($reader->getElement('/element/children'));
  78. $this->assertNull($reader->getValue('/element/children'));
  79. }
  80. /**
  81. * Test that xpath fails if custom namespace is not registered
  82. */
  83. public function testShouldThrowExceptionIfNamespaceIsNotKnown()
  84. {
  85. try {
  86. $reader = new XMLReader();
  87. $reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
  88. $this->assertTrue($reader->elementExists('/element/test:child'));
  89. $this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
  90. $this->fail();
  91. } catch (\Exception $e) {
  92. $this->assertTrue(true);
  93. }
  94. }
  95. /**
  96. * Test reading XML with manually registered namespace
  97. */
  98. public function testShouldParseXmlWithCustomNamespace()
  99. {
  100. $reader = new XMLReader();
  101. $reader->getDomFromString('<element><test:child xmlns:test="http://phpword.com/my/custom/namespace">AAA</test:child></element>');
  102. $reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
  103. $this->assertTrue($reader->elementExists('/element/test:child'));
  104. $this->assertEquals('AAA', $reader->getElement('/element/test:child')->textContent);
  105. }
  106. /**
  107. * Test that xpath fails if custom namespace is not registered
  108. *
  109. * @expectedException InvalidArgumentException
  110. */
  111. public function testShouldThowExceptionIfTryingToRegisterNamespaceBeforeReadingDoc()
  112. {
  113. $reader = new XMLReader();
  114. $reader->registerNamespace('test', 'http://phpword.com/my/custom/namespace');
  115. }
  116. }