fDOMDocument.test.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. /**
  44. *
  45. * @author Arne Blankerts <arne@blankerts.de>
  46. * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
  47. */
  48. class fDOMDocumentTest extends \PHPUnit\Framework\TestCase {
  49. /**
  50. * @var fDOMDocument
  51. */
  52. private $dom;
  53. public function setUp() {
  54. $this->dom = new fDOMDocument();
  55. }
  56. public function testloadingXMLStringWorks() {
  57. $this->dom->loadXML('<?xml version="1.0" ?><test />');
  58. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $this->dom->documentElement);
  59. }
  60. public function testloadingXMLFileWorks() {
  61. $this->dom->load(__DIR__ . '/_data/valid.xml');
  62. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $this->dom->documentElement);
  63. }
  64. public function testGetDomXPathReturnsXPathObject() {
  65. $this->dom->loadXML('<?xml version="1.0" ?><test />');
  66. $this->assertInstanceOf('TheSeer\fDOM\fDOMXpath', $this->dom->getDomXPath());
  67. }
  68. /**
  69. * @expectedException \TheSeer\fDOM\fDOMException
  70. */
  71. public function testAttemptingToLoadAnXMLStringWithAnUndefinedEntityThrowsException() {
  72. $this->dom->loadXML('<?xml version="1.0" ?><root>&undefined;</root>');
  73. }
  74. /**
  75. * @expectedException \TheSeer\fDOM\fDOMException
  76. */
  77. public function testAttemptingToLoadAnEmptyXMLStringThrowsException() {
  78. $this->dom->loadXML('');
  79. }
  80. /**
  81. * @expectedException \TheSeer\fDOM\fDOMException
  82. */
  83. public function testAttemptingToLoadWithEmptyFilenameThrowsException() {
  84. $this->dom->load('');
  85. }
  86. /**
  87. * @expectedException \TheSeer\fDOM\fDOMException
  88. */
  89. public function testAttemptingToLoadHTMLWithAnEmptyFilenameThrowsException() {
  90. $this->dom->loadHTMLFile('');
  91. }
  92. /**
  93. * @expectedException \TheSeer\fDOM\fDOMException
  94. */
  95. public function testAttemptingToLoadHMLWithAnEmptyStringThrowsException() {
  96. $this->dom->loadHTML('');
  97. }
  98. /**
  99. * @expectedException \TheSeer\fDOM\fDOMException
  100. */
  101. public function testloadingInvalidXMLStringThrowsException() {
  102. $this->dom->loadXML('<?xml version="1.0" ?><broken>');
  103. }
  104. /**
  105. * @expectedException \TheSeer\fDOM\fDOMException
  106. */
  107. public function testTryingToLoadNonExistingFileThrowsException() {
  108. $this->dom->load('_does_not_exist.xml');
  109. }
  110. /**
  111. * @expectedException \TheSeer\fDOM\fDOMException
  112. */
  113. public function testloadingBrokenXMLFileThrowsException() {
  114. $this->dom->load(__DIR__ . '/_data/broken.xml');
  115. }
  116. /**
  117. * @expectedException \TheSeer\fDOM\fDOMException
  118. */
  119. public function testAttemptingToLoadAnXMLFileWithAnUndefinedEntityThrowsException() {
  120. $this->dom->load(__DIR__ . '/_data/undefentity.xml');
  121. }
  122. /**
  123. * @covers \TheSeer\fDOM\fDOMDocument::query
  124. */
  125. public function testQueryReturnsNodeList() {
  126. $this->dom->load(__DIR__ . '/_data/valid.xml');
  127. $list = $this->dom->query('/test');
  128. $this->assertInstanceOf('DomNodelist', $list);
  129. $this->assertEquals(1, $list->length);
  130. }
  131. /**
  132. * @covers \TheSeer\fDOM\fDOMDocument::queryOne
  133. */
  134. public function testQueryOneReturnsElement() {
  135. $this->dom->load(__DIR__ . '/_data/valid.xml');
  136. $node = $this->dom->queryOne('/test');
  137. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  138. }
  139. public function testSaveXMLReturnsCorrectXMLString() {
  140. $xml = file_get_contents(__DIR__ . '/_data/valid.xml');
  141. $this->dom->loadXML($xml);
  142. $this->assertEquals($xml, $this->dom->saveXML());
  143. }
  144. /**
  145. * @expectedException \TheSeer\fDOM\fDOMException
  146. */
  147. public function testSaveXMLThrowsExceptionWithReferenceToNodeFromOtherDocument() {
  148. $dom = new fDOMDocument();
  149. $this->dom->saveXML($dom->createElement('foo'));
  150. }
  151. /**
  152. * @covers \TheSeer\fDOM\fDOMDocument::nodeList2FragMent
  153. */
  154. public function testTransformNodeListToFragmentWorks() {
  155. $this->dom->loadXML('<?xml version="1.0" ?><root><node1/><node2 /></root>');
  156. $frag = $this->dom->nodeList2Fragment($this->dom->query('/root/*'));
  157. $this->assertInstanceOf('TheSeer\fDOM\fDOMDocumentFragment', $frag);
  158. $this->assertEquals(2, $frag->childNodes->length);
  159. }
  160. public function testPrepareQueryReturnsValidXPathString() {
  161. $values = array('key' => 'the "value" of \'values\'');
  162. $xpath = '//some[@value = :key]';
  163. $result = $this->dom->prepareQuery($xpath, $values);
  164. $this->assertEquals('//some[@value = concat("the ",\'"\',"value",\'"\'," of \'values\'")]', $result);
  165. }
  166. public function testRegisteringANamespaceWithPrefixWorks() {
  167. $this->dom->registerNamespace('test', 'test:uri');
  168. $this->assertAttributeEquals(array('test' => 'test:uri'), 'prefixes', $this->dom);
  169. }
  170. /**
  171. * @expectedException \TheSeer\fDOM\fDOMException
  172. */
  173. public function testCreatingElementWithInvalidNameThrowsException() {
  174. $node = $this->dom->createElement('in valid');
  175. }
  176. public function testCreatingElementWithoutText() {
  177. $node = $this->dom->createElement('name');
  178. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  179. $this->assertEquals('name', $node->nodeName);
  180. }
  181. /**
  182. * @covers \TheSeer\fDOM\fDOMDocument::createElementPrefix
  183. */
  184. public function testCreatingNewElementByprefix() {
  185. $this->dom->registerNamespace('test', 'test:uri');
  186. $node = $this->dom->createElementPrefix('test', 'node');
  187. $this->assertEquals('test:uri', $node->namespaceURI);
  188. }
  189. /**
  190. * @covers \TheSeer\fDOM\fDOMDocument::createElementPrefix
  191. * @expectedException \TheSeer\fDOM\fDOMException
  192. */
  193. public function testTryingToCreateNewElementByprefixWithUndefinedPrefixThrowsException() {
  194. $this->dom->createElementPrefix('test', 'node');
  195. }
  196. public function testSettingContentUnescapedForNewElementRemainsIntact() {
  197. $node = $this->dom->createElement('test', "test &amp; demo");
  198. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  199. $this->assertEquals('test & demo', $node->nodeValue);
  200. }
  201. /**
  202. * @expectedException \TheSeer\fDOM\fDOMException
  203. */
  204. public function testSettingContentUnescapedForNewElementThrowsExceptionOnInvalidEntity() {
  205. $node = $this->dom->createElement('test', "test & demo");
  206. }
  207. public function testSettingContentAsTextNodeForNewElementEncodesEntities() {
  208. $node = $this->dom->createElement('test', "test &amp; demo", TRUE);
  209. $this->assertEquals('test &amp; demo', $node->nodeValue);
  210. }
  211. public function testSettingContentUnescapedForNewElementWithNamespaceRemainsIntact() {
  212. $node = $this->dom->createElementNS('test:uri', 'test', "test &amp; demo");
  213. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  214. $this->assertEquals('test & demo', $node->nodeValue);
  215. }
  216. /**
  217. * @expectedException \TheSeer\fDOM\fDOMException
  218. */
  219. public function testSettingContentUnescapedForNewElementWithNamespaceThrowsExceptionOnInvalidEntity() {
  220. $node = $this->dom->createElementNS('test:uri', 'test', "test & demo");
  221. }
  222. public function testSettingContentAsTextNodeForNewElementWithNamespaceEncodesEntities() {
  223. $node = $this->dom->createElementNS('test:uri', 'test', "test &amp; demo", TRUE);
  224. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  225. $this->assertEquals('test &amp; demo', $node->nodeValue);
  226. }
  227. /**
  228. * @covers \TheSeer\fDOM\fDOMDocument::queryOne
  229. */
  230. public function testThatTwoNodesAreIdentifiedAsBeingInTheSameDocument() {
  231. $this->dom->loadXML('<?xml version="1.0" ?><root><node /></root>');
  232. $node = $this->dom->queryOne('//node');
  233. $this->assertTrue($this->dom->inSameDocument($node));
  234. }
  235. /**
  236. * @covers \TheSeer\fDOM\fDOMDocument::inSameDocument
  237. */
  238. public function testThatANodeFromADifferentDocumentIsNotConsideredAsInSameDocument() {
  239. $dom = new fDOMDocument();
  240. $node = $dom->createElement('foo');
  241. $this->dom->loadXML('<?xml version="1.0" ?><root />');
  242. $this->assertFalse($this->dom->documentElement->inSameDocument($node));
  243. }
  244. /**
  245. * @covers \TheSeer\fDOM\fDOMDocument::inSameDocument
  246. */
  247. public function testInSameDocumentWorksForDOMDocument() {
  248. $dom = new fDOMDocument();
  249. $this->assertFalse($this->dom->inSameDocument($dom));
  250. }
  251. public function testAppendElementCreatesANewNodeAndAttachesIt() {
  252. $node = $this->dom->appendElement('test');
  253. $this->assertSame($node, $this->dom->documentElement);
  254. }
  255. public function testAppendElementNSCreatesANewNodeAndAttachesIt() {
  256. $node = $this->dom->appendElementNS('test:uri', 'test');
  257. $this->assertSame($node, $this->dom->documentElement);
  258. }
  259. /**
  260. * @covers \TheSeer\fDOM\fDOMDocument::__clone
  261. */
  262. public function testCloningTriggersCreationOfNewDOMXPathInstance() {
  263. $this->dom->loadXML('<?xml version="1.0" ?><test />');
  264. $xp1 = $this->dom->getDOMXPath();
  265. $clone = clone $this->dom;
  266. $xp2 = $clone->getDOMXPath();
  267. $this->assertNotSame($xp2, $xp1);
  268. }
  269. /**
  270. * @covers \TheSeer\fDOM\fDOMDocument::__clone
  271. */
  272. public function testRegisteredNamespacePrefixesGetCopiedToClonedDocument() {
  273. $this->dom->loadXML('<?xml version="1.0" ?><foo:test xmlns:foo="test:uri" />');
  274. $this->dom->registerNamespace('foo', 'test:uri');
  275. $clone = clone $this->dom;
  276. $node = $clone->queryOne('//foo:test');
  277. $this->assertSame($clone->documentElement, $node);
  278. }
  279. /**
  280. * https://github.com/theseer/fDOMDocument/issues/15
  281. */
  282. public function testQueryReturnsNodeFromClonedDocument() {
  283. $this->dom->loadXML('<?xml version="1.0" ?><test />');
  284. $clone = clone $this->dom;
  285. $node = $clone->queryOne('/test');
  286. $this->assertNotSame($this->dom->documentElement, $node);
  287. }
  288. public function testCSSSelectorReturnsCorrectNodes() {
  289. $this->dom->load(__DIR__ . '/_data/selector.xml');
  290. $result = $this->dom->select('child');
  291. $this->assertEquals(2, $result->length);
  292. $this->assertEquals('child', $result->item(0)->nodeName);
  293. $this->assertEquals('child', $result->item(1)->nodeName);
  294. }
  295. public function testCSSSelectorHonorsContextNode() {
  296. $this->dom->load(__DIR__ . '/_data/selector.xml');
  297. $ctx = $this->dom->getElementsByTagName('child')->item(0);
  298. $result = $this->dom->select('child', $ctx);
  299. $this->assertEquals(1, $result->length);
  300. $this->assertEquals('child', $result->item(0)->nodeName);
  301. $this->assertEquals('other', $result->item(0)->getAttribute('attr'));
  302. }
  303. }
  304. }