Browse Source

Filling out code coverage on protected methods

wrapper-classes
Dustin Wilson 3 years ago
parent
commit
0b41f8d7b4
  1. 2
      README.md
  2. 121
      tests/cases/TestNode.php

2
README.md

@ -49,7 +49,7 @@ The primary aim of this library is accuracy. However, due either to limitations
4. Per the specification an actual HTML document cannot be created outside of the parser itself unless created via `DOMImplementation::createHTMLDocument`. Also, per the spec `DOMImplementation` cannot be instantiated via its constructor. This would require in this library's use case first creating a document then creating an HTML document via its implementation. This is impractical, so in this library (like PHP DOM itself) a `DOMImplementation` can be instantiated independent of a document.
5. The specification shows `Document` as being able to be instantated through its constructor and shows `XMLDocument` as inheriting from `Document`. In browsers `XMLDocument` cannot be instantiated through its constructor. We will follow the specification here and allow it.
6. CDATA section nodes, text nodes, and document fragments per the specification can be instantiated by their constructors independent of the `Document::createCDATASectionNode`, `Document::createTextNode`, and `Document::createDocumentFragment` methods respectively. This is not possible currently with this library and probably never will be due to the difficulty of implementing it and the awkwardness of their being different from every other node type in this respect.
7. This implementation will not implement the `NodeIterator` and `TreeWalker` APIs. They are horribly conceived and impractical APIs that few people actually use because it's literally easier to write recursive loops to walk through the DOM than it is to use those APIs. They have instead been replaced with the `ParentNode::walk` generator.
7. This implementation will not implement the `NodeIterator` and `TreeWalker` APIs. They are horribly conceived and impractical APIs that few people actually use because it's literally easier and faster to write recursive loops to walk through the DOM than it is to use those APIs. They have instead been replaced with the `ParentNode::walk` generator.
8. All of the `Range` APIs will also not be implemented due to the sheer complexity of creating them in userland and how it adds undue difficulty to node manipulation in the "core" DOM. Numerous operations reference in excrutiating detail what to do with Ranges when manipulating nodes and would have to be added here to be compliant or mostly so -- slowing everything else down in the process on an already front-heavy library.
9. The `DOMParser` and `XMLSerializer` APIs will not be implemented because they are ridiculous and limited in their scope. For instance, `DOMParser::parseFromString` won't set a document's character set to anything but UTF-8. This library needs to be able to print to other encodings due to the nature of how it is used. `Document::__construct` will accept optional `$source` and `$charset` arguments, and there are both `Document::load` and `Document::loadFile` methods for loading DOM from a string or a file respectively.
10. Aside from `HTMLElement`, `HTMLTemplateElement`, `MathMLElement`, and `SVGElement` none of the specific derived element classes (such as `HTMLAnchorElement` or `SVGSVGElement`) are implemented. The focus on this library will be on the core DOM before moving onto those. They may or may not be implemented in the future.

121
tests/cases/TestNode.php

@ -90,61 +90,40 @@ class TestNode extends \PHPUnit\Framework\TestCase {
$text = $d->createTextNode('ook');
$frag = $d->createDocumentFragment();
$frag->appendChild($d->createTextNode('ook'));
// Node::cloneNode on attribute node
$attrClone = $attr->cloneNode();
$this->assertNotSame($attrClone, $attr);
$this->assertTrue($attrClone->isEqualNode($attr));
// Node::cloneNode on CDATA section
$cdataClone = $cdata->cloneNode();
$this->assertNotSame($cdataClone, $cdata);
$this->assertTrue($cdataClone->isEqualNode($cdata));
// Node::cloneNode on comment
$commentClone = $comment->cloneNode();
$this->assertNotSame($commentClone, $comment);
$this->assertTrue($commentClone->isEqualNode($comment));
// Node::cloneNode on document
$dClone = $d->cloneNode(true);
$this->assertNotSame($dClone, $d);
$this->assertTrue($dClone->isEqualNode($d));
// Node::cloneNode on doctype
$doctypeClone = $doctype->cloneNode();
$this->assertNotSame($doctypeClone, $doctype);
$this->assertTrue($doctypeClone->isEqualNode($doctype));
// Node::cloneNode on document fragment
$fragClone = $frag->cloneNode(true);
$this->assertNotSame($fragClone, $frag);
$this->assertTrue($fragClone->isEqualNode($frag));
// Node::cloneNode on element
$elementClone = $element->cloneNode(true);
$this->assertNotSame($elementClone, $element);
$this->assertTrue($elementClone->isEqualNode($element));
// Node::cloneNode on element with attribute
$bodyClone = $body->cloneNode(true);
$this->assertNotSame($bodyClone, $body);
$this->assertTrue($bodyClone->isEqualNode($body));
// Node::cloneNode on template element
$templateClone = $template->cloneNode(true);
$this->assertNotSame($templateClone, $template);
$this->assertTrue($templateClone->isEqualNode($template));
// Node::cloneNode on processing instruction
$piClone = $pi->cloneNode();
$this->assertNotSame($piClone, $pi);
$this->assertTrue($piClone->isEqualNode($pi));
// Node::cloneNode on text node
$textClone = $text->cloneNode();
$this->assertNotSame($textClone, $text);
$this->assertTrue($textClone->isEqualNode($text));
$d3 = new Document('<!DOCTYPE html><html><template>Ook</template></html>');
$nodes = [
// Attribute node
$attr,
// CDATA section node
$cdata,
// Comment
$comment,
// Document
$d,
// Parsed document
$d3,
// Doctype
$doctype,
// Document fragment
$frag,
// Element
$element,
// Body with id attribute
$body,
// Template
$template,
// Processing instruction
$pi,
// Text
$text
];
foreach ($nodes as $n) {
$clone = $n->cloneNode(true);
$this->assertNotSame($clone, $n);
$this->assertTrue($clone->isEqualNode($n));
}
}
@ -322,6 +301,38 @@ class TestNode extends \PHPUnit\Framework\TestCase {
$this->assertSame('<body><template></template>ook<div></div></body>', (string)$d->body);
}
/** @covers \MensBeam\HTML\DOM\Node::isEqualNode */
public function testMethod_isEqualNode(): void {
$d = new Document();
// Only thing left to check for are failures.
// Different node types
$this->assertFalse($d->isEqualNode($d->createElement('html')));
// Different doctypes
$this->assertFalse($d->implementation->createDocumentType('html', '', '')->isEqualNode($d->implementation->createDocumentType('lmht', 'ook', 'eek')));
// Different elements
$html = $d->createElement('html');
$html2 = $d->createElement('html');
$body = $d->createElement('body');
$body->setAttribute('id', 'ook');
$body2 = $html2->appendChild($d->createElement('body'));
$body2->setAttribute('id', 'eek');
$this->assertFalse($html->isEqualNode($body));
$this->assertFalse($body->isEqualNode($body2));
$this->assertFalse($html->isEqualNode($html2));
$html->appendChild($body);
$this->assertFalse($html->isEqualNode($html2));
// Different text nodes
$this->assertFalse($d->createTextNode('ook')->isEqualNode($d->createTextNode('eek')));
// Different comments
$this->assertFalse($d->createComment('ook')->isEqualNode($d->createComment('eek')));
}
/**
* @covers \MensBeam\HTML\DOM\Document::__construct

Loading…
Cancel
Save