Skip to content

Instantly share code, notes, and snippets.

@edorian
Created September 5, 2012 13:20
Show Gist options
  • Save edorian/3636420 to your computer and use it in GitHub Desktop.
Save edorian/3636420 to your computer and use it in GitHub Desktop.
assertXPath
/**
* Assert that a XPath matches on a DomNode
*
* Uses the XPath Method boolean() to see if a node test evaluates to true
* It's true if one of the follow conditions is met:
*
* - a number is true if and only if it is neither positive or negative zero nor NaN
* - a node-set is true if and only if it is non-empty
* - a string is true if and only if its length is non-zero
* - an object of a type other than the four basic types is converted to a
* boolean in a way that is dependent on that type
*
* @see http://www.w3.org/TR/xpath/#section-Boolean-Functions
*
* @param string $xpath
*/
public static function assertXPath($xpathQuery, DOMDocument $doc, $message = '') {
$xpath = new \DOMXPath($doc);
self::assertTrue(
$xpath->evaluate("boolean($xpathQuery)"),
'Failed asserting that XPath expression: "' . $xpathQuery . '" evaluates to true. ' . $message
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment