public static function PHPUnit_Framework_Assert::assertTag

Evaluate an HTML or XML string and assert its structure and/or contents.

The first argument ($matcher) is an associative array that specifies the match criteria for the assertion:

  • `id` : the node with the given id attribute must match the corresponsing value.
  • `tag` : the node type must match the corresponding value.
  • `attributes` : a hash. The node's attributres must match the corresponsing values in the hash.
  • `content` : The text content must match the given value.
  • `parent` : a hash. The node's parent must match the corresponsing hash.
  • `child` : a hash. At least one of the node's immediate children must meet the criteria described by the hash.
  • `ancestor` : a hash. At least one of the node's ancestors must meet the criteria described by the hash.
  • `descendant` : a hash. At least one of the node's descendants must meet the criteria described by the hash.
  • `children` : a hash, for counting children of a node. Accepts the keys:

    • `count` : a number which must equal the number of children that match
    • `less_than` : the number of matching children must be greater than this number
    • `greater_than` : the number of matching children must be less than this number
    • `only` : another hash consisting of the keys to use to match on the children, and only matching children will be counted

<code> // Matcher that asserts that there is an element with an id="my_id". $matcher = array('id' => 'my_id');

// Matcher that asserts that there is a "span" tag. $matcher = array('tag' => 'span');

// Matcher that asserts that there is a "span" tag with the content // "Hello World". $matcher = array('tag' => 'span', 'content' => 'Hello World');

// Matcher that asserts that there is a "span" tag with content matching // the regular expression pattern. $matcher = array('tag' => 'span', 'content' => 'regexp:/Try P(HP|ython)/');

// Matcher that asserts that there is a "span" with an "list" class // attribute. $matcher = array( 'tag' => 'span', 'attributes' => array('class' => 'list') );

// Matcher that asserts that there is a "span" inside of a "div". $matcher = array( 'tag' => 'span', 'parent' => array('tag' => 'div') );

// Matcher that asserts that there is a "span" somewhere inside a // "table". $matcher = array( 'tag' => 'span', 'ancestor' => array('tag' => 'table') );

// Matcher that asserts that there is a "span" with at least one "em" // child. $matcher = array( 'tag' => 'span', 'child' => array('tag' => 'em') );

// Matcher that asserts that there is a "span" containing a (possibly // nested) "strong" tag. $matcher = array( 'tag' => 'span', 'descendant' => array('tag' => 'strong') );

// Matcher that asserts that there is a "span" containing 5-10 "em" tags // as immediate children. $matcher = array( 'tag' => 'span', 'children' => array( 'less_than' => 11, 'greater_than' => 4, 'only' => array('tag' => 'em') ) );

// Matcher that asserts that there is a "div", with an "ul" ancestor and // a "li" parent (with class="enum"), and containing a "span" descendant // that contains an element with id="my_test" and the text "Hello World". $matcher = array( 'tag' => 'div', 'ancestor' => array('tag' => 'ul'), 'parent' => array( 'tag' => 'li', 'attributes' => array('class' => 'enum') ), 'descendant' => array( 'tag' => 'span', 'child' => array( 'id' => 'my_test', 'content' => 'Hello World' ) ) );

// Use assertTag() to apply a $matcher to a piece of $html. $this->assertTag($matcher, $html);

// Use assertTag() to apply a $matcher to a piece of $xml. $this->assertTag($matcher, $xml, '', FALSE); </code>

The second argument ($actual) is a string containing either HTML or XML text to be tested.

The third argument ($message) is an optional message that will be used if the assertion fails.

The fourth argument ($html) is an optional flag specifying whether to load the $actual string into a DOMDocument using the HTML or XML load strategy. It is TRUE by default, which assumes the HTML load strategy. In many cases, this will be acceptable for XML as well.

@since Method available since Release 3.3.0 @author Mike Naberezny <mike@maintainable.com> @author Derek DeVries <derek@maintainable.com>

Parameters

array $matcher:

string $actual:

string $message:

boolean $isHtml:

50 calls to PHPUnit_Framework_Assert::assertTag()
Framework_AssertTest::testAssertAncestorContentAttributes in drupal/core/vendor/phpunit/phpunit/Tests/Framework/AssertTest.php
@covers PHPUnit_Framework_Assert::assertTag
Framework_AssertTest::testAssertChildContentAttributes in drupal/core/vendor/phpunit/phpunit/Tests/Framework/AssertTest.php
@covers PHPUnit_Framework_Assert::assertTag
Framework_AssertTest::testAssertChildrenContentAttributes in drupal/core/vendor/phpunit/phpunit/Tests/Framework/AssertTest.php
@covers PHPUnit_Framework_Assert::assertTag
Framework_AssertTest::testAssertChildSubChildren in drupal/core/vendor/phpunit/phpunit/Tests/Framework/AssertTest.php
@covers PHPUnit_Framework_Assert::assertTag
Framework_AssertTest::testAssertDescendantContentAttributes in drupal/core/vendor/phpunit/phpunit/Tests/Framework/AssertTest.php
@covers PHPUnit_Framework_Assert::assertTag

... See full list

File

drupal/core/vendor/phpunit/phpunit/PHPUnit/Framework/Assert.php, line 2090

Class

PHPUnit_Framework_Assert
A set of assert methods.

Code

public static function assertTag($matcher, $actual, $message = '', $isHtml = TRUE) {
  $dom = PHPUnit_Util_XML::load($actual, $isHtml);
  $tags = PHPUnit_Util_XML::findNodes($dom, $matcher, $isHtml);
  $matched = count($tags) > 0 && $tags[0] instanceof DOMNode;
  self::assertTrue($matched, $message);
}