Dustin Wilson
3 years ago
18 changed files with 204 additions and 58 deletions
@ -0,0 +1,33 @@ |
|||
<?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; |
|||
|
|||
/** |
|||
* Not in standard. Exists so all node types that cannot contain children will have |
|||
* the insertion methods disabled. |
|||
*/ |
|||
trait LeafNode { |
|||
use Node; |
|||
|
|||
public function appendChild($node) { |
|||
throw new DOMException(DOMException::HIERARCHY_REQUEST_ERROR); |
|||
} |
|||
|
|||
public function insertBefore($node, $child = null) { |
|||
throw new DOMException(DOMException::HIERARCHY_REQUEST_ERROR); |
|||
} |
|||
|
|||
public function removeChild($child) { |
|||
throw new DOMException(DOMException::HIERARCHY_REQUEST_ERROR); |
|||
} |
|||
|
|||
public function replaceChild($node, $child) { |
|||
throw new DOMException(DOMException::HIERARCHY_REQUEST_ERROR); |
|||
} |
|||
} |
@ -0,0 +1,34 @@ |
|||
<?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; |
|||
|
|||
|
|||
/** @covers \MensBeam\HTML\DOM\ChildNode */ |
|||
class TestChildNode extends \PHPUnit\Framework\TestCase { |
|||
/** @covers \MensBeam\HTML\DOM\ChildNode::after */ |
|||
public function testAfter(): void { |
|||
$d = new Document(); |
|||
$d->appendChild($d->createElement('html')); |
|||
$d->documentElement->appendChild($d->createElement('body')); |
|||
$div = $d->body->appendChild($d->createElement('div')); |
|||
$o = $d->body->appendChild($d->createTextNode('ook')); |
|||
$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($o); |
|||
|
|||
// On node with no parent |
|||
$c = $d->createComment('ook'); |
|||
$this->assertNull($c->after($d->createTextNode('ook'))); |
|||
} |
|||
} |
@ -0,0 +1,39 @@ |
|||
<?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, |
|||
ElementMap |
|||
}; |
|||
use MensBeam\HTML\Parser; |
|||
|
|||
|
|||
/** |
|||
* @covers \MensBeam\HTML\DOM\ElementMap |
|||
*/ |
|||
class TestElementMap extends \PHPUnit\Framework\TestCase { |
|||
/** @covers \MensBeam\HTML\DOM\ElementMap::add */ |
|||
public function testAdd(): void { |
|||
$d = new Document(); |
|||
$t = $d->createElement('template'); |
|||
$this->assertTrue(ElementMap::add($t)); |
|||
$this->assertFalse(ElementMap::add($t)); |
|||
} |
|||
|
|||
|
|||
/** @covers \MensBeam\HTML\DOM\ElementMap::delete */ |
|||
public function testDelete(): void { |
|||
$d = new Document(); |
|||
$t = $d->createElement('template'); |
|||
$this->assertTrue(ElementMap::add($t)); |
|||
$this->assertTrue(ElementMap::delete($t)); |
|||
$this->assertFalse(ElementMap::delete($t)); |
|||
} |
|||
} |
@ -0,0 +1,49 @@ |
|||
<?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 |
|||
}; |
|||
|
|||
|
|||
/** @covers \MensBeam\HTML\DOM\LeafNode */ |
|||
class TestLeafNode extends \PHPUnit\Framework\TestCase { |
|||
public function provideDisabledMethods(): iterable { |
|||
return [ |
|||
[ function($d, $n) { |
|||
$n->appendChild($d->createElement('fail')); |
|||
} ], |
|||
[ function($d, $n) { |
|||
$n->insertBefore($d->createElement('fail')); |
|||
} ], |
|||
[ function($d, $n) { |
|||
$n->removeChild($d->createElement('fail')); |
|||
} ], |
|||
[ function($d, $n) { |
|||
$n->replaceChild($d->createElement('fail2'), $d->createElement('fail')); |
|||
} ], |
|||
]; |
|||
} |
|||
|
|||
/** |
|||
* @dataProvider provideDisabledMethods |
|||
* @covers \MensBeam\HTML\DOM\LeafNode::appendChild |
|||
* @covers \MensBeam\HTML\DOM\LeafNode::insertBefore |
|||
* @covers \MensBeam\HTML\DOM\LeafNode::removeChild |
|||
* @covers \MensBeam\HTML\DOM\LeafNode::replaceChild |
|||
*/ |
|||
public function testDisabledMethods(\Closure $closure): void { |
|||
$this->expectException(DOMException::class); |
|||
$this->expectExceptionCode(DOMException::HIERARCHY_REQUEST_ERROR); |
|||
$d = new Document(); |
|||
$closure($d, $d->createTextNode('ook')); |
|||
} |
|||
} |
Loading…
Reference in new issue