Page MenuHomeWickedGov Phorge

RootElementMatcher.php
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

RootElementMatcher.php

<?php
namespace WMDE\HamcrestHtml;
use DOMDocument;
use Hamcrest\Description;
use Hamcrest\Matcher;
use Hamcrest\TypeSafeDiagnosingMatcher;
class RootElementMatcher extends TypeSafeDiagnosingMatcher {
/**
* @var Matcher|null
*/
private $tagMatcher;
/**
* @param Matcher|null $tagMatcher
*
* @return static
*/
public static function havingRootElement( ?Matcher $tagMatcher = null ) {
return new static( $tagMatcher );
}
public function __construct( ?Matcher $tagMatcher = null ) {
parent::__construct( self::TYPE_OBJECT, DOMDocument::class );
$this->tagMatcher = $tagMatcher;
}
public function describeTo( Description $description ) {
$description->appendText( 'having root element ' );
if ( $this->tagMatcher ) {
$description->appendDescriptionOf( $this->tagMatcher );
}
}
/**
* @param DOMDocument $item
* @param Description $mismatchDescription
*
* @return bool
*/
protected function matchesSafelyWithDiagnosticDescription(
$item, Description $mismatchDescription
) {
$DOMNodeList = $item->documentElement->childNodes->item( 0 )->childNodes;
if ( $DOMNodeList->length > 1 ) {
// TODO Test this description
$mismatchDescription->appendText( 'having ' . $DOMNodeList->length . ' root elements ' );
return false;
}
$target = $DOMNodeList->item( 0 );
if ( !$target ) {
// TODO Reproduce?
$mismatchDescription->appendText( 'having no root elements ' );
return false;
}
if ( $this->tagMatcher ) {
$mismatchDescription->appendText( 'root element ' );
$this->tagMatcher->describeMismatch( $target, $mismatchDescription );
return $this->tagMatcher->matches( $target );
}
return true;
}
}

File Metadata

Mime Type
text/x-php
Expires
Aug 19 2026, 21:48 (7 w, 6 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
f8/b4/7ffb10c2ddef416470c68cb6dca4
Default Alt Text
RootElementMatcher.php (1 KB)

Event Timeline