Skip to content

Commit

Permalink
Add first set of Tests for Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
theseer committed Aug 8, 2023
1 parent dd61c2c commit 0f32f3c
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/_data/serializer/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title property="foo">Hello world</title>
<script src="/foo.js"></script>
</head>
<body>
<h1>hello world.</h1>
<p>test</p>
<br />
<p>test 2</p>
<p class="foo" id="import">namespace prefixed</p>
</body>
</html>
14 changes: 14 additions & 0 deletions tests/_data/serializer/input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<html lang="en">
<head xmlns="http://www.w3.org/1999/xhtml">
<title property="foo">Hello world</title>
<script src="/foo.js" />
</head>
<body xmlns="http://www.w3.org/1999/xhtml">
<h1>hello world.</h1>
<p>test</p>
<br/>
<p>test 2</p>
<default:p xmlns:default="http://www.w3.org/1999/xhtml" class="foo" id="import">namespace prefixed</default:p>
</body>
</html>
14 changes: 14 additions & 0 deletions tests/_data/serializer/nocleaning.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head xmlns="http://www.w3.org/1999/xhtml">
<title property="foo">Hello world</title>
<script src="/foo.js"></script>
</head>
<body xmlns="http://www.w3.org/1999/xhtml">
<h1>hello world.</h1>
<p>test</p>
<br />
<p>test 2</p>
<default:p xmlns:default="http://www.w3.org/1999/xhtml" class="foo" id="import">namespace prefixed</default:p>
</body>
</html>
13 changes: 13 additions & 0 deletions tests/_data/serializer/nodoctype.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title property="foo">Hello world</title>
<script src="/foo.js"></script>
</head>
<body>
<h1>hello world.</h1>
<p>test</p>
<br />
<p>test 2</p>
<p class="foo" id="import">namespace prefixed</p>
</body>
</html>
14 changes: 14 additions & 0 deletions tests/_data/serializer/nordfa.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Hello world</title>
<script src="/foo.js"></script>
</head>
<body>
<h1>hello world.</h1>
<p>test</p>
<br />
<p>test 2</p>
<p class="foo" id="import">namespace prefixed</p>
</body>
</html>
15 changes: 15 additions & 0 deletions tests/_data/serializer/withxmlheader.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title property="foo">Hello world</title>
<script src="/foo.js"></script>
</head>
<body>
<h1>hello world.</h1>
<p>test</p>
<br />
<p>test 2</p>
<p class="foo" id="import">namespace prefixed</p>
</body>
</html>
42 changes: 42 additions & 0 deletions tests/serializer/EmptyElementsFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);
namespace Templado\Engine;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(EmptyElementsFilter::class)]
class EmptyElementsFilterTest extends TestCase {

#[DataProvider('selfContainedElementsProvider')]
public function testSelfContainedElementsGetClosed(string $expected, string $input): void {
$this->assertEquals(
$expected,
(new EmptyElementsFilter())->apply($input)
);
}

public static function selfContainedElementsProvider(): array {
$tagList = [
'base', 'br', 'meta', 'link', 'img', 'input', 'button', 'hr', 'embed',
'param', 'source', 'track', 'area', 'keygen',
];

$map = [];

foreach ($tagList as $tag) {
$map[$tag] = [
\sprintf('<%s />', $tag),
\sprintf('<%1$s></%1$s>', $tag)
];
}

return $map;
}

public function testRegexErrorsAreTurnedIntoException(): void {
$this->iniSet('pcre.backtrack_limit', '100');
$this->expectException(EmptyElementsFilterException::class);
(new EmptyElementsFilter())->apply(\file_get_contents(__DIR__ . '/../_data/filter/regex_backtrack.html'));
}
}
83 changes: 83 additions & 0 deletions tests/serializer/HTMLSerializerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php declare(strict_types = 1);
namespace Templado\Engine;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

#[CoversClass(HTMLSerializer::class)]
#[UsesClass(Document::class)]
#[UsesClass(EmptyElementsFilter::class)]
#[UsesClass(NamespaceCleaningTransformation::class)]
#[UsesClass(Selection::class)]
#[UsesClass(StaticNodeList::class)]
#[UsesClass(TransformationProcessor::class)]
#[UsesClass(XMLHeaderFilter::class)]
#[UsesClass(XPathSelector::class)]
#[UsesClass(StripRDFaAttributesTransformation::class)]
class HTMLSerializerTest extends TestCase {

public function testSerializesDocumentWithDefaultSettingsAsExpected() {
$doc = $this->createInputDocument();

$this->assertSame(
file_get_contents(__DIR__ . '/../_data/serializer/default.html'),
$doc->asString((new HTMLSerializer()))
);
}

public function testSerializesDocumentWithoutDoctypeIfRequested() {
$this->assertSame(
file_get_contents(__DIR__ . '/../_data/serializer/nodoctype.html'),
$this->createInputDocument()->asString((new HTMLSerializer())->noHtml5Doctype())
);
}

public function testSerializesDocumentWithXMLHeaderIfRequested() {
$this->assertSame(
file_get_contents(__DIR__ . '/../_data/serializer/withxmlheader.html'),
$this->createInputDocument()->asString((new HTMLSerializer())->keepXMLHeader())
);
}

public function testStripsRDFaIfRequested() {
$this->assertSame(
file_get_contents(__DIR__ . '/../_data/serializer/nordfa.html'),
$this->createInputDocument()->asString((new HTMLSerializer())->stripRDFa())
);
}

public function testSerializesDocumentWithoutCleaningIfRequested() {
$this->assertSame(
file_get_contents(__DIR__ . '/../_data/serializer/nocleaning.html'),
$this->createInputDocument()->asString((new HTMLSerializer())->disableNamespaceCleaning())
);
}

public function testAddedFilterGetsApplied(): void {
$this->assertSame(
'replaced-by-filter',
$this->createInputDocument()->asString(
(new HTMLSerializer())->addFilter(new class implements Filter {
public function apply(string $content) : string {
return 'replaced-by-filter';
}
})
)
);
}

public function testAddedTransformationGetsApplies(): void {
$this->assertSame(
file_get_contents(__DIR__ . '/../_data/serializer/nordfa.html'),
$this->createInputDocument()->asString((new HTMLSerializer())->addTransformation(
new StripRDFaAttributesTransformation()
))
);
}

private function createInputDocument(): Document {
return Document::fromString(file_get_contents(__DIR__ . '/../_data/serializer/input.xml'));
}

}

0 comments on commit 0f32f3c

Please sign in to comment.