-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first set of Tests for Serializer
- Loading branch information
Showing
8 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
|
||
} |