Skip to content

Commit

Permalink
refactor: rename "modular" to "partial" wherever it exists (#289)
Browse files Browse the repository at this point in the history
closes #281
  • Loading branch information
g105b authored Nov 3, 2021
1 parent a275434 commit 6099617
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Features at a glance

+ Binding of dynamic data - bind key value pairs, associative arrays, lists of associative arrays and even nested lists.
+ HTML components - organise and reuse DOM trees by storing separate components in their own HTML files, and including them using custom HTML tags.
+ Page templates - create HTML documents that "extend" others.
+ Page templates - create partial HTML documents that "extend" others.
+ Easily modularise CSS by selecting their custom tag names.

[dom]: https://www.php.gt/dom
4 changes: 2 additions & 2 deletions src/ComponentExpander.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Gt\Dom\Element;
use Throwable;

class ComponentExpander extends ModularContentExpander {
class ComponentExpander extends PartialContentExpander {
/** @return Element[] */
public function expand(Element $context = null):array {
$expandedComponentArray = [];
Expand All @@ -26,7 +26,7 @@ public function expand(Element $context = null):array {
$name = strtolower($element->tagName);

try {
$content = $this->modularContent->getContent($name);
$content = $this->partialContent->getContent($name);
$element->innerHTML = $content;
array_push($expandedComponentArray, $element);
$recursiveExpandedComponents = $this->expand($element);
Expand Down
4 changes: 0 additions & 4 deletions src/ModularContentDirectoryNotFoundException.php

This file was deleted.

4 changes: 0 additions & 4 deletions src/ModularContentFileNotFoundException.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/ModularContent.php → src/PartialContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

use Gt\Dom\HTMLDocument;

class ModularContent {
class PartialContent {
public function __construct(
private string $dirPath
) {
if(!is_dir($this->dirPath)) {
throw new ModularContentDirectoryNotFoundException("The modular content path does not exist: $this->dirPath");
throw new PartialContentDirectoryNotFoundException("The partial content path does not exist: $this->dirPath");
}
}

public function getContent(string $name, string $extension = "html"):string {
$filePath = $this->dirPath . "/" . $name . ".$extension";
if(!is_file($filePath)) {
throw new ModularContentFileNotFoundException("The modular content file does not exist: $filePath");
throw new PartialContentFileNotFoundException("The partial content file does not exist: $filePath");
}

return file_get_contents($filePath);
Expand Down
4 changes: 4 additions & 0 deletions src/PartialContentDirectoryNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace Gt\DomTemplate;

class PartialContentDirectoryNotFoundException extends DomTemplateException {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

use Gt\Dom\Document;

abstract class ModularContentExpander {
abstract class PartialContentExpander {
public function __construct(
protected Document $document,
protected ModularContent $modularContent
protected PartialContent $partialContent
) {
}

Expand Down
4 changes: 4 additions & 0 deletions src/PartialContentFileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
namespace Gt\DomTemplate;

class PartialContentFileNotFoundException extends DomTemplateException {}
4 changes: 2 additions & 2 deletions src/PartialExpander.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Gt\Dom\HTMLDocument;
use Gt\Dom\HTMLElement\HTMLElement;

class PartialExpander extends ModularContentExpander {
class PartialExpander extends PartialContentExpander {
/**
* @return string[] A list of names of partials that have been expanded,
* in the order that they were expanded.
Expand All @@ -24,7 +24,7 @@ public function expand(Element $context = null):array {
break;
}

$partialDocument = $this->modularContent->getHTMLDocument($extends);
$partialDocument = $this->partialContent->getHTMLDocument($extends);
$partialDocumentArray[$extends] = $partialDocument;
$context = $partialDocument;
}
Expand Down
22 changes: 11 additions & 11 deletions test/phpunit/ComponentExpanderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,59 @@
namespace Gt\DomTemplate\Test;

use Gt\DomTemplate\ComponentExpander;
use Gt\DomTemplate\ModularContent;
use Gt\DomTemplate\ModularContentFileNotFoundException;
use Gt\DomTemplate\PartialContent;
use Gt\DomTemplate\PartialContentFileNotFoundException;
use Gt\DomTemplate\Test\TestFactory\DocumentTestFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ComponentExpanderTest extends ModularContentTestCase {
class ComponentExpanderTest extends PartialContentTestCase {
public function testExpand_doesNothingWhenNoMatchingFiles():void {
$modularContent = self::mockModularContent("_component");
$partialContent = self::mockPartialContent("_component");
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_COMPONENT);
$sut = new ComponentExpander($document, $modularContent);
$sut = new ComponentExpander($document, $partialContent);
self::assertEmpty($sut->expand());
}

public function testExpand_returnsArrayOfExpandedElements():void {
$html = "<h2>This has been replaced!</h2> <p>If you can read this, your custom element is working!</p>";

$modularContent = self::mockModularContent(
$partialContent = self::mockPartialContent(
"_component", [
"custom-element" => $html
]
);
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_COMPONENT);
$sut = new ComponentExpander($document, $modularContent);
$sut = new ComponentExpander($document, $partialContent);
$expandedElements = $sut->expand();
self::assertCount(1, $expandedElements);
self::assertSame("CUSTOM-ELEMENT", $expandedElements[0]->tagName);
self::assertSame($html, $expandedElements[0]->innerHTML);
}

public function testExpand_recursive():void {
$modularContent = self::mockModularContent(
$partialContent = self::mockPartialContent(
"_component", [
"todo-list" => DocumentTestFactory::HTML_TODO_COMPONENT_TODO_LIST,
"todo-list-item" => DocumentTestFactory::HTML_TODO_COMPONENT_TODO_LIST_ITEM,
]
);
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TODO_CUSTOM_ELEMENT);
$sut = new ComponentExpander($document, $modularContent);
$sut = new ComponentExpander($document, $partialContent);
$expandedElements = $sut->expand();
self::assertCount(2, $expandedElements);
self::assertSame("TODO-LIST", $expandedElements[0]->tagName);
self::assertSame("TODO-LIST-ITEM", $expandedElements[1]->tagName);
}

public function testExpand_empty():void {
$modularContent = self::mockModularContent(
$partialContent = self::mockPartialContent(
"_component", [
"empty-component" => "",
]
);
$document = DocumentTestFactory::createHTML("<!doctype html><html><body><empty-component /></body></html>");
$sut = new ComponentExpander($document, $modularContent);
$sut = new ComponentExpander($document, $partialContent);
$expandedElements = $sut->expand();
self::assertCount(1, $expandedElements);
self::assertEquals("<empty-component></empty-component>", $document->body->innerHTML);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace Gt\DomTemplate\Test;

use Gt\DomTemplate\ModularContent;
use Gt\DomTemplate\ModularContentDirectoryNotFoundException;
use Gt\DomTemplate\ModularContentFileNotFoundException;
use Gt\DomTemplate\PartialContent;
use Gt\DomTemplate\PartialContentDirectoryNotFoundException;
use Gt\DomTemplate\PartialContentFileNotFoundException;
use PHPUnit\Framework\TestCase;

class ModularContentTest extends TestCase {
class PartialContentTest extends TestCase {
private string $baseDir;

protected function setUp():void {
Expand All @@ -21,17 +21,17 @@ protected function tearDown():void {

public function testConstruct_throwsIfDirectoryNotExist():void {
$dir = $this->baseDir . "/" . uniqid("random-");
self::expectException(ModularContentDirectoryNotFoundException::class);
self::expectExceptionMessage("The modular content path does not exist: $dir");
new ModularContent($dir);
self::expectException(PartialContentDirectoryNotFoundException::class);
self::expectExceptionMessage("The partial content path does not exist: $dir");
new PartialContent($dir);
}

public function testGetContent_notExists():void {
$dir = $this->baseDir . "/" . uniqid("_partial");
mkdir($dir);
$sut = new ModularContent($dir);
self::expectException(ModularContentFileNotFoundException::class);
self::expectExceptionMessage("The modular content file does not exist: $dir/nothing.html");
$sut = new PartialContent($dir);
self::expectException(PartialContentFileNotFoundException::class);
self::expectExceptionMessage("The partial content file does not exist: $dir/nothing.html");
$sut->getContent("nothing");
}

Expand All @@ -40,7 +40,7 @@ public function testGetContent():void {
$dir = $this->baseDir . "/" . uniqid("_partial");
mkdir($dir);
file_put_contents("$dir/test.html", $expectedContent);
$sut = new ModularContent($dir);
$sut = new PartialContent($dir);
self::assertSame(
$expectedContent,
$sut->getContent("test")
Expand All @@ -52,7 +52,7 @@ public function testGetHTMLDocument():void {
$dir = $this->baseDir . "/" . uniqid("_partial");
mkdir($dir);
file_put_contents("$dir/test.html", $expectedContent);
$sut = new ModularContent($dir);
$sut = new PartialContent($dir);
$document = $sut->getHTMLDocument("test");
self::assertSame(
"Test file contents",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
namespace Gt\DomTemplate\Test;

use Gt\Dom\HTMLDocument;
use Gt\DomTemplate\ModularContent;
use Gt\DomTemplate\ModularContentFileNotFoundException;
use Gt\DomTemplate\PartialContent;
use Gt\DomTemplate\PartialContentFileNotFoundException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ModularContentTestCase extends TestCase {
class PartialContentTestCase extends TestCase {
/**
* @param array<string, string> $contentFiles Associative array where
* the key is the modular content's name, and the value is its content.
* the key is the partial content's name, and the value is its content.
*/
protected function mockModularContent(
protected function mockPartialContent(
string $dirName,
array $contentFiles = []
):MockObject|ModularContent {
$mock = self::createMock(ModularContent::class);
):MockObject|PartialContent {
$mock = self::createMock(PartialContent::class);
$mock->method("getContent")
->willReturnCallback(
function(string $name) use($contentFiles) {
$content = $contentFiles[$name] ?? null;
if(is_null($content)) {
throw new ModularContentFileNotFoundException();
throw new PartialContentFileNotFoundException();
}

return $content;
Expand All @@ -33,7 +33,7 @@ function(string $name) use($contentFiles) {
function(string $name) use($contentFiles) {
$content = $contentFiles[$name] ?? null;
if(is_null($content)) {
throw new ModularContentFileNotFoundException();
throw new PartialContentFileNotFoundException();
}

return new HTMLDocument($content);
Expand Down
32 changes: 16 additions & 16 deletions test/phpunit/PartialExpanderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
use Gt\Dom\Element;
use Gt\Dom\HTMLDocument;
use Gt\DomTemplate\CommentIni;
use Gt\DomTemplate\ModularContent;
use Gt\DomTemplate\ModularContentFileNotFoundException;
use Gt\DomTemplate\PartialContent;
use Gt\DomTemplate\PartialContentFileNotFoundException;
use Gt\DomTemplate\PartialExpander;
use Gt\DomTemplate\PartialInjectionMultiplePointException;
use Gt\DomTemplate\PartialInjectionPointNotFoundException;
use Gt\DomTemplate\Test\TestFactory\DocumentTestFactory;

class PartialExpanderTest extends ModularContentTestCase {
class PartialExpanderTest extends PartialContentTestCase {
public function testExpand_noMatchingPartial():void {
$modularContent = self::mockModularContent(
$partialContent = self::mockPartialContent(
"_partial", [
"nothing" => "this doesn't exist",
]
);
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW);
$sut = new PartialExpander(
$document,
$modularContent
$partialContent
);
self::expectException(ModularContentFileNotFoundException::class);
self::expectException(PartialContentFileNotFoundException::class);
self::assertEmpty($sut->expand());
}

public function testExpand():void {
$modularContent = self::mockModularContent(
$partialContent = self::mockPartialContent(
"_partial", [
"base-page" => DocumentTestFactory::HTML_PARTIAL_VIEW
]
Expand All @@ -40,7 +40,7 @@ public function testExpand():void {

$sut = new PartialExpander(
$document,
$modularContent
$partialContent
);
$expandedPartials = $sut->expand();
$mainElement = $document->querySelector("body>main");
Expand All @@ -61,21 +61,21 @@ public function testExpand():void {

public function testExpand_noExtendsSectionOfCommentIni():void {
$document = DocumentTestFactory::createHTML();
$modularContent = self::createMock(ModularContent::class);
$partialContent = self::createMock(PartialContent::class);

$sut = new PartialExpander($document, $modularContent);
$sut = new PartialExpander($document, $partialContent);
self::assertEmpty($sut->expand());
}

public function testExpand_recursive():void {
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW_RECURSIVE);
$modularContent = self::mockModularContent(
$partialContent = self::mockPartialContent(
"_partial", [
"extended-page" => DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW_RECURSIVE_BASE,
"partial-base" => DocumentTestFactory::HTML_PARTIAL_VIEW,
]
);
$sut = new PartialExpander($document, $modularContent);
$sut = new PartialExpander($document, $partialContent);
$sut->expand();
$body = $document->body;
$main = $body->querySelector("main");
Expand All @@ -95,26 +95,26 @@ public function testExpand_recursive():void {

public function testExpand_noDataPartialElement():void {
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW);
$modularContent = self::mockModularContent(
$partialContent = self::mockPartialContent(
"_partial", [
// Here, the HTML_COMPONENT isn't expected, because there is no data-partial element.
"base-page" => DocumentTestFactory::HTML_COMPONENT,
]
);
$sut = new PartialExpander($document, $modularContent);
$sut = new PartialExpander($document, $partialContent);
self::expectException(PartialInjectionPointNotFoundException::class);
self::expectExceptionMessage("The current view extends the partial \"base-page\", but there is no element marked with `data-partial`.");
$sut->expand();
}

public function testExpand_multipleDataPartialElements():void {
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW);
$modularContent = self::mockModularContent(
$partialContent = self::mockPartialContent(
"_partial", [
"base-page" => DocumentTestFactory::HTML_INCORRECT_PARTIAL_VIEW,
]
);
$sut = new PartialExpander($document, $modularContent);
$sut = new PartialExpander($document, $partialContent);
self::expectException(PartialInjectionMultiplePointException::class);
self::expectExceptionMessage("The current view extends the partial \"base-page\", but there is more than one element marked with `data-partial`.");
$sut->expand();
Expand Down
2 changes: 1 addition & 1 deletion test/phpunit/TestFactory/DocumentTestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ class DocumentTestFactory {
<h1>Component test</h1>
<p>This test shows how a custom element can be injected into the document.</p>
<custom-element />
<p>If there's matching modular content in the _component directory, the above element will be filled with its content.</p>
<p>If there's matching partial content in the _component directory, the above element will be filled with its content.</p>
HTML;

const HTML_TRANSPORT_ROUTES = <<<HTML
Expand Down

0 comments on commit 6099617

Please sign in to comment.