MDL-44725 Availability: Add utility API to make unit testing easier (7)

Previously there was no way in PHP to create the JSON values used for
availability, except by manually hard-coding them (the main code to
do this normally is in JavaScript).

This makes unit tests hard to read, so I have implemented static
functions tree::get_root_json and tree::get_nested_json which allow
for easier-to-read unit tests.
This commit is contained in:
sam marshall 2014-08-26 18:07:16 +01:00
parent 9c85baa8c5
commit f9103882dc
4 changed files with 243 additions and 102 deletions

View file

@ -621,4 +621,99 @@ class tree extends tree_node {
}
return $changed;
}
/**
* Returns a JSON object which corresponds to a tree.
*
* Intended for unit testing, as normally the JSON values are constructed
* by JavaScript code.
*
* This function generates 'nested' (i.e. not root-level) trees.
*
* @param array $children Array of JSON objects from component children
* @param string $op Operator (tree::OP_xx)
* @return stdClass JSON object
* @throws coding_exception If you get parameters wrong
*/
public static function get_nested_json(array $children, $op = self::OP_AND) {
// Check $op and work out its type.
switch($op) {
case self::OP_AND:
case self::OP_NOT_OR:
case self::OP_OR:
case self::OP_NOT_AND:
break;
default:
throw new \coding_exception('Invalid $op');
}
// Do simple tree.
$result = new \stdClass();
$result->op = $op;
$result->c = $children;
return $result;
}
/**
* Returns a JSON object which corresponds to a tree at root level.
*
* Intended for unit testing, as normally the JSON values are constructed
* by JavaScript code.
*
* The $show parameter can be a boolean for all OP_xx options. For OP_AND
* and OP_NOT_OR where you have individual show options, you can specify
* a boolean (same for all) or an array.
*
* @param array $children Array of JSON objects from component children
* @param string $op Operator (tree::OP_xx)
* @param bool|array $show Whether 'show' option is turned on (see above)
* @return stdClass JSON object ready for encoding
* @throws coding_exception If you get parameters wrong
*/
public static function get_root_json(array $children, $op = self::OP_AND, $show = true) {
// Get the basic object.
$result = self::get_nested_json($children, $op);
// Check $op type.
switch($op) {
case self::OP_AND:
case self::OP_NOT_OR:
$multishow = true;
break;
case self::OP_OR:
case self::OP_NOT_AND:
$multishow = false;
break;
}
// Add show options depending on operator.
if ($multishow) {
if (is_bool($show)) {
$result->showc = array_pad(array(), count($result->c), $show);
} else if (is_array($show)) {
// The JSON will break if anything isn't an actual bool, so check.
foreach ($show as $item) {
if (!is_bool($item)) {
throw new \coding_exception('$show array members must be bool');
}
}
// Check the size matches.
if (count($show) != count($result->c)) {
throw new \coding_exception('$show array size does not match $children');
}
$result->showc = $show;
} else {
throw new \coding_exception('$show must be bool or array');
}
} else {
if (!is_bool($show)) {
throw new \coding_exception('For this operator, $show must be bool');
}
$result->show = $show;
}
return $result;
}
}