Browse Source

MagicProperties 100% covered, starting Node

wrapper-classes
Dustin Wilson 3 years ago
parent
commit
136b1420a2
  1. 8
      composer.json
  2. 23
      composer.lock
  3. 2
      lib/Document.php
  4. 4
      lib/traits/Node.php
  5. 9
      tests/cases/TestChildNode.php
  6. 1
      tests/cases/TestDocument.php
  7. 79
      tests/cases/TestMagicProperties.php
  8. 43
      tests/cases/TestNode.php
  9. 1
      tests/cases/TestParentNode.php
  10. 2
      tests/phpunit.dist.xml

8
composer.json

@ -46,5 +46,11 @@
"bamarni/composer-bin-plugin": "^1.3",
"daux/daux.io": "^0.16.0",
"mikey179/vfsstream": "^1.6"
}
},
"repositories": [
{
"type": "git",
"url": "mensbeam-gitea:MensBeam/HTML-Parser.git"
}
]
}

23
composer.lock

@ -4,15 +4,15 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c5d8046a2bc8743cc92add9484f3380f",
"content-hash": "3371a9b669d02688e062be96d83f1eff",
"packages": [
{
"name": "mensbeam/html-parser",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://code.mensbeam.com/MensBeam/HTML-Parser",
"reference": "49764a456743ac69074b4f64619cf87af254c071"
"url": "mensbeam-gitea:MensBeam/HTML-Parser.git",
"reference": "29127c51ee3b127fb607df4f5518920046df9704"
},
"require": {
"ext-dom": "*",
@ -42,7 +42,20 @@
"lib/Parser/ctype.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"autoload-dev": {
"psr-4": {
"MensBeam\\HTML\\Test\\": "tests/lib/",
"MensBeam\\HTML\\TestCase\\": "tests/cases/"
}
},
"scripts": {
"post-install-cmd": [
"@composer bin all install"
],
"post-update-cmd": [
"@composer bin all update"
]
},
"license": [
"MIT"
],
@ -59,7 +72,7 @@
}
],
"description": "Parses modern HTML text into a PHP DOMDocument",
"time": "2021-09-28T20:15:07+00:00"
"time": "2021-10-09T03:23:33+00:00"
},
{
"name": "mensbeam/intl",

2
lib/Document.php

@ -67,7 +67,7 @@ class Document extends \DOMDocument {
return null;
}
protected function __set_body(?\DOMNode $value) {
protected function __set_body(\DOMNode $value) {
# On setting, the following algorithm must be run:
#
# 1. If the new value is not a body or frameset element, then throw a

4
lib/traits/Node.php

@ -14,12 +14,12 @@ namespace MensBeam\HTML\DOM;
trait Node {
// Disable C14N
public function C14N($exclusive = null, $with_comments = null, ?array $xpath = null, ?array $ns_prefixes = null): bool {
return false;
throw new DOMException(DOMException::NOT_SUPPORTED, __CLASS__ . ' is meant for XML and buggy; use Document::saveHTML or cast to a string');
}
// Disable C14NFile
public function C14NFile($uri, $exclusive = null, $with_comments = null, ?array $xpath = null, ?array $ns_prefixes = null): bool {
return false;
throw new DOMException(DOMException::NOT_SUPPORTED, __CLASS__ . ' is meant for XML and buggy; use Document::saveHTMLFile');
}
public function getRootNode(): ?\DOMNode {

9
tests/cases/TestChildNode.php

@ -13,7 +13,10 @@ use MensBeam\HTML\DOM\Document;
/** @covers \MensBeam\HTML\DOM\ChildNode */
class TestChildNode extends \PHPUnit\Framework\TestCase {
/** @covers \MensBeam\HTML\DOM\ChildNode::after */
/**
* @covers \MensBeam\HTML\DOM\ChildNode::after
* @covers \MensBeam\HTML\DOM\Node::convertNodesToNode
*/
public function testAfter(): void {
$d = new Document();
$d->appendChild($d->createElement('html'));
@ -23,8 +26,8 @@ class TestChildNode extends \PHPUnit\Framework\TestCase {
$div2 = $d->body->appendChild($d->createElement('div'));
// On node with parent
$div->after($d->createElement('span'), $o, $d->createElement('br'));
$this->assertSame('<body><div></div><span></span>ook<br><div></div></body>', (string)$d->body);
$div->after($d->createElement('span'), $o, 'eek');
$this->assertSame('<body><div></div><span></span>ookeek<div></div></body>', (string)$d->body);
$div->after($o);
// On node with no parent

1
tests/cases/TestDocument.php

@ -121,6 +121,7 @@ class TestDocument extends \PHPUnit\Framework\TestCase {
* @covers \MensBeam\HTML\DOM\Document::preInsertionValidity
* @covers \MensBeam\HTML\DOM\Document::replaceTemplates
* @covers \MensBeam\HTML\DOM\Document::__get_quirksMode
* @covers \MensBeam\HTML\DOM\Node::getRootNode
*/
public function testDocumentCreation(): void {
// Test null source

79
tests/cases/TestMagicProperties.php

@ -0,0 +1,79 @@
<?php
/**
* @license MIT
* Copyright 2017, Dustin Wilson, J. King et al.
* See LICENSE and AUTHORS files for details
*/
declare(strict_types=1);
namespace MensBeam\HTML\DOM\TestCase;
use MensBeam\HTML\DOM\{
Document,
Exception,
MagicProperties
};
/** @covers \MensBeam\HTML\DOM\MagicProperties */
class TestMagicProperties extends \PHPUnit\Framework\TestCase {
public function provideFailures(): iterable {
return [
[ function() {
$d = new Document();
$d->omgWTFBBQ;
}, Exception::NONEXISTENT_PROPERTY ],
[ function() {
$d = new Document();
$d->omgWTFBBQ = 'ook';
}, Exception::NONEXISTENT_PROPERTY ],
[ function() {
$d = new Document();
$d->xpath = 'ook';
}, Exception::READONLY_PROPERTY ],
[ function() {
$d = new Document();
unset($d->xpath);
}, Exception::READONLY_PROPERTY ]
];
}
/**
* @dataProvider provideFailures
* @covers \MensBeam\HTML\DOM\MagicProperties::__get
* @covers \MensBeam\HTML\DOM\MagicProperties::__set
* @covers \MensBeam\HTML\DOM\MagicProperties::__unset
*/
public function testFailures(\Closure $closure, int $errorCode): void {
$this->expectException(Exception::class);
$this->expectExceptionCode($errorCode);
$closure();
}
/** @covers \MensBeam\HTML\DOM\MagicProperties::__isset */
public function testIsset(): void {
$d = new Document();
$this->assertTrue(isset($d->body));
}
/** @covers \MensBeam\HTML\DOM\MagicProperties::__unset */
public function testUnset(): void {
// Nothing allows setting values to null yet, so make one
$d = new class {
use MagicProperties;
protected ?string $_ook = 'ook';
protected function __get_ook(): ?string {
return $this->_ook;
}
protected function __set_ook(?string $value): void {
$this->_ook = $value;
}
};
unset($d->ook);
$this->assertNull($d->ook);
}
}

43
tests/cases/TestNode.php

@ -0,0 +1,43 @@
<?php
/**
* @license MIT
* Copyright 2017, Dustin Wilson, J. King et al.
* See LICENSE and AUTHORS files for details
*/
declare(strict_types=1);
namespace MensBeam\HTML\DOM\TestCase;
use MensBeam\HTML\DOM\{
Document,
DOMException
};
use MensBeam\HTML\Parser;
/** @covers \MensBeam\HTML\DOM\Node */
class TestNode extends \PHPUnit\Framework\TestCase {
public function provideDisabledMethods(): iterable {
return [
[ function() {
$d = new Document();
$d->C14N();
} ],
[ function() {
$d = new Document();
$d->C14NFile('fail');
} ],
];
}
/**
* @dataProvider provideDisabledMethods
* @covers \MensBeam\HTML\DOM\Node::C14N
* @covers \MensBeam\HTML\DOM\Node::C14NFile
*/
public function testDisabledMethods(\Closure $closure): void {
$this->expectException(DOMException::class);
$this->expectExceptionCode(DOMException::NOT_SUPPORTED);
$closure();
}
}

1
tests/cases/TestParentNode.php

@ -113,6 +113,7 @@ class TestParentNode extends \PHPUnit\Framework\TestCase {
* @covers \MensBeam\HTML\DOM\ParentNode::__get_children
* @covers \MensBeam\HTML\DOM\ParentNode::preInsertionValidity
* @covers \MensBeam\HTML\DOM\DOMException::__construct
* @covers \MensBeam\HTML\DOM\Node::getRootNode
*/
public function testPreInsertionValidationFailures(\Closure $closure, int $errorCode = DOMException::HIERARCHY_REQUEST_ERROR): void {
$this->expectException(DOMException::class);

2
tests/phpunit.dist.xml

@ -23,6 +23,8 @@
<file>cases/TestElementMap.php</file>
<file>cases/TestException.php</file>
<file>cases/TestLeafNode.php</file>
<file>cases/TestMagicProperties.php</file>
<file>cases/TestNode.php</file>
<file>cases/TestParentNode.php</file>
</testsuite>
<testsuite name="Serializer">

Loading…
Cancel
Save