FrxCrosstabTest.php in Forena Reports 8
File
tests/src/Unit/Renderer/FrxCrosstabTest.php
View source
<?php
namespace Drupal\Tests\forena\Unit\Renderer;
use Drupal\forena\Report;
use Drupal\Tests\forena\Unit\FrxTestCase;
class FrxCrosstabTest extends FrxRendererTestCase {
private $doc = '<?xml version="1.0"?>
<!DOCTYPE root [
<!ENTITY nbsp " ">
]>
<html xmlns:frx="urn:FrxReports">
<head>
<title>Report Title</title>
<frx:category>Category</frx:category>
<frx:fields>
</frx:fields>
</head>
<body>
<table frx:renderer="FrxCrosstab" frx:group="{state}" frx:dim="{gender}">
<thead>
<tr><th>State</th><td>users</td></tr>
</thead>
<tbody>
<tr><th>{state}</th><td>{users}</td></tr>
</tbody>
</table>
</body>
</html>';
public function arrayData(array $array) {
$new_array = [];
foreach ($array as $r => $row) {
$new_row = [];
foreach ($row as $key => $value) {
$new_row[$key] = $value['data'];
}
$new_array[] = $new_row;
}
return $new_array;
}
public function testFrxCrosstab() {
$data = $this
->dataManager()
->data('test/crosstab_data');
$this
->pushData($data);
$elements = $this
->render('\\Drupal\\forena\\FrxPlugin\\Renderer\\FrxCrosstab', $this->doc, 'table');
$this
->popData();
$this
->assertGreaterThan(0, count($elements), $elements);
$element = $elements[0];
$this
->assertEquals('table', $element['#type']);
$rows = $element['#rows'];
$this
->assertEquals(2, count($rows), "Correct row grouping count returned");
$data_rows = $this
->arrayData($rows);
$AL = $data_rows[0];
$CA = $data_rows[1];
$header_data[] = $element['#header'];
$headers = $this
->arrayData($header_data);
$h = $headers[0];
$this
->assertEquals(4, count($h));
$this
->assertEquals(4, count($AL), "Correct Number of columns AL");
$this
->assertEquals(4, count($CA), "Correct number of columns CA");
$this
->assertEquals('State', $h[0]);
$this
->assertEquals('Male', $h[1]);
$this
->assertEquals('Female', $h[2]);
$this
->assertEquals('Unknown', $h[3]);
}
}