Skip to content

Commit

Permalink
Catch empty template snippet content
Browse files Browse the repository at this point in the history
  • Loading branch information
theseer committed May 26, 2019
1 parent ee6a0af commit 8f564a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/snippet/TempladoSnippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ private function replaceNode(DOMElement $node): DOMNode {
return $first;
}

if ($dom->childNodes->length === 0) {
private function ensureNotEmpty(DOMDocument $dom): void {
if ($dom->childNodes->length === 0) {
throw new SnippetException('Document cannot be empty');
}

if ($dom->documentElement->childNodes->length === 0) {
throw new SnippetException('Snippet content cannot be empty');
}
}
}
8 changes: 7 additions & 1 deletion tests/snippet/TempladoSnippetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public function testTryingToLoadEmptyDocumentThrowsException(): void {
new TempladoSnippet('foo', $this->snippetDom);
}

public function testTryingToLoadEmptyContentDocumentThrowsException(): void {
$this->snippetDom->loadXML('<?xml version="1.0" ?><p:snippet xmlns:p="https://templado.io/snippets/1.0" />');
$this->expectException(SnippetException::class);
new TempladoSnippet('foo', $this->snippetDom);
}

public function testTargetIdCanBeRetrieved(): void {
$this->snippetDom->loadXML('<?xml version="1.0" ?><p:snippet xmlns:p="https://templado.io/snippets/1.0" id="abc"><content /></p:snippet>');

Expand All @@ -27,7 +33,7 @@ public function testTargetIdCanBeRetrieved(): void {
}

public function testWrongNamespaceThrowsException(): void {
$this->snippetDom->loadXML('<?xml version="1.0" ?><foo xmlns="a:b" />');
$this->snippetDom->loadXML('<?xml version="1.0" ?><foo xmlns="a:b"><node/></foo>');

$this->expectException(SnippetException::class);
new TempladoSnippet('foo', $this->snippetDom);
Expand Down

0 comments on commit 8f564a1

Please sign in to comment.