.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees;
use DOMDocument;
use Exception;
use function str_starts_with;
use function trim;
use const LIBXML_PEDANTIC;
/**
* Common functions for testing views
*/
abstract class AbstractViewTest extends TestCase
{
protected const EVIL_VALUE = '';
/**
* Check the view runs without error and generates valid HTML
*
* @param string $view
* @param array> $data
*/
protected function doTestView(string $view, array $data): void
{
foreach ($this->cartesian($data) as $datum) {
$html = view($view, $datum);
$this->validateHTML($html);
}
}
/**
* @param array> $input
*
* @return array>
*/
private function cartesian(array $input): array
{
$result = [[]];
foreach ($input as $key => $values) {
$append = [];
foreach ($result as $product) {
foreach ($values as $item) {
$product[$key] = $item;
$append[] = $product;
}
}
$result = $append;
}
return $result;
}
/**
* @param string $html
*/
private function validateHTML(string $html): void
{
if (str_starts_with($html, '')) {
$xml = $html;
} else {
$xml = '' . $html . '';
}
$doc = new DOMDocument();
$doc->validateOnParse = true;
try {
self::assertTrue($doc->loadXML($xml, LIBXML_PEDANTIC), $html);
} catch (Exception $ex) {
echo $html, PHP_EOL;
self::assertTrue(false);
}
}
}