Dustin Wilson
2 years ago
14 changed files with 426 additions and 92 deletions
@ -0,0 +1,159 @@ |
|||||
|
<?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, |
||||
|
Node, |
||||
|
XPathException, |
||||
|
XPathExpression, |
||||
|
XPathResult |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
/** @covers \MensBeam\HTML\DOM\XPathExpression */ |
||||
|
class TestXPathExpression extends \PHPUnit\Framework\TestCase { |
||||
|
/** |
||||
|
* @covers \MensBeam\HTML\DOM\XPathExpression::__construct |
||||
|
* |
||||
|
* @covers \MensBeam\HTML\DOM\Attr::__get_localName |
||||
|
* @covers \MensBeam\HTML\DOM\Attr::__get_namespaceURI |
||||
|
* @covers \MensBeam\HTML\DOM\Attr::__get_ownerElement |
||||
|
* @covers \MensBeam\HTML\DOM\Attr::__set_value |
||||
|
* @covers \MensBeam\HTML\DOM\Document::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Document::__get_body |
||||
|
* @covers \MensBeam\HTML\DOM\Document::__get_documentElement |
||||
|
* @covers \MensBeam\HTML\DOM\Document::createAttributeNS |
||||
|
* @covers \MensBeam\HTML\DOM\Document::createElementNS |
||||
|
* @covers \MensBeam\HTML\DOM\Document::load |
||||
|
* @covers \MensBeam\HTML\DOM\DocumentOrElement::validateAndExtract |
||||
|
* @covers \MensBeam\HTML\DOM\DOMImplementation::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Element::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Element::getAttributeNodeNS |
||||
|
* @covers \MensBeam\HTML\DOM\Element::setAttributeNode |
||||
|
* @covers \MensBeam\HTML\DOM\Element::setAttributeNodeNS |
||||
|
* @covers \MensBeam\HTML\DOM\Element::setAttributeNS |
||||
|
* @covers \MensBeam\HTML\DOM\Node::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Node::__get_ownerDocument |
||||
|
* @covers \MensBeam\HTML\DOM\Node::appendChild |
||||
|
* @covers \MensBeam\HTML\DOM\Node::getInnerDocument |
||||
|
* @covers \MensBeam\HTML\DOM\Node::getInnerNode |
||||
|
* @covers \MensBeam\HTML\DOM\Node::hasChildNodes |
||||
|
* @covers \MensBeam\HTML\DOM\Node::locateNamespace |
||||
|
* @covers \MensBeam\HTML\DOM\Node::lookupNamespaceURI |
||||
|
* @covers \MensBeam\HTML\DOM\Node::postInsertionBugFixes |
||||
|
* @covers \MensBeam\HTML\DOM\Node::postParsingTemplatesFix |
||||
|
* @covers \MensBeam\HTML\DOM\Node::preInsertionBugFixes |
||||
|
* @covers \MensBeam\HTML\DOM\Node::preInsertionValidity |
||||
|
* @covers \MensBeam\HTML\DOM\XPathEvaluatorBase::createExpression |
||||
|
* @covers \MensBeam\HTML\DOM\XPathEvaluatorBase::createNSResolver |
||||
|
* @covers \MensBeam\HTML\DOM\XPathNSResolver::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\XPathNSResolver::lookupNamespaceURI |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Document::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Document::__get_wrapperNode |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Document::__get_xpath |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Document::getWrapperNode |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::get |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::has |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::key |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::set |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Reflection::createFromProtectedConstructor |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Reflection::getProtectedProperty |
||||
|
*/ |
||||
|
function testMethod_constructor(): void { |
||||
|
$d = new Document('<!DOCTYPE><html></html>'); |
||||
|
$d->documentElement->setAttributeNS(Node::XMLNS_NAMESPACE, 'xmlns:poop', 'https://poop.poop'); |
||||
|
$poop = $d->body->appendChild($d->createElementNS('https://poop.poop', 'poop:poop')); |
||||
|
|
||||
|
$this->assertSame(XPathExpression::class, $d->createExpression('//poop:poop', $d->createNSResolver($d))::class); |
||||
|
$this->assertSame(XPathExpression::class, $d->createExpression('//span')::class); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
function provideMethod_constructor__errors(): iterable { |
||||
|
return [ |
||||
|
[ function() { |
||||
|
$d = new Document(); |
||||
|
$d->createExpression('//fail:fail'); |
||||
|
}, |
||||
|
XPathException::UNRESOLVABLE_NAMESPACE_PREFIX ], |
||||
|
|
||||
|
[ function() { |
||||
|
$d = new Document(); |
||||
|
$d->createExpression('//fail:fail', $d->createNSResolver($d)); |
||||
|
}, |
||||
|
XPathException::UNRESOLVABLE_NAMESPACE_PREFIX ], |
||||
|
|
||||
|
[ function() { |
||||
|
$d = new Document(); |
||||
|
$d->createExpression('fail?'); |
||||
|
}, |
||||
|
XPathException::INVALID_EXPRESSION ], |
||||
|
|
||||
|
[ function() { |
||||
|
$d = new Document(); |
||||
|
$d->createExpression('fail?', $d->createNSResolver($d)); |
||||
|
}, |
||||
|
XPathException::INVALID_EXPRESSION ], |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @dataProvider provideMethod_constructor__errors |
||||
|
* @covers \MensBeam\HTML\DOM\XPathExpression::__construct |
||||
|
* |
||||
|
* @covers \MensBeam\HTML\DOM\Document::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\DOMImplementation::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Node::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\XPathEvaluate::xpathErrorHandler |
||||
|
* @covers \MensBeam\HTML\DOM\XPathEvaluatorBase::createExpression |
||||
|
* @covers \MensBeam\HTML\DOM\XPathException::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Document::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::has |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::key |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::set |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Reflection::createFromProtectedConstructor |
||||
|
*/ |
||||
|
function testMethod_constructor__errors(\Closure $closure, int $errorCode): void { |
||||
|
$this->expectException(XPathException::class); |
||||
|
$this->expectExceptionCode($errorCode); |
||||
|
$closure(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* @covers \MensBeam\HTML\DOM\XPathExpression::evaluate |
||||
|
* |
||||
|
* @covers \MensBeam\HTML\DOM\Document::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Document::load |
||||
|
* @covers \MensBeam\HTML\DOM\DOMImplementation::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Node::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Node::getInnerDocument |
||||
|
* @covers \MensBeam\HTML\DOM\Node::hasChildNodes |
||||
|
* @covers \MensBeam\HTML\DOM\Node::postParsingTemplatesFix |
||||
|
* @covers \MensBeam\HTML\DOM\XPathEvaluate::xpathEvaluate |
||||
|
* @covers \MensBeam\HTML\DOM\XPathEvaluatorBase::createExpression |
||||
|
* @covers \MensBeam\HTML\DOM\XPathExpression::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\XPathResult::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\XPathResult::__get_booleanValue |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Document::__construct |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Document::__get_xpath |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::has |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::key |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\NodeCache::set |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Reflection::createFromProtectedConstructor |
||||
|
* @covers \MensBeam\HTML\DOM\Inner\Reflection::getProtectedProperty |
||||
|
*/ |
||||
|
function testMethod_evaluate(): void { |
||||
|
$d = new Document('<!DOCTYPE html><html><body><span><span>Ook</span></span><span></span></body></html>'); |
||||
|
$e = $d->createExpression('//span'); |
||||
|
$this->assertTrue($e->evaluate($d, XPathResult::BOOLEAN_TYPE)->booleanValue); |
||||
|
} |
||||
|
} |
@ -0,0 +1,149 @@ |
|||||
|
<?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, |
||||
|
HTMLElement, |
||||
|
Node, |
||||
|
XPathException, |
||||
|
XPathResult |
||||
|
}; |
||||
|
|
||||
|
|
||||
|
/** @covers \MensBeam\HTML\DOM\XPathResult */ |
||||
|
class TestXPathResult extends \PHPUnit\Framework\TestCase { |
||||
|
function testIteration(): void { |
||||
|
$d = new Document('<!DOCTYPE html><html><body><span><span>Ook</span></span><span></span></body></html>'); |
||||
|
$result = $d->evaluate('.//span', $d); |
||||
|
foreach ($result as $k => $r) { |
||||
|
$this->assertSame(HTMLElement::class, $r::class); |
||||
|
$this->assertSame(HTMLElement::class, $result[$k]::class); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
function testMethod_iterateNext(): void { |
||||
|
$d = new Document('<!DOCTYPE html><html><body><span></span></body></html>'); |
||||
|
$result = $d->evaluate('.//span', $d); |
||||
|
$this->assertSame($d->getElementsByTagName('span')[0], $result->iterateNext()); |
||||
|
$this->assertNull($result->iterateNext()); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
function testMethod_iterateNext__errors(): void { |
||||
|
$this->expectException(XPathException::class); |
||||
|
$this->expectExceptionCode(XPathException::TYPE_ERROR); |
||||
|
$d = new Document(); |
||||
|
$d->evaluate('.//fail', $d, null, XPathResult::ORDERED_NODE_SNAPSHOT_TYPE)->iterateNext(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
function testMethod_snapshotItem(): void { |
||||
|
$d = new Document('<!DOCTYPE html><html><body><span></span></body></html>'); |
||||
|
$this->assertSame($d->getElementsByTagName('span')[0], $d->evaluate('.//span', $d, null, XPathResult::ORDERED_NODE_SNAPSHOT_TYPE)->snapshotItem(0)); |
||||
|
$this->assertNull($d->evaluate('.//span', $d, null, XPathResult::ORDERED_NODE_SNAPSHOT_TYPE)->snapshotItem(42)); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
function testMethod_snapshotItem__errors(): void { |
||||
|
$this->expectException(XPathException::class); |
||||
|
$this->expectExceptionCode(XPathException::TYPE_ERROR); |
||||
|
$d = new Document(); |
||||
|
$d->evaluate('.//fail', $d)->snapshotItem(0); |
||||
|
} |
||||
|
|
||||
|
function testMethod_validateStorage__errors(): void { |
||||
|
$this->expectException(XPathException::class); |
||||
|
$this->expectExceptionCode(XPathException::TYPE_ERROR); |
||||
|
$d = new Document(); |
||||
|
$result = $d->evaluate('count(.//fail)', $d, null, XPathResult::NUMBER_TYPE); |
||||
|
$result[0]; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
function provideProperty__errors(): iterable { |
||||
|
return [ |
||||
|
[ function() { |
||||
|
$d = new Document(); |
||||
|
$result = $d->evaluate('name(//fail)', $d, null, XPathResult::NUMBER_TYPE); |
||||
|
$result->booleanValue; |
||||
|
} ], |
||||
|
|
||||
|
[ function() { |
||||
|
$d = new Document(); |
||||
|
$result = $d->evaluate('//fail', $d, null, XPathResult::BOOLEAN_TYPE); |
||||
|
$result->numberValue; |
||||
|
} ], |
||||
|
|
||||
|
[ function() { |
||||
|
$d = new Document(); |
||||
|
$result = $d->evaluate('//fail', $d, null, XPathResult::BOOLEAN_TYPE); |
||||
|
$result->singleNodeValue; |
||||
|
} ], |
||||
|
|
||||
|
[ function() { |
||||
|
$d = new Document(); |
||||
|
$result = $d->evaluate('//fail', $d, null, XPathResult::BOOLEAN_TYPE); |
||||
|
$result->stringValue; |
||||
|
} ] |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
/** @dataProvider provideProperty__errors */ |
||||
|
function testProperty__errors(\Closure $closure): void { |
||||
|
$this->expectException(XPathException::class); |
||||
|
$this->expectExceptionCode(XPathException::TYPE_ERROR); |
||||
|
$closure(); |
||||
|
} |
||||
|
|
||||
|
function testProperty_invalidIteratorState(): void { |
||||
|
$d = new Document(); |
||||
|
$this->assertFalse($d->evaluate('//span', $d)->invalidIteratorState); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
function testProperty_offsetSet_offsetUnset(): void { |
||||
|
$d = new Document('<!DOCTYPE html><html><body><span><span>Ook</span></span><span></span></body></html>'); |
||||
|
$result = $d->evaluate('.//span', $d); |
||||
|
$result[1] = 'ook'; |
||||
|
$this->assertNotSame('ook', $result[1]); |
||||
|
unset($result[1]); |
||||
|
$this->assertSame(HTMLElement::class, $result[1]::class); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
function testProperty_resultType(): void { |
||||
|
$d = new Document('<!DOCTYPE html><html><body><span><span>Ook</span></span><span></span></body></html>'); |
||||
|
$this->assertEquals(XPathResult::ORDERED_NODE_ITERATOR_TYPE, $d->evaluate('.//span', $d->body, null, XPathResult::ORDERED_NODE_ITERATOR_TYPE)->resultType); |
||||
|
$this->assertEquals(XPathResult::ORDERED_NODE_ITERATOR_TYPE, $d->evaluate('.//span', $d->body)->resultType); |
||||
|
$this->assertEquals(XPathResult::NUMBER_TYPE, $d->evaluate('count(.//span)', $d->body, null, XPathResult::NUMBER_TYPE)->resultType); |
||||
|
$this->assertEquals(XPathResult::NUMBER_TYPE, $d->evaluate('count(.//span)', $d->body)->resultType); |
||||
|
$this->assertEquals(XPathResult::STRING_TYPE, $d->evaluate('name(.//span)', $d->body, null, XPathResult::STRING_TYPE)->resultType); |
||||
|
$this->assertEquals(XPathResult::STRING_TYPE, $d->evaluate('name(.//span)', $d->body)->resultType); |
||||
|
$this->assertEquals(XPathResult::BOOLEAN_TYPE, $d->evaluate('.//span', $d->body, null, XPathResult::BOOLEAN_TYPE)->resultType); |
||||
|
$this->assertEquals(XPathResult::BOOLEAN_TYPE, $d->evaluate('not(.//span)', $d->body, null, XPathResult::BOOLEAN_TYPE)->resultType); |
||||
|
$this->assertEquals(XPathResult::BOOLEAN_TYPE, $d->evaluate('not(.//span)', $d->body)->resultType); |
||||
|
$this->assertEquals(XPathResult::UNORDERED_NODE_SNAPSHOT_TYPE, $d->evaluate('.//span', $d->body, null, XPathResult::UNORDERED_NODE_SNAPSHOT_TYPE)->resultType); |
||||
|
$this->assertEquals(XPathResult::FIRST_ORDERED_NODE_TYPE, $d->evaluate('.//span', $d->body, null, XPathResult::FIRST_ORDERED_NODE_TYPE)->resultType); |
||||
|
} |
||||
|
|
||||
|
function testProperty_snapshotLength(): void { |
||||
|
$d = new Document('<!DOCTYPE html><html><body><span><span>Ook</span></span><span></span></body></html>'); |
||||
|
$this->assertEquals(3, $d->evaluate('.//span', $d, null, XPathResult::ORDERED_NODE_SNAPSHOT_TYPE)->snapshotLength); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
function testProperty_snapshotLength__errors(): void { |
||||
|
$this->expectException(XPathException::class); |
||||
|
$this->expectExceptionCode(XPathException::TYPE_ERROR); |
||||
|
$d = new Document(); |
||||
|
$d->evaluate('.//fail', $d)->snapshotLength; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue