fDOMDocumentFragment.test.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\fDOMDocumentFragment;
  44. /**
  45. *
  46. * @author Arne Blankerts <arne@blankerts.de>
  47. * @copyright Arne Blankerts <arne@blankerts.de>, All rights reserved.
  48. */
  49. class fDOMDocumentFragmentTest extends \PHPUnit\Framework\TestCase {
  50. /**
  51. * @var fDOMDocument
  52. */
  53. private $dom;
  54. /**
  55. * @var fDOMDocumentFragment
  56. */
  57. private $frag;
  58. public function setUp() {
  59. $this->dom = new fDOMDocument();
  60. $this->frag = $this->dom->createDocumentFragment();
  61. }
  62. public function testAppendedXMLGetsAddedAndIsParsedAsXML() {
  63. $this->frag->appendXML('<some />');
  64. $this->assertEquals('some', $this->frag->firstChild->nodeName);
  65. }
  66. /**
  67. * @expectedException \TheSeer\fDOM\fDOMException
  68. */
  69. public function testTryingToAppendInvalidXMLToFragmentThrowsException() {
  70. $this->frag->appendXML('<foo');
  71. }
  72. public function testCheckingInSameDocumentReturnsTrueOnNodeFromFragment() {
  73. $this->frag->appendXML('<some />');
  74. $this->assertTrue($this->frag->inSameDocument($this->frag->firstChild));
  75. }
  76. public function testAppendingANewElement() {
  77. $node = $this->frag->appendElement('append', 'text');
  78. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  79. $this->assertEquals(1, $this->frag->query('count(append)'));
  80. $this->assertEquals('text', $node->nodeValue);
  81. }
  82. public function testAppendingANewElementWithinANamespace() {
  83. $node = $this->frag->appendElementNS('test:uri', 'append', 'text');
  84. $this->dom->registerNamespace('t', 'test:uri');
  85. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  86. $this->assertEquals(1, $this->frag->query('count(t:append)'));
  87. $this->assertEquals('text', $node->nodeValue);
  88. }
  89. public function testAppendingANewElementWithinANamespaceByPrefix() {
  90. $this->dom->registerNamespace('t', 'test:uri');
  91. $node = $this->frag->appendElementPrefix('t', 'append', 'text');
  92. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  93. $this->assertEquals(1, $this->frag->query('count(t:append)'));
  94. $this->assertEquals('text', $node->nodeValue);
  95. }
  96. public function testAppendingANewElementWithinANamespaceAsTextNodeByPrefix() {
  97. $this->dom->registerNamespace('t', 'test:uri');
  98. $node = $this->frag->appendElementPrefix('t', 'append', 'test & demo', true);
  99. $this->assertInstanceOf('TheSeer\fDOM\fDOMElement', $node);
  100. $this->assertEquals(1, $this->frag->query('count(t:append)'));
  101. $this->assertEquals('test & demo', $node->nodeValue);
  102. }
  103. public function testAppendingATextAsTextnode() {
  104. $node = $this->frag->appendTextNode('test & demo');
  105. $found = $this->frag->queryOne('text()');
  106. $this->assertSame($node, $found);
  107. $this->assertEquals('test & demo', $node->nodeValue);
  108. }
  109. public function testCSSSelectorReturnsCorrectNodes() {
  110. $node = $this->frag->appendElement('append', 'text');
  111. $result = $this->frag->select('append');
  112. $this->assertSame($node, $result->item(0));
  113. $this->assertEquals(1, $result->length);
  114. }
  115. public function testToStringReturnsSerializedXMLString() {
  116. $this->frag->appendElement('append', 'text');
  117. $this->assertEquals('<append>text</append>', (string)$this->frag);
  118. }
  119. }
  120. }