assertEquals( $expected_lines[$line], $actual_lines[$line] );
}
}
function createActualHtmlLines($actual_html) {
// remove newline from the end of table HTML
$actual_html = preg_replace('/\n$/', '', $actual_html);
// remove all tabs from the HTML (they are only needed for nicer formatting
$actual_html = str_replace( "\t", "", $actual_html);
// convert actual HTML into array of lines
return explode("\n", $actual_html);
}
/**
* Creates the expected HTML output from an abstract table description
*
* The output is an array of lines of HTML
*
*
* $table = array(
* "thead" => array(
* array("foo", "bar", "baz"),
* ),
* "tbody" => array(
* array("cell1", "cell2", "cell3"),
* array("cell1", "cell2", "cell3"),
* array("cell1", "cell2", "cell3"),
* ...
* ),
* );
*
*/
function createExpectedHtmlLines($table) {
$start = array("");
$thead = $this->createExpectedHtmlRows( $table, "thead", "th", false );
$tfoot = $this->createExpectedHtmlRows( $table, "tfoot", "td", false );
$tbody = $this->createExpectedHtmlRows( $table, "tbody", "td", true );
$end = array("
");
return array_merge($start, $thead, $tfoot, $tbody, $end);
}
function createExpectedHtmlRows( $table, $section, $td="td", $odd_even=true) {
// ignore empty sections, e.g. missing footer
if ( !isset($table[$section]) ) {
return array();
}
$rows = array();
$rows[] = "<$section>";
$odd_even_counter=0;
foreach ( $table[$section] as $row ) {
$odd_even_counter++;
$class = $this->createOddEvenClass($odd_even, $odd_even_counter);
$rows[] = "";
$rows = array_merge( $rows, $this->createExpectedHtmlCells($row, $td) );
$rows[] = "
";
}
$rows[] = "$section>";
return $rows;
}
function createExpectedHtmlCells( $row, $td="td" ) {
$rows = array();
foreach ( $row as $cell ) {
// there can be two types of cells:
// simple - only the text of cell content
// complex - array( cell_content, class_names )
if ( is_array($cell) ) {
$rows[] = "<$td class=\"$cell[1]\">$cell[0]$td>";
}
else {
$rows[] = "<$td>$cell$td>";
}
}
return $rows;
}
function createOddEvenClass( $odd_even, $odd_even_counter ) {
// do nothing, when odd and even rows need no distinction
if (!$odd_even) {
return "";
}
$odd_even_class = ($odd_even_counter % 2 == 0) ? "even" : "odd";
return " class=\"$odd_even_class\"";
}
}
?>