mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
Build/Test Tools: Various docblock improvements within test utilities.
See #51802 git-svn-id: https://develop.svn.wordpress.org/trunk@50265 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -2,10 +2,23 @@
|
||||
|
||||
// Misc help functions and utilities.
|
||||
|
||||
/**
|
||||
* Returns a string of the required length containing random characters. Note that
|
||||
* the maximum possible string length is 32.
|
||||
*
|
||||
* @param int $len Optional. The required length. Default 32.
|
||||
* @return string The string.
|
||||
*/
|
||||
function rand_str( $len = 32 ) {
|
||||
return substr( md5( uniqid( rand() ) ), 0, $len );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string of the required length containing random characters.
|
||||
*
|
||||
* @param int $len The required length.
|
||||
* @return string The string.
|
||||
*/
|
||||
function rand_long_str( $length ) {
|
||||
$chars = 'abcdefghijklmnopqrstuvwxyz';
|
||||
$string = '';
|
||||
@@ -18,7 +31,12 @@ function rand_long_str( $length ) {
|
||||
return $string;
|
||||
}
|
||||
|
||||
// Strip leading and trailing whitespace from each line in the string.
|
||||
/**
|
||||
* Strips leading and trailing whitespace from each line in the string.
|
||||
*
|
||||
* @param string $txt The text.
|
||||
* @return string Text with line-leading and line-trailing whitespace stripped.
|
||||
*/
|
||||
function strip_ws( $txt ) {
|
||||
$lines = explode( "\n", $txt );
|
||||
$result = array();
|
||||
@@ -33,9 +51,11 @@ function strip_ws( $txt ) {
|
||||
|
||||
/*
|
||||
* Helper class for testing code that involves actions and filters.
|
||||
*
|
||||
* Typical use:
|
||||
* $ma = new MockAction();
|
||||
* add_action( 'foo', array( &$ma, 'action' ) );
|
||||
*
|
||||
* $ma = new MockAction();
|
||||
* add_action( 'foo', array( &$ma, 'action' ) );
|
||||
*/
|
||||
class MockAction {
|
||||
public $events;
|
||||
@@ -240,11 +260,31 @@ class TestXMLParser {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an XML string into an array tree structure.
|
||||
*
|
||||
* The output of this function can be passed to xml_find() to find nodes by their path.
|
||||
*
|
||||
* @param string $in The XML string.
|
||||
* @return array XML as an array.
|
||||
*/
|
||||
function xml_to_array( $in ) {
|
||||
$p = new TestXMLParser( $in );
|
||||
return $p->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds XML nodes by a given "path".
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* $tree = xml_to_array( $rss );
|
||||
* $items = xml_find( $tree, 'rss', 'channel', 'item' );
|
||||
*
|
||||
* @param array $tree An array tree structure of XML, typically from xml_to_array().
|
||||
* @param string ...$elements Names of XML nodes to create a "path" to find within the XML.
|
||||
* @return array Array of matching XML node information.
|
||||
*/
|
||||
function xml_find( $tree, ...$elements ) {
|
||||
$n = count( $elements );
|
||||
$out = array();
|
||||
|
||||
Reference in New Issue
Block a user