fDOMElement.test.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Copyright (c) 2010-2017 Arne Blankerts <arne@blankerts.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Arne Blankerts nor the names of contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT * NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER ORCONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. *
  33. * @category PHP
  34. * @package TheSeer\fDOM
  35. * @author Arne Blankerts <arne@blankerts.de>
  36. * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
  37. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  38. * @link http://github.com/theseer/fdomdocument
  39. *
  40. */
  41. namespace TheSeer\fDOM\Tests {
  42. use TheSeer\fDOM\fDOMDocument;
  43. use TheSeer\fDOM\fDOMElement;
  44. /**
  45. *
  46. * @author Arne Blankerts <arne@blankerts.de>
  47. * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
  48. */
  49. class fDOMElementTest extends \PHPUnit\Framework\TestCase {
  50. /**
  51. * @var fDOMDocument
  52. */
  53. private $dom;
  54. /**
  55. * @var fDOMElement
  56. */
  57. private $node;
  58. public function setUp() {
  59. $this->dom = new fDOMDocument();
  60. $this->dom->loadXML('<?xml version="1.0" ?><root><node><child/></node><node /></root>');
  61. $this->node = $this->dom->documentElement;
  62. }
  63. /**
  64. * The query is a forwarder to the DOMDocument, so just checking if the forwarding works is enough
  65. */
  66. public function testQueryReturnsNodelist() {
  67. $list = $this->node->query('//node');
  68. $this->assertInstanceOf('DOMNodelist', $list);
  69. $this->assertEquals(2, $list->length);
  70. }
  71. /**
  72. * The query is a forwarder to the DOMDocument, so just checking if the forwarding works is enough
  73. */
  74. public function testQueryOneReturnsNode() {
  75. $node = $this->node->queryOne('//root');
  76. $this->assertSame($this->node, $node);
  77. }
  78. public function testAppendingAnXMLStringCreatesAFragment() {
  79. $frag = $this->node->appendXML('<append />');
  80. $this->assertInstanceOf('TheSeer\fDOM\fDOMDocumentFragment', $frag);
  81. $this->assertEquals(1, $this->node->query('count(append)'));
  82. }
  83. public function testAppendingANewElement() {
  84. $node = $this->node->appendElement('append', 'text');
  85. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  86. $this->assertEquals(1, $this->node->query('count(append)'));
  87. $this->assertEquals('text', $node->nodeValue);
  88. }
  89. public function testAppendingANewElementAsTextNode() {
  90. $node = $this->node->appendElement('append', 'test & demo', true);
  91. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  92. $this->assertEquals(1, $this->node->query('count(append)'));
  93. $this->assertEquals('test & demo', $node->nodeValue);
  94. }
  95. public function testAppendingANewElementWithinANamespace() {
  96. $node = $this->node->appendElementNS('test:uri', 'append', 'text');
  97. $this->dom->registerNamespace('t','test:uri');
  98. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  99. $this->assertEquals(1, $this->node->query('count(t:append)'));
  100. $this->assertEquals('text', $node->nodeValue);
  101. }
  102. public function testAppendingANewElementWithinANamespaceAsTextNode() {
  103. $node = $this->node->appendElementNS('test:uri', 'append', 'test & demo', true);
  104. $this->dom->registerNamespace('t','test:uri');
  105. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  106. $this->assertEquals(1, $this->node->query('count(t:append)'));
  107. $this->assertEquals('test & demo', $node->nodeValue);
  108. }
  109. public function testAppendingANewElementWithinANamespaceByPrefix() {
  110. $this->dom->registerNamespace('t','test:uri');
  111. $node = $this->node->appendElementPrefix('t', 'append', 'text');
  112. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  113. $this->assertEquals(1, $this->node->query('count(t:append)'));
  114. $this->assertEquals('text', $node->nodeValue);
  115. }
  116. public function testAppendingANewElementWithinANamespaceAsTextNodeByPrefix() {
  117. $this->dom->registerNamespace('t','test:uri');
  118. $node = $this->node->appendElementPrefix('t', 'append', 'test & demo', true);
  119. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  120. $this->assertEquals(1, $this->node->query('count(t:append)'));
  121. $this->assertEquals('test & demo', $node->nodeValue);
  122. }
  123. public function testAppendingATextAsTextnode() {
  124. $node = $this->node->appendTextNode('test & demo');
  125. $found = $this->node->queryOne('text()');
  126. $this->assertSame($node, $found);
  127. $this->assertEquals('test & demo', $node->nodeValue);
  128. }
  129. public function testGettingNonExistingAttributeReturnsDefaultValue() {
  130. $res = $this->node->getAttribute('missing','default');
  131. $this->assertEquals('default', $res);
  132. }
  133. public function testGettingNonExistingAttributeWithNamespaceReturnsDefaultValue() {
  134. $res = $this->node->getAttributeNS('some:uri', 'missing','default');
  135. $this->assertEquals('default', $res);
  136. }
  137. public function testSettingAttributeValueWithPlainValue() {
  138. $attr = $this->node->setAttribute('test', 'value');
  139. $this->assertTrue($this->node->hasAttribute('test'));
  140. $this->assertInstanceOf('DOMAttr', $attr);
  141. $this->assertEquals('value', $this->node->getAttribute('test'));
  142. }
  143. public function testSettingAttributeValueWithEntitiesEncodesProperly() {
  144. $attr = $this->node->setAttribute('test', '&amp;');
  145. $this->assertTrue($this->node->hasAttribute('test'));
  146. $this->assertInstanceOf('DOMAttr', $attr);
  147. $this->assertEquals('&amp;', $this->node->getAttribute('test'));
  148. }
  149. public function testSettingAttributeValueWithEntitiesAndKeepEntitiesEnabledDoesNotEncode() {
  150. $attr = $this->node->setAttribute('test', '&amp;', true);
  151. $this->assertTrue($this->node->hasAttribute('test'));
  152. $this->assertInstanceOf('DOMAttr', $attr);
  153. $this->assertEquals('&', $this->node->getAttribute('test'));
  154. }
  155. public function testSettingNamespacedAttributeValueWithPlainValue() {
  156. $this->node->setAttributeNS('some:uri','s:attr', 'value');
  157. $this->assertTrue($this->node->hasAttributeNS('some:uri','attr'));
  158. $this->assertEquals('value', $this->node->getAttributeNS('some:uri','attr'));
  159. }
  160. public function testSettingNamespacedAttributeValueWithEntitiesEncodesProperly() {
  161. $this->node->setAttributeNS('test:uri', 't:test', '&amp;');
  162. $this->assertTrue($this->node->hasAttributeNS('test:uri', 'test'));
  163. $this->assertEquals('&amp;', $this->node->getAttributeNS('test:uri','test'));
  164. }
  165. public function testSettingNamespacedAttributeValueWithEntitiesAndKeepEntitiesEnabledDoesNotEncode() {
  166. $attr = $this->node->setAttributeNS('test:uri', 't:test', '&amp;', true);
  167. $this->assertTrue($this->node->hasAttributeNS('test:uri', 'test'));
  168. $this->assertInstanceOf('DOMAttr', $attr);
  169. $this->assertEquals('&', $this->node->getAttributeNS('test:uri', 'test'));
  170. }
  171. public function testSettingMultipleAttributesFromArray() {
  172. $attrs = array('a1' => 'v1', 'a2' => 'v2');
  173. $this->node->setAttributes($attrs);
  174. $this->assertEquals('v1', $this->node->getAttribute('a1'));
  175. $this->assertEquals('v2', $this->node->getAttribute('a2'));
  176. }
  177. public function testSettingMultipleAttributesWithNamespaceFromArray() {
  178. $attrs = array('a1' => 'v1', 'a2' => 'v2');
  179. $this->node->setAttributesNS('some:uri','s', $attrs);
  180. $this->assertEquals('v1', $this->node->getAttributeNS('some:uri', 'a1'));
  181. $this->assertEquals('v2', $this->node->getAttributeNS('some:uri', 'a2'));
  182. }
  183. public function testGetChildrenByTagnameReturnsCorrectNodelist() {
  184. $list = $this->node->getChildrenByTagName('node');
  185. $this->assertInstanceOf('DOMNodeList', $list);
  186. $this->assertEquals(2, $list->length);
  187. }
  188. public function testGetChildrenByTagnameNSReturnsCorrectNodelist() {
  189. $this->dom->loadXML('<?xml version="1.0" ?><root xmlns="test:uri"><node><child/></node><node /></root>');
  190. $this->node = $this->dom->documentElement;
  191. $list = $this->node->getChildrenByTagNameNS('test:uri', 'node');
  192. $this->assertInstanceOf('DOMNodeList', $list);
  193. $this->assertEquals(2, $list->length);
  194. }
  195. public function testInSameDocumentForwardsToOwnerDocumentAndReturnsCorrectValue() {
  196. $rc = $this->node->inSameDocument($this->node->firstChild);
  197. $this->assertTrue($rc);
  198. }
  199. }
  200. }