Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/Html/HtmlLibxmlTest.php
T
oleibman 32e59142a3 Suppress Libxml Warnings in Reader/Html
Fix #647, which went stale in 2019 and is now reopened.

Fix #849, which went stale in 2019 and is now reopened.

Html Reader currently echoes any warning messages from Dom::loadHtml when it parses Html input. These are generally not needed, and may cause PhpSpreadsheet to fail needlessly. A new property `$suppressLoadWarnings` is added. It will additionally default to `null`, which will avoid any BC breaks. However, the default will be changed to `true` in a future release. Should the caller be interested in any messages which are suppressed when `true` is in effect, these are available through new method `getLibxmlMessages` after the Html has been loaded.
2025-12-27 11:16:45 -08:00

50 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Html;
use PhpOffice\PhpSpreadsheet\Reader\Html;
use PHPUnit\Framework\TestCase;
/**
* Dom loadHtml usually succeeds even with invalid Html,
* although it will generate warning messages.
* This member demonstrates a method less intrusive than
* set_error_handler to detect if there has been a problem.
*/
class HtmlLibxmlTest extends TestCase
{
public function testLoadInvalidString(): void
{
$libxml = libxml_use_internal_errors();
$html = '<table<>';
$reader = new Html();
$reader->setSuppressLoadWarnings(true);
$reader->loadFromString($html);
self::assertNotEmpty($reader->getLibxmlMessages());
self::assertSame($libxml, libxml_use_internal_errors());
}
public function testLoadValidString(): void
{
$libxml = libxml_use_internal_errors();
$html = '<table>';
$reader = new Html();
$reader->setSuppressLoadWarnings(true);
$reader->loadFromString($html);
self::assertEmpty($reader->getLibxmlMessages());
self::assertSame($libxml, libxml_use_internal_errors());
}
public function testLoadValidStringNoSuppress(): void
{
$libxml = libxml_use_internal_errors();
$html = '<table>';
$reader = new Html();
$reader->loadFromString($html);
self::assertEmpty($reader->getLibxmlMessages());
self::assertSame($libxml, libxml_use_internal_errors());
}
}