| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Html\Test; |
| 4 |
|
| 5 |
use DOMDocument; |
| 6 |
use ipl\Html\ValidHtml; |
| 7 |
use PHPUnit\Framework\TestCase as BaseTestCase; |
| 8 |
|
| 9 |
abstract class TestCase extends BaseTestCase |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Assert that HTML is equal |
| 13 |
* |
| 14 |
* @param string $expected |
| 15 |
* @param ValidHtml $actual |
| 16 |
*/ |
| 17 |
protected function assertHtml(string $expected, ValidHtml $actual): void |
| 18 |
{ |
| 19 |
$expectedHtml = str_replace( |
| 20 |
"\n", |
| 21 |
'', |
| 22 |
preg_replace('/^\s+/m', '', trim($expected)) |
| 23 |
); |
| 24 |
$actualHtml = $actual->render(); |
| 25 |
|
| 26 |
$expectedDom = new DOMDocument(); |
| 27 |
$this->assertTrue($expectedDom->loadHTML($expectedHtml, LIBXML_NOERROR), 'Expected HTML is not valid'); |
| 28 |
$actualDom = new DOMDocument(); |
| 29 |
$this->assertTrue($actualDom->loadHTML($actualHtml, LIBXML_NOERROR), 'Actual HTML is not valid'); |
| 30 |
|
| 31 |
$this->assertEquals($expectedDom, $actualDom); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @deprecated Use {@link assertHtml()} instead. assertRendersHtml() suffers from the fact that the HTML being |
| 36 |
* processed must have a root node, e.g. the HTML `<b>foo</b><b>bar</b>` would always fail with "Extra content at |
| 37 |
* the end of the document". {@link assertHtml()} just does the job. |
| 38 |
*/ |
| 39 |
protected function assertRendersHtml(string $html, ValidHtml $element): void |
| 40 |
{ |
| 41 |
$this->assertXmlStringEqualsXmlString($html, $element->render()); |
| 42 |
} |
| 43 |
} |