diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2909ead..1f888a0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,10 @@ jobs: path: /tmp/composer-cache key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }} - - uses: php-actions/composer@v6 + - name: Composer + uses: php-actions/composer@v6 + with: + php_version: '8.1' - name: Archive build run: mkdir /tmp/github-actions/ && tar -cvf /tmp/github-actions/build.tar ./ @@ -42,7 +45,7 @@ jobs: - name: PHP Unit tests uses: php-actions/phpunit@v3 with: - php_version: 8.0 + php_version: '8.1' php_extensions: xdebug configuration: test/phpunit/phpunit.xml bootstrap: vendor/autoload.php @@ -63,5 +66,7 @@ jobs: - name: PHP Static Analysis uses: php-actions/phpstan@v3 with: + php_version: '8.1' + memory_limit: '2G' path: src/ level: 6 diff --git a/composer.json b/composer.json index 37c40e8..65fd84f 100644 --- a/composer.json +++ b/composer.json @@ -2,15 +2,14 @@ "name": "phpgt/domtemplate", "description": "Bind dynamic data to reusable HTML components.", "license": "MIT", - "require": { - "php": ">=8.0", + "php": ">=8.1", "ext-dom": "*", - "phpgt/dom": "^3.0" + "phpgt/dom": "^v4.0.0" }, "require-dev": { - "phpunit/phpunit": "^v9.5", - "phpstan/phpstan": "^v1.4" + "phpstan/phpstan": "v1.8.0", + "phpunit/phpunit": "v9.5.21" }, "autoload": { diff --git a/composer.lock b/composer.lock index 9fc7c2a..7c2a90a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "290f9e38be3d7036f17e046e80146000", + "content-hash": "5f5f2ae66e96eed9fdf142cf445d1c50", "packages": [ { "name": "phpgt/cssxpath", @@ -62,30 +62,30 @@ }, { "name": "phpgt/dom", - "version": "v3.0.3", + "version": "v4.0.0", "source": { "type": "git", "url": "https://github.com/PhpGt/Dom.git", - "reference": "234c3f2b2b482c046d0317467d53d1d90960193d" + "reference": "e27378f0fefe1952353fcd082e4189243ebfcfee" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/PhpGt/Dom/zipball/234c3f2b2b482c046d0317467d53d1d90960193d", - "reference": "234c3f2b2b482c046d0317467d53d1d90960193d", + "url": "https://api.github.com/repos/PhpGt/Dom/zipball/e27378f0fefe1952353fcd082e4189243ebfcfee", + "reference": "e27378f0fefe1952353fcd082e4189243ebfcfee", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-mbstring": "*", - "php": ">=8.0", + "php": ">=8.1", "phpgt/cssxpath": "^1.1", "phpgt/propfunc": "^1.0", "psr/http-message": "^1.0" }, "require-dev": { - "phpstan/phpstan": "^1.4", - "phpunit/phpunit": "^9.5" + "phpstan/phpstan": "v1.8.0", + "phpunit/phpunit": "v9.5.21" }, "type": "library", "autoload": { @@ -148,7 +148,7 @@ "description": "The modern DOM API for PHP projects.", "support": { "issues": "https://github.com/PhpGt/Dom/issues", - "source": "https://github.com/PhpGt/Dom/tree/v3.0.3" + "source": "https://github.com/PhpGt/Dom/tree/v4.0.0" }, "funding": [ { @@ -156,7 +156,7 @@ "type": "github" } ], - "time": "2022-02-26T14:23:56+00:00" + "time": "2022-07-01T11:24:27+00:00" }, { "name": "phpgt/propfunc", @@ -790,16 +790,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.7.15", + "version": "1.8.0", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "cd0202ea1b1fc6d1bbe156c6e2e18a03e0ff160a" + "reference": "b7648d4ee9321665acaf112e49da9fd93df8fbd5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/cd0202ea1b1fc6d1bbe156c6e2e18a03e0ff160a", - "reference": "cd0202ea1b1fc6d1bbe156c6e2e18a03e0ff160a", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/b7648d4ee9321665acaf112e49da9fd93df8fbd5", + "reference": "b7648d4ee9321665acaf112e49da9fd93df8fbd5", "shasum": "" }, "require": { @@ -825,7 +825,7 @@ "description": "PHPStan - PHP Static Analysis Tool", "support": { "issues": "https://github.com/phpstan/phpstan/issues", - "source": "https://github.com/phpstan/phpstan/tree/1.7.15" + "source": "https://github.com/phpstan/phpstan/tree/1.8.0" }, "funding": [ { @@ -845,7 +845,7 @@ "type": "tidelift" } ], - "time": "2022-06-20T08:29:01+00:00" + "time": "2022-06-29T08:53:31+00:00" }, { "name": "phpunit/php-code-coverage", @@ -2346,7 +2346,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": ">=8.0", + "php": ">=8.1", "ext-dom": "*" }, "platform-dev": [], diff --git a/examples/bind/01-simple.php b/examples/bind/01-simple.php deleted file mode 100644 index a0cd4be..0000000 --- a/examples/bind/01-simple.php +++ /dev/null @@ -1,23 +0,0 @@ -Name -HTML; - -function example(DocumentBinder $binder):void { - $binder->bindValue("Cody"); -} - -// END OF EXAMPLE CODE. - -$document = new HTMLDocument($html); -$binder = new DocumentBinder($document); -example($binder); -$binder->cleanDatasets(); -echo $document; diff --git a/examples/bind/02-non-trivial.php b/examples/bind/02-non-trivial.php deleted file mode 100644 index 8bd25f3..0000000 --- a/examples/bind/02-non-trivial.php +++ /dev/null @@ -1,124 +0,0 @@ -Top three drivers of 0000 - - -HTML; - -function example(DocumentBinder $binder, DriverRepository $driverRepo):void { - $now = new DateTime(); - $currentYear = $now->format("Y"); - - // Here we're calling an example data source to get an array of Driver objects. - $drivers = $driverRepo->getDrivers( - year: $currentYear, - orderBy: "points", - limit: 3, - ); - - $binder->bindKeyValue("year", $currentYear); - $binder->bindList($drivers); -} - -// END OF EXAMPLE CODE. - -$document = new HTMLDocument($html); -$binder = new DocumentBinder($document); -$driverRepo = new DriverRepository(); -example($binder, $driverRepo); -$binder->cleanDatasets(); -echo $document; - -class DriverRepository { - /** @return array - * @noinspection PhpUnusedParameterInspection - */ - public function getDrivers( - ?int $year = null, - ?string $orderBy = "name", - ?int $limit = null - ):array { -// This is a fake data source. In the real world, this function's parameters -// would be used to query the database. For this example, we're just -// hard-coding the data. - return [ - new Driver("Lewis Hamilton", "Mercedes", 347, "United Kingdom (GBR)"), - new Driver("Valtteri Bottas", "Mercedes", 223, "Finland (FIN)"), - new Driver("Max Verstappen", "Red Bull Racing Honda", 214, "Netherlands (NED)"), - ]; - } -} - -/** - * This is an example of how a model object could look in your application. - * It can represent a row in a database, with some extra custom functionality. - * Note how the #[Bind] and #[BindGetter] attributes mark the functions as - * containing data to bind to the document. - */ -class Driver { - private string $name; - private string $team; - private int $points; - private string $countryDetail; - - public function __construct( - string $name, - string $team, - int $points, - string $countryDetail - ) { - $this->name = $name; - $this->team = $team; - $this->points = $points; - $this->countryDetail = $countryDetail; - } - - #[Bind("name")] - public function getFullName():string { - return $this->name; - } - - #[BindGetter] - public function getTeam():string { - return $this->team; - } - - #[BindGetter] - public function getPoints():int { - return $this->points; - } - - #[BindGetter] - public function getCountryName():string { - return trim( - strtok($this->countryDetail, "(") - ); - } - - #[BindGetter] - public function getCountryCode():string { - strtok($this->countryDetail, "("); - return trim(strtok("("), "()"); - } -} diff --git a/examples/bind/03-bindValue.php b/examples/bind/03-bindValue.php deleted file mode 100644 index 63571db..0000000 --- a/examples/bind/03-bindValue.php +++ /dev/null @@ -1,23 +0,0 @@ -This is a quick example

-HTML; - -function example(DocumentBinder $binder):void { - $binder->bindValue("This is an updated example"); -} - -// END OF EXAMPLE CODE. - -$document = new HTMLDocument($html); -$binder = new DocumentBinder($document); -example($binder); -$binder->cleanDatasets(); -echo $document; diff --git a/examples/bind/04-bindKeyValue.php b/examples/bind/04-bindKeyValue.php deleted file mode 100644 index 4b57ca4..0000000 --- a/examples/bind/04-bindKeyValue.php +++ /dev/null @@ -1,23 +0,0 @@ -Hello, you! -HTML; - -function example(DocumentBinder $binder):void { - $binder->bindKeyValue("name", "Cody"); -} - -// END OF EXAMPLE CODE. - -$document = new HTMLDocument($html); -$binder = new DocumentBinder($document); -example($binder); -$binder->cleanDatasets(); -echo $document; diff --git a/examples/bind/05-bindData.php b/examples/bind/05-bindData.php deleted file mode 100644 index 2581f14..0000000 --- a/examples/bind/05-bindData.php +++ /dev/null @@ -1,37 +0,0 @@ -User profile - -
-

Username

-

Full name: Full Name

-

Bio: Bio goes here

-
-HTML; - -function example(DocumentBinder $binder):void { - // In a real application, $data might be supplied from the database - // and could contain model objects rather than associative arrays. - $data = [ - "username" => "PhpNut", - "fullName" => "Larry E. Masters", - "bio" => "Christian - Dad - 4x Grandad - Co-Founder of @CakePHP - Developer - Open Source Advocate", - ]; - - $binder->bindData($data); -} - -// END OF EXAMPLE CODE. - -$document = new HTMLDocument($html); -$binder = new DocumentBinder($document); -example($binder); -$binder->cleanDatasets(); -echo $document; diff --git a/examples/bind/06-bindList.php b/examples/bind/06-bindList.php deleted file mode 100644 index 6133621..0000000 --- a/examples/bind/06-bindList.php +++ /dev/null @@ -1,33 +0,0 @@ -Shopping list - - -HTML; - -function example(DocumentBinder $binder):void { - $listData = [ - "Eggs", - "Potatoes", - "Butter", - "Plain flour", - ]; - $binder->bindList($listData); -} - -// END OF EXAMPLE CODE. - -$document = new HTMLDocument($html); -$binder = new DocumentBinder($document); -example($binder); -$binder->cleanDatasets(); -echo $document; diff --git a/examples/bind/07-bindListCallback.php b/examples/bind/07-bindListCallback.php deleted file mode 100644 index 7af7868..0000000 --- a/examples/bind/07-bindListCallback.php +++ /dev/null @@ -1,37 +0,0 @@ -Shopping list - - -HTML; - -function example(DocumentBinder $binder):void { - $listData = [ - "Eggs", - "Potatoes", - "Butter", - "Plain flour", - ]; - $binder->bindListCallback($listData, function(HTMLElement $element, $listItem, $listKey) { - $element->classList->add("item-$listKey"); - return "$listItem (item $listKey)"; - }); -} - -// END OF EXAMPLE CODE. - -$document = new HTMLDocument($html); -$binder = new DocumentBinder($document); -example($binder); -$binder->cleanDatasets(); -echo $document; diff --git a/examples/bind/08-bindTable.php b/examples/bind/08-bindTable.php deleted file mode 100644 index f4d6f9e..0000000 --- a/examples/bind/08-bindTable.php +++ /dev/null @@ -1,35 +0,0 @@ - - - - Day - Weather - - - -HTML; - -function example(DocumentBinder $binder):void { - $tableData = [ - "Day" => ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"], - "Weather" => ["Rain", "Cloud", "Cloud", "Sun", "Sun", "Cloud", "Cloud"], - ]; - - $binder->bindTable($tableData); -} - -// END OF EXAMPLE CODE. - -$document = new HTMLDocument($html); -$binder = new DocumentBinder($document); -example($binder); -$binder->cleanDatasets(); -echo $document; diff --git a/examples/binding/07-bindListCallback.php b/examples/binding/07-bindListCallback.php index 7af7868..d27de9d 100644 --- a/examples/binding/07-bindListCallback.php +++ b/examples/binding/07-bindListCallback.php @@ -1,6 +1,6 @@ bindListCallback($listData, function(HTMLElement $element, $listItem, $listKey) { + $binder->bindListCallback($listData, function(Element $element, $listItem, $listKey) { $element->classList->add("item-$listKey"); return "$listItem (item $listKey)"; }); diff --git a/src/Bind.php b/src/Bind.php index 4655a64..fee7f85 100644 --- a/src/Bind.php +++ b/src/Bind.php @@ -3,6 +3,7 @@ use Attribute; +/** @codeCoverageIgnore */ #[Attribute] class Bind { public function __construct( diff --git a/src/CommentIni.php b/src/CommentIni.php index a43a7db..d0a1c4d 100644 --- a/src/CommentIni.php +++ b/src/CommentIni.php @@ -14,12 +14,12 @@ class CommentIni { public function __construct( Document|Element $context ) { - if($context instanceof Element) { - $context = $context->ownerDocument; + if($context instanceof Document) { + $context = $context->documentElement; } - /** @var Document $context */ + /** @var Element $context */ - $walker = $context->createTreeWalker( + $walker = $context->ownerDocument->createTreeWalker( $context, NodeFilter::SHOW_COMMENT ); @@ -27,8 +27,12 @@ public function __construct( $ini = null; $commentNodeToRemove = null; - while($commentNode = $walker->nextNode()) { - /** @var Comment $commentNode */ + /** @var Element|Comment $commentNode */ + foreach($walker as $commentNode) { + if(!$commentNode instanceof Comment) { + continue; + } + $data = trim($commentNode->data); try { diff --git a/src/DocumentBinder.php b/src/DocumentBinder.php index a47af6e..7074093 100644 --- a/src/DocumentBinder.php +++ b/src/DocumentBinder.php @@ -131,10 +131,10 @@ public function bindListCallback( } public function cleanDatasets():void { - /** @var XPathResult $xpathResult */ $xpathResult = $this->document->evaluate( "//*/@*[starts-with(name(), 'data-bind')] | //*/@*[starts-with(name(), 'data-template')] | //*/@*[starts-with(name(), 'data-table-key')]" ); + /** @var Attr $item */ foreach($xpathResult as $item) { $item->ownerElement->removeAttribute($item->name); } diff --git a/src/HTMLAttributeBinder.php b/src/HTMLAttributeBinder.php index 2d2fe1d..113db0c 100644 --- a/src/HTMLAttributeBinder.php +++ b/src/HTMLAttributeBinder.php @@ -1,12 +1,12 @@ documentElement; } - foreach($element->attributes as $attrName => $attrValue) { + /** + * @var string $attrName + * @var Attr $attr + */ + foreach($element->attributes as $attrName => $attr) { + $attrValue = $attr->value; + if(!str_starts_with($attrName, "data-bind")) { continue; } - if(!strstr($attrName, ":")) { + if(!str_contains($attrName, ":")) { $tag = $this->getHTMLTag($element); throw new InvalidBindPropertyException("$tag Element has a data-bind attribute with missing bind property - did you mean `data-bind:text`?"); } @@ -43,7 +49,7 @@ public function bind( } } else { -// If there is a key specified, and the bind attribute's value doesn't match, +// If a key is specified, and the bind attribute's value doesn't match, // skip this attribute. $trimmedAttrValue = ltrim($attrValue, ":!?"); $trimmedAttrValue = strtok($trimmedAttrValue, " "); @@ -68,7 +74,13 @@ public function bind( } public function expandAttributes(Element $element):void { - foreach($element->attributes as $attrName => $attrValue) { + /** + * @var string $attrName + * @var Attr $attr + */ + foreach($element->attributes as $attrName => $attr) { + $attrValue = $attr->value; + if(!str_starts_with($attrName, "data-bind:")) { continue; } @@ -174,7 +186,7 @@ private function setBindProperty( break; case "value": - $element->value = $bindValue; /** @phpstan-ignore-line */ + $element->value = $bindValue; break; default: @@ -187,22 +199,7 @@ private function setBindProperty( ); } else { - if($element instanceof HTMLSelectElement - && $bindProperty === "value") { - /** @var HTMLOptionElement $option */ - foreach($element->options as $option) { - $optionValue = $option->value; - if($bindValue == $optionValue) { - $option->selected = true; - } - else { - $option->selected = false; - } - } - } - else { - $element->setAttribute($bindProperty, $bindValue); - } + $element->setAttribute($bindProperty, $bindValue); } break; diff --git a/src/NodePathCalculator.php b/src/NodePathCalculator.php index 4d74639..c0d456d 100644 --- a/src/NodePathCalculator.php +++ b/src/NodePathCalculator.php @@ -2,15 +2,12 @@ namespace Gt\DomTemplate; use Gt\Dom\Element; -use Gt\Dom\HTMLElement\HTMLElement; use Gt\Dom\Node; -use Gt\Dom\Facade\NodeClass\DOMElementFacade; -use ReflectionObject; use Stringable; class NodePathCalculator implements Stringable { public function __construct( - private Node $element + private Node|Element $element ) { } diff --git a/src/PartialContentExpander.php b/src/PartialContentExpander.php index 152159e..8e33ae6 100644 --- a/src/PartialContentExpander.php +++ b/src/PartialContentExpander.php @@ -1,11 +1,11 @@ title = $currentTitle; } - /** @var HTMLElement $importedRoot */ $importedRoot = $this->document->importNode( $partialDocument->documentElement, true diff --git a/src/PlaceholderBinder.php b/src/PlaceholderBinder.php index ef81e14..5a9065e 100644 --- a/src/PlaceholderBinder.php +++ b/src/PlaceholderBinder.php @@ -3,6 +3,7 @@ use Gt\Dom\Attr; use Gt\Dom\Document; +use Gt\Dom\Element; use Gt\Dom\Node; use Gt\Dom\Text; @@ -10,7 +11,7 @@ class PlaceholderBinder { public function bind( ?string $key, mixed $value, - Node|Document $context + Node|Element|Document $context ):void { if($context instanceof Document) { $context = $context->documentElement; diff --git a/src/TableBinder.php b/src/TableBinder.php index 101c119..bca02bd 100644 --- a/src/TableBinder.php +++ b/src/TableBinder.php @@ -3,6 +3,7 @@ use Gt\Dom\Document; use Gt\Dom\Element; +use Gt\Dom\ElementType; use Gt\Dom\HTMLElement\HTMLTableCellElement; use Gt\Dom\HTMLElement\HTMLTableElement; use Gt\Dom\HTMLElement\HTMLTableRowElement; @@ -35,7 +36,7 @@ public function bindTableData( $this->initBinders(); $tableArray = [$context]; - if(!$context instanceof HTMLTableElement) { + if($context->elementType !== ElementType::HTMLTableElement) { $tableArray = []; foreach($context->querySelectorAll("table") as $table) { array_push($tableArray, $table); @@ -47,7 +48,6 @@ public function bindTableData( } $headerRow = array_shift($tableData); - /** @var HTMLTableElement $table */ foreach($tableArray as $table) { $allowedHeaders = $headerRow; @@ -55,10 +55,8 @@ public function bindTableData( if($tHead) { $allowedHeaders = []; - /** @var HTMLTableRowElement $tHeadRow */ $tHeadRow = $tHead->rows[0]; foreach($tHeadRow->cells as $cell) { - /** @var HTMLTableCellElement $cell */ $headerKey = $cell->hasAttribute("data-table-key") ? $cell->getAttribute("data-table-key") : trim($cell->textContent); @@ -75,7 +73,6 @@ public function bindTableData( } } - /** @var ?HTMLTableSectionElement $tbody */ $tbody = $table->tBodies[0] ?? null; if(!$tbody) { $tbody = $table->createTBody(); @@ -87,7 +84,6 @@ public function bindTableData( foreach($tableData as $rowData) { try { $trTemplate = $templateCollection->get($tbody); - /** @var HTMLTableRowElement $tr */ $tr = $trTemplate->insertTemplate(); } catch(TemplateElementNotFoundInContextException) { diff --git a/src/TemplateElement.php b/src/TemplateElement.php index 9dcaaa2..ee4ed57 100644 --- a/src/TemplateElement.php +++ b/src/TemplateElement.php @@ -7,10 +7,10 @@ class TemplateElement { private string $templateParentPath; - private ?Element $templateNextSibling; + private null|Node|Element $templateNextSibling; public function __construct( - private Element $originalElement + private Node|Element $originalElement ) { $parentElement = $this->originalElement->parentElement; if(!$parentElement->id) { @@ -35,7 +35,7 @@ public function removeOriginalElement():void { $this->originalElement->remove(); } - public function getClone():Element { + public function getClone():Node|Element { /** @noinspection PhpUnnecessaryLocalVariableInspection */ /** @var Element $element */ $element = $this->originalElement->cloneNode(true); @@ -47,7 +47,7 @@ public function getClone():Element { * originally extracted from the document, returning the newly-inserted * clone. */ - public function insertTemplate():Element { + public function insertTemplate():Node|Element { $clone = $this->getClone(); $templateParent = $this->getTemplateParent(); $templateParent->insertBefore( @@ -58,7 +58,7 @@ public function insertTemplate():Element { return $clone; } - public function getTemplateParent():Element { + public function getTemplateParent():Node|Element { $matches = $this->originalElement->ownerDocument->evaluate( $this->templateParentPath ); @@ -71,7 +71,7 @@ public function getTemplateParent():Element { return $parent; } - public function getTemplateNextSibling():?Node { + public function getTemplateNextSibling():null|Node|Element { return $this->templateNextSibling ?? null; } diff --git a/test/phpunit/CommentIniTest.php b/test/phpunit/CommentIniTest.php index 26111d1..8999cc8 100644 --- a/test/phpunit/CommentIniTest.php +++ b/test/phpunit/CommentIniTest.php @@ -1,6 +1,7 @@ containsIniData()); } public function testConstruct_throwsIfCommentBlockNotFirst():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_INCORRECTLY_EXTENDS_PARTIAL_VIEW); + $document = new HTMLDocument(DocumentTestFactory::HTML_INCORRECTLY_EXTENDS_PARTIAL_VIEW); self::expectException(CommentIniInvalidDocumentLocationException::class); self::expectExceptionMessage("A Comment INI must only appear as the first node of the HTML."); new CommentIni($document); } public function testContainsIniData_emptyIfNoIniData():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_COMMENT_WITHOUT_INI_DATA_PARTIAL_VIEW); + $document = new HTMLDocument(DocumentTestFactory::HTML_COMMENT_WITHOUT_INI_DATA_PARTIAL_VIEW); $sut = new CommentIni($document); self::assertFalse($sut->containsIniData()); } public function testGet_noMatchingData():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); + $document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); $sut = new CommentIni($document); self::assertNull($sut->get("no-match")); } public function testGet():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); + $document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); $sut = new CommentIni($document); self::assertEquals("base-page", $sut->get("extends")); } public function testGetNested():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); + $document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); $sut = new CommentIni($document); self::assertEquals("My website, extended...", $sut->get("vars.title")); } diff --git a/test/phpunit/ComponentExpanderTest.php b/test/phpunit/ComponentExpanderTest.php index b75c969..3add0bb 100644 --- a/test/phpunit/ComponentExpanderTest.php +++ b/test/phpunit/ComponentExpanderTest.php @@ -1,17 +1,14 @@ expand()); } @@ -24,11 +21,11 @@ public function testExpand_returnsArrayOfExpandedElements():void { "custom-element" => $html ] ); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_COMPONENT); + $document = new HTMLDocument(DocumentTestFactory::HTML_COMPONENT); $sut = new ComponentExpander($document, $partialContent); $expandedElements = $sut->expand(); self::assertCount(1, $expandedElements); - self::assertSame("CUSTOM-ELEMENT", $expandedElements[0]->tagName); + self::assertSame("custom-element", $expandedElements[0]->tagName); self::assertSame($html, $expandedElements[0]->innerHTML); } @@ -39,12 +36,12 @@ public function testExpand_recursive():void { "todo-list-item" => DocumentTestFactory::HTML_TODO_COMPONENT_TODO_LIST_ITEM, ] ); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TODO_CUSTOM_ELEMENT); + $document = new HTMLDocument(DocumentTestFactory::HTML_TODO_CUSTOM_ELEMENT); $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); + self::assertSame("todo-list", $expandedElements[0]->tagName); + self::assertSame("todo-list-item", $expandedElements[1]->tagName); } public function testExpand_empty():void { @@ -53,7 +50,7 @@ public function testExpand_empty():void { "empty-component" => "", ] ); - $document = DocumentTestFactory::createHTML(""); + $document = new HTMLDocument(""); $sut = new ComponentExpander($document, $partialContent); $expandedElements = $sut->expand(); self::assertCount(1, $expandedElements); diff --git a/test/phpunit/DocumentBinderTest.php b/test/phpunit/DocumentBinderTest.php index c92b5de..139444f 100644 --- a/test/phpunit/DocumentBinderTest.php +++ b/test/phpunit/DocumentBinderTest.php @@ -1,24 +1,17 @@ - Element has a data-bind attribute with missing bind property - did you mean `data-bind:text`?"); @@ -38,7 +31,7 @@ public function testBindValue_missingBindProperty():void { } public function testBindValue_singleElement():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SINGLE_ELEMENT); + $document = new HTMLDocument(DocumentTestFactory::HTML_SINGLE_ELEMENT); $sut = new DocumentBinder($document); $output = $document->querySelector("output"); self::assertSame("Nothing is bound", $output->textContent); @@ -47,7 +40,7 @@ public function testBindValue_singleElement():void { } public function testBindValue_multipleElements():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_MULTIPLE_ELEMENTS); + $document = new HTMLDocument(DocumentTestFactory::HTML_MULTIPLE_ELEMENTS); $sut = new DocumentBinder($document); $output1 = $document->getElementById("o1"); $output2 = $document->getElementById("o2"); @@ -59,7 +52,7 @@ public function testBindValue_multipleElements():void { } public function testBindValue_multipleNestedElements():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_MULTIPLE_NESTED_ELEMENTS); + $document = new HTMLDocument(DocumentTestFactory::HTML_MULTIPLE_NESTED_ELEMENTS); $sut = new DocumentBinder($document); $container1 = $document->getElementById("container1"); $container2 = $document->getElementById("container2"); @@ -82,7 +75,7 @@ public function testBindValue_multipleNestedElements():void { } public function testBindValue_multipleNestedElements_skipsElementWithBindProperty():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_MULTIPLE_NESTED_ELEMENTS); + $document = new HTMLDocument(DocumentTestFactory::HTML_MULTIPLE_NESTED_ELEMENTS); $sut = new DocumentBinder($document); $container3 = $document->getElementById("container3"); $sut->bindValue("Test!", $container3); @@ -91,7 +84,7 @@ public function testBindValue_multipleNestedElements_skipsElementWithBindPropert } public function testBindValue_synonymousProperties():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SYNONYMOUS_BIND_PROPERTIES); + $document = new HTMLDocument(DocumentTestFactory::HTML_SYNONYMOUS_BIND_PROPERTIES); $sut = new DocumentBinder($document); $sut->bindValue("updated bold"); @@ -107,7 +100,7 @@ public function testBindValue_synonymousProperties():void { } public function testBindValue_null():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SINGLE_ELEMENT); + $document = new HTMLDocument(DocumentTestFactory::HTML_SINGLE_ELEMENT); $sut = new DocumentBinder($document); $exception = null; @@ -120,14 +113,14 @@ public function testBindValue_null():void { } public function testBindKeyValue_noMatches():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SINGLE_ELEMENT); + $document = new HTMLDocument(DocumentTestFactory::HTML_SINGLE_ELEMENT); $sut = new DocumentBinder($document); $sut->bindKeyValue("missing", "example"); self::assertSame("Nothing is bound", $document->querySelector("output")->innerHTML); } public function testBindKeyValue_noMatchesInDifferentHierarchy():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_MULTIPLE_NESTED_ELEMENTS); + $document = new HTMLDocument(DocumentTestFactory::HTML_MULTIPLE_NESTED_ELEMENTS); $sut = new DocumentBinder($document); // The "title" bind element is actually within the #c3 hierarchy so should not be bound. $sut->bindKeyValue("title", "This should not bind", $document->getElementById("container1")); @@ -135,7 +128,7 @@ public function testBindKeyValue_noMatchesInDifferentHierarchy():void { } public function testBindKeyValue():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_MULTIPLE_NESTED_ELEMENTS); + $document = new HTMLDocument(DocumentTestFactory::HTML_MULTIPLE_NESTED_ELEMENTS); $sut = new DocumentBinder($document); $sut->bindKeyValue("title", "This should bind"); self::assertSame("This should bind", $document->querySelector("#container3 h1")->textContent); @@ -143,7 +136,7 @@ public function testBindKeyValue():void { } public function testBindKeyValue_null():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_MULTIPLE_NESTED_ELEMENTS); + $document = new HTMLDocument(DocumentTestFactory::HTML_MULTIPLE_NESTED_ELEMENTS); $sut = new DocumentBinder($document); $exception = null; @@ -162,7 +155,7 @@ public function testBindData_assocArray():void { $email = uniqid() . "@example.com"; $category = uniqid("category-"); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_PROFILE); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_PROFILE); $sut = new DocumentBinder($document); $sut->bindData([ "username" => $username, @@ -180,7 +173,7 @@ public function testBindData_assocArray_withNull():void { $email = null; $category = uniqid("category-"); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_PROFILE); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_PROFILE); $sut = new DocumentBinder($document); $sut->bindData([ "username" => $username, @@ -194,7 +187,7 @@ public function testBindData_assocArray_withNull():void { } public function testBindData_indexedArray():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_PROFILE); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_PROFILE); $sut = new DocumentBinder($document); self::expectException(IncompatibleBindDataException::class); self::expectExceptionMessage("bindData is only compatible with key-value-pair data, but it was passed an indexed array."); @@ -207,7 +200,7 @@ public function testBindData_object():void { $userObject->email = "greg.bowler@g105b.com"; $userObject->category = "maintainer"; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_PROFILE); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_PROFILE); $sut = new DocumentBinder($document); $sut->bindData($userObject); @@ -222,7 +215,7 @@ public function testBindData_object_withNull():void { $userObject->email = "greg.bowler@g105b.com"; $userObject->category = null; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_PROFILE); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_PROFILE); $sut = new DocumentBinder($document); $sut->bindData($userObject); @@ -232,7 +225,7 @@ public function testBindData_object_withNull():void { } public function testBindData_indexArray_shouldThrowException():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_PROFILE); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_PROFILE); $sut = new DocumentBinder($document); self::expectException(IncompatibleBindDataException::class); self::expectExceptionMessage("bindData is only compatible with key-value-pair data, but it was passed an indexed array."); @@ -240,7 +233,7 @@ public function testBindData_indexArray_shouldThrowException():void { } public function testBindData_outOfContext():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_PROFILE); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_PROFILE); $sut = new DocumentBinder($document); $sut->bindData([ "username" => "will-not-bind", @@ -254,9 +247,8 @@ public function testBindData_outOfContext():void { } public function testBindKeyValue_arbitraryAttributes():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); + $document = new HTMLDocument(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); $sut = new DocumentBinder($document); - /** @var HTMLImageElement $img */ $img = $document->getElementById("img1"); $sut->bindKeyValue("photoURL", "/cat.jpg"); @@ -267,10 +259,9 @@ public function testBindKeyValue_arbitraryAttributes():void { } public function testBindKeyValue_classAttribute():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); + $document = new HTMLDocument(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); $sut = new DocumentBinder($document); - /** @var HTMLImageElement $img */ $img = $document->getElementById("img1"); self::assertSame("main", $img->className); @@ -281,10 +272,9 @@ public function testBindKeyValue_classAttribute():void { } public function testBindKeyValue_classToggle():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); + $document = new HTMLDocument(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); $sut = new DocumentBinder($document); - /** @var HTMLImageElement $img */ $img = $document->getElementById("img2"); self::assertSame("secondary", $img->className); @@ -295,10 +285,9 @@ public function testBindKeyValue_classToggle():void { } public function testBindKeyValue_classToggle_differentClassNameToBindKey():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); + $document = new HTMLDocument(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); $sut = new DocumentBinder($document); - /** @var HTMLImageElement $img */ $img = $document->getElementById("img3"); self::assertSame("secondary", $img->className); @@ -309,19 +298,18 @@ public function testBindKeyValue_classToggle_differentClassNameToBindKey():void } public function testBindKeyValue_toggleArbitraryAttribute():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); + $document = new HTMLDocument(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); $sut = new DocumentBinder($document); - /** @var HTMLParagraphElement $paragraph */ $paragraph = $document->getElementById("p1"); - self::assertSame("funny friendly", $paragraph->dataset->params); + self::assertSame("funny friendly", $paragraph->dataset->get("params")); $sut->bindKeyValue("isMagic", false, $paragraph); - self::assertSame("funny friendly", $paragraph->dataset->params); + self::assertSame("funny friendly", $paragraph->dataset->get("params")); $sut->bindKeyValue("isMagic", true, $paragraph); - self::assertSame("funny friendly magical", $paragraph->dataset->params); + self::assertSame("funny friendly magical", $paragraph->dataset->get("params")); $sut->bindKeyValue("isMagic", false, $paragraph); - self::assertSame("funny friendly", $paragraph->dataset->params); + self::assertSame("funny friendly", $paragraph->dataset->get("params")); } /** @@ -330,10 +318,9 @@ public function testBindKeyValue_toggleArbitraryAttribute():void { * bind attribute will be toggled depending on a bound boolean value. */ public function testBindKeyValue_toggleDisabled():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); + $document = new HTMLDocument(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); $sut = new DocumentBinder($document); - /** @var HTMLButtonElement $button */ $button = $document->getElementById("btn1"); self::assertFalse($button->disabled); @@ -352,10 +339,9 @@ public function testBindKeyValue_toggleDisabled():void { * HTML attribute). */ public function testBindKeyValue_toggleDisabled_inverseLogic():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); + $document = new HTMLDocument(DocumentTestFactory::HTML_DIFFERENT_BIND_PROPERTIES); $sut = new DocumentBinder($document); - /** @var HTMLButtonElement $button */ $button = $document->getElementById("btn2"); self::assertFalse($button->disabled); @@ -366,14 +352,14 @@ public function testBindKeyValue_toggleDisabled_inverseLogic():void { } public function testBindKeyValue_tableData_noTable():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_NO_TABLE); + $document = new HTMLDocument(DocumentTestFactory::HTML_NO_TABLE); $sut = new DocumentBinder($document); self::expectException(TableElementNotFoundInContextException::class); $sut->bindKeyValue("tableData", []); } public function testBindTable():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLES); $sut = new DocumentBinder($document); $tableData = [ @@ -385,12 +371,10 @@ public function testBindTable():void { ["Caroline Todd", "Surgical Registrar"], ]; - /** @var HTMLTableElement $table */ $table = $document->getElementById("tbl1"); $sut->bindTable($tableData, $table); foreach($tableData as $rowIndex => $rowData) { - /** @var HTMLTableRowElement $row */ $row = $table->rows[$rowIndex]; foreach($rowData as $cellIndex => $cellValue) { @@ -403,7 +387,7 @@ public function testBindTable():void { } public function testBindTable_withNullData():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLES); $sut = new DocumentBinder($document); $tableData = [ @@ -416,7 +400,6 @@ public function testBindTable_withNullData():void { ]; $exception = null; - /** @var HTMLTableElement $table */ $table = $document->getElementById("tbl1"); try { $sut->bindTable($tableData, $table); @@ -425,7 +408,6 @@ public function testBindTable_withNullData():void { self::assertNull($exception); foreach($tableData as $rowIndex => $rowData) { - /** @var HTMLTableRowElement $row */ $row = $table->rows[$rowIndex]; foreach($rowData as $cellIndex => $cellValue) { @@ -447,7 +429,7 @@ public function testBindTable_withNullData():void { } public function testBindKeyValue_tableData():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLES); $sut = new DocumentBinder($document); $tableData = [ @@ -459,12 +441,10 @@ public function testBindKeyValue_tableData():void { ["Caroline Todd", "Surgical Registrar"], ]; - /** @var HTMLTableElement $table */ $table = $document->getElementById("tbl1"); $sut->bindKeyValue("tableData", $tableData, $table); foreach($tableData as $rowIndex => $rowData) { - /** @var HTMLTableRowElement $row */ $row = $table->rows[$rowIndex]; foreach($rowData as $cellIndex => $cellValue) { @@ -477,7 +457,7 @@ public function testBindKeyValue_tableData():void { } public function testBindList():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_LIST_TEMPLATE); + $document = new HTMLDocument(DocumentTestFactory::HTML_LIST_TEMPLATE); $sut = new DocumentBinder($document); $listData = ["One", "Two", "Three"]; @@ -491,7 +471,7 @@ public function testBindList():void { } public function testBindList_nullData():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_LIST_TEMPLATE); + $document = new HTMLDocument(DocumentTestFactory::HTML_LIST_TEMPLATE); $sut = new DocumentBinder($document); $listData = ["One", null, "Three"]; @@ -510,7 +490,7 @@ public function testBindList_nullData():void { } public function testBindList_emptyLeavesNoWhiteSpace():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_LIST_TEMPLATE); + $document = new HTMLDocument(DocumentTestFactory::HTML_LIST_TEMPLATE); $sut = new DocumentBinder($document); $listData = []; $sut->bindList($listData); @@ -518,7 +498,7 @@ public function testBindList_emptyLeavesNoWhiteSpace():void { } public function testBindData_objectWithAttribute():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_PROFILE); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_PROFILE); $sut = new DocumentBinder($document); $userObject = new class { @@ -539,7 +519,7 @@ public function getEmailAddress():string { } public function testBindList_objectListWithAttributes():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_ORDER_LIST); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_ORDER_LIST); $sut = new DocumentBinder($document); $userObjectList = [ @@ -620,7 +600,7 @@ public function ordersCompleted():int { } public function testBindData_castToArray():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_PROFILE); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_PROFILE); $sut = new DocumentBinder($document); $row = new class { @@ -641,7 +621,7 @@ public function asArray():array { } public function testBindList_castToArray():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_ORDER_LIST); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_ORDER_LIST); $sut = new DocumentBinder($document); $row1 = new class { @@ -671,7 +651,7 @@ public function asArray():array { } public function testBindValue_callable():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SINGLE_ELEMENT); + $document = new HTMLDocument(DocumentTestFactory::HTML_SINGLE_ELEMENT); $sut = new DocumentBinder($document); $sut->bindValue(fn() => "test"); self::assertSame("test", $document->querySelector("output")->textContent); @@ -755,7 +735,7 @@ public function testBindList_complexHTML():void { ] ]; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TRANSPORT_ROUTES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TRANSPORT_ROUTES); $sut = new DocumentBinder($document); $sut->bindKeyValue("from", $from); $sut->bindKeyValue("to", $to); @@ -818,7 +798,7 @@ public function testBindListData_callback():void { return $listItem; }; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SALES); + $document = new HTMLDocument(DocumentTestFactory::HTML_SALES); $sut = new DocumentBinder($document); $sut->bindListCallback( $salesData, @@ -839,7 +819,7 @@ public function testBindListData_callback():void { } public function testCleanDatasets_dataBind():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_PROFILE); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_PROFILE); $sut = new DocumentBinder($document); $sut->bindData([ "username" => "codyboy123", @@ -859,7 +839,7 @@ public function testCleanDatasets_dataBind():void { } public function testCleanDatasets_dataTemplate():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_LIST_TEMPLATE); + $document = new HTMLDocument(DocumentTestFactory::HTML_LIST_TEMPLATE); $sut = new DocumentBinder($document); $sut->bindList(["One", "Two", "Three", "Four"]); $sut->cleanDatasets(); @@ -879,7 +859,7 @@ public function testCleanDatasets_dataTemplate():void { } public function testBindListData_twoListsDifferentContexts():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TWO_LISTS_WITH_UNNAMED_TEMPLATES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TWO_LISTS_WITH_UNNAMED_TEMPLATES); $sut = new DocumentBinder($document); $progLangData = ["PHP", "HTML", "bash"]; @@ -897,7 +877,7 @@ public function testBindListData_twoListsDifferentContexts():void { } public function testBindListData_twoListsDifferentContexts_withHtmlParents():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TWO_LISTS_WITH_UNNAMED_TEMPLATES_CLASS_PARENTS); + $document = new HTMLDocument(DocumentTestFactory::HTML_TWO_LISTS_WITH_UNNAMED_TEMPLATES_CLASS_PARENTS); $sut = new DocumentBinder($document); $progLangData = ["PHP", "HTML", "bash"]; @@ -915,7 +895,7 @@ public function testBindListData_twoListsDifferentContexts_withHtmlParents():voi } public function testBindValue_callableString():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SINGLE_ELEMENT); + $document = new HTMLDocument(DocumentTestFactory::HTML_SINGLE_ELEMENT); $sut = new DocumentBinder($document); $value = "explode"; $sut->bindValue($value); @@ -923,7 +903,7 @@ public function testBindValue_callableString():void { } public function testBindList_twoListsWithSamePath():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TEMPLATES_WITH_SAME_XPATH); + $document = new HTMLDocument(DocumentTestFactory::HTML_TEMPLATES_WITH_SAME_XPATH); $sut = new DocumentBinder($document); $list1 = [ ["uuid" => "AAAAAAAA", "fullName" => "Test 1"], @@ -936,9 +916,7 @@ public function testBindList_twoListsWithSamePath():void { ["uuid" => "FFFFFFFF", "fullName" => "Test 6"], ]; - /** @var HTMLSelectElement $select1 */ $select1 = $document->querySelector("[name='pass-on-to']"); - /** @var HTMLSelectElement $select2 */ $select2 = $document->querySelector("[name='tag-user']"); $sut->bindList($list1, $select1); diff --git a/test/phpunit/HTMLAttributeBinderTest.php b/test/phpunit/HTMLAttributeBinderTest.php index 580b40c..f8be650 100644 --- a/test/phpunit/HTMLAttributeBinderTest.php +++ b/test/phpunit/HTMLAttributeBinderTest.php @@ -1,28 +1,26 @@ bind("language", "en_GB", $document); self::assertSame("en_GB", $document->documentElement->getAttribute("lang")); } public function testBind_selectValue():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SELECT_OPTIONS_WITH_VALUE); + $document = new HTMLDocument(DocumentTestFactory::HTML_SELECT_OPTIONS_WITH_VALUE); $select = $document->querySelector("select[name='drink']"); $sut = new HTMLAttributeBinder(); $valueToSelect = "tea"; $sut->bind("drink", $valueToSelect, $select); - /** @var HTMLOptionElement $option */ foreach($document->querySelectorAll("select option") as $option) { $value = $option->getAttribute("value"); if($value === $valueToSelect) { @@ -35,13 +33,12 @@ public function testBind_selectValue():void { } public function testBind_selectValue_noOptions():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SELECT_OPTIONS_WITHOUT_VALUE); + $document = new HTMLDocument(DocumentTestFactory::HTML_SELECT_OPTIONS_WITHOUT_VALUE); $select = $document->querySelector("select[name='drink']"); $sut = new HTMLAttributeBinder(); $valueToSelect = "Tea"; $sut->bind("drink", $valueToSelect, $select); - /** @var HTMLOptionElement $option */ foreach($document->querySelectorAll("select option") as $option) { if($option->value === $valueToSelect) { self::assertTrue($option->hasAttribute("selected")); @@ -53,17 +50,15 @@ public function testBind_selectValue_noOptions():void { } public function testBind_selectValue_optionDoesNotExist():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SELECT_OPTIONS_WITHOUT_VALUE); - /** @var HTMLSelectElement $select */ + $document = new HTMLDocument(DocumentTestFactory::HTML_SELECT_OPTIONS_WITHOUT_VALUE); $select = $document->querySelector("select[name='drink']"); $sut = new HTMLAttributeBinder(); $valueToSelect = "Grape Juice"; $select->options[2]->selected = true; $sut->bind("drink", $valueToSelect, $select); - /** @var HTMLOptionElement $option */ - foreach($document->querySelectorAll("select option") as $option) { - self::assertFalse($option->hasAttribute("selected")); + foreach($document->querySelectorAll("select option") as $i => $option) { + self::assertFalse($option->hasAttribute("selected"), $i); } } } diff --git a/test/phpunit/ListBinderTest.php b/test/phpunit/ListBinderTest.php index 579a1de..a01c9c1 100644 --- a/test/phpunit/ListBinderTest.php +++ b/test/phpunit/ListBinderTest.php @@ -5,9 +5,8 @@ use DateInterval; use DateTime; use Gt\Dom\Element; -use Gt\Dom\HTMLElement\HTMLLiElement; +use Gt\Dom\HTMLDocument; use Gt\DomTemplate\Bind; -use Gt\DomTemplate\ElementBinder; use Gt\DomTemplate\ListBinder; use Gt\DomTemplate\TableElementNotFoundInContextException; use Gt\DomTemplate\TemplateCollection; @@ -18,7 +17,7 @@ class ListBinderTest extends TestCase { public function testBindList_emptyList():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_LIST_TEMPLATE); + $document = new HTMLDocument(DocumentTestFactory::HTML_LIST_TEMPLATE); $templateCollection = new TemplateCollection($document); $sut = new ListBinder($templateCollection); @@ -30,7 +29,7 @@ public function testBindList_emptyList():void { } public function testBindList_empty_shouldHaveNoWhitespace():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_LIST_TEMPLATE); + $document = new HTMLDocument(DocumentTestFactory::HTML_LIST_TEMPLATE); $templateCollection = new TemplateCollection($document); $sut = new ListBinder($templateCollection); $sut->bindListData([], $document); @@ -38,9 +37,15 @@ public function testBindList_empty_shouldHaveNoWhitespace():void { } public function testBindList_emptyList_iterator():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_LIST_TEMPLATE); + $document = new HTMLDocument(DocumentTestFactory::HTML_LIST_TEMPLATE); + $templateParent = $document->querySelector("ul"); + $templateElement = self::createMock(TemplateElement::class); + $templateElement->method("getTemplateParent") + ->willReturn($templateParent); $templateCollection = self::createMock(TemplateCollection::class); + $templateCollection->method("get") + ->willReturn($templateElement); $sut = new ListBinder($templateCollection); $boundCount = $sut->bindListData( @@ -51,7 +56,7 @@ public function testBindList_emptyList_iterator():void { } public function testBindList_noMatchingTemplate():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_LIST_TEMPLATE); + $document = new HTMLDocument(DocumentTestFactory::HTML_LIST_TEMPLATE); $templateCollection = self::createMock(TemplateCollection::class); $templateCollection->expects(self::once()) ->method("get") @@ -70,7 +75,7 @@ public function testBindList_noMatchingTemplate():void { } public function testBindList_simpleList():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_LIST_TEMPLATE); + $document = new HTMLDocument(DocumentTestFactory::HTML_LIST_TEMPLATE); $templateElement = new TemplateElement($document->querySelector("li[data-template]")); $templateCollection = self::createMock(TemplateCollection::class); @@ -113,7 +118,7 @@ public function testBindList_simpleList():void { * two template elements to have different template names. */ public function testBindListData_twoLists():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TWO_LISTS); + $document = new HTMLDocument(DocumentTestFactory::HTML_TWO_LISTS); $templateElementProgLang = new TemplateElement( $document->querySelector("#favourites li[data-template='prog-lang']") ); @@ -153,7 +158,7 @@ public function testBindListData_twoLists():void { * elements do not identify their own template name. */ public function testBindListData_twoListsDifferentContexts():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TWO_LISTS_WITH_UNNAMED_TEMPLATES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TWO_LISTS_WITH_UNNAMED_TEMPLATES); $templateElementProgLang = new TemplateElement( $document->querySelector("#prog-lang-list li[data-template]") ); @@ -188,7 +193,7 @@ public function testBindListData_twoListsDifferentContexts():void { } public function testBindListData_empty_parentShouldBeEmpty():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_LIST_TEMPLATE); + $document = new HTMLDocument(DocumentTestFactory::HTML_LIST_TEMPLATE); $templateElement = new TemplateElement($document->querySelector("li[data-template]")); $templateCollection = self::createMock(TemplateCollection::class); $templateCollection->method("get") @@ -207,7 +212,7 @@ public function testBindListData_kvpList_array():void { ["userId" => 559, "username" => "seafoam", "orderCount" => 30], ["userId" => 274, "username" => "hammatime", "orderCount" => 23], ]; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_ORDER_LIST); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_ORDER_LIST); $orderList = $document->querySelector("ul"); $templateElement = new TemplateElement($document->querySelector("ul li[data-template]")); @@ -233,7 +238,7 @@ public function testBindListData_kvpList_object():void { (object)["userId" => 559, "username" => "seafoam", "orderCount" => 30], (object)["userId" => 274, "username" => "hammatime", "orderCount" => 23], ]; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_ORDER_LIST); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_ORDER_LIST); $orderList = $document->querySelector("ul"); $templateElement = new TemplateElement($document->querySelector("ul li[data-template]")); @@ -259,7 +264,7 @@ public function testBindListData_kvpList_instanceObject():void { new class { public int $userId = 559; public string $username = "seafoam"; public int $orderCount = 30; }, new class { public int $userId = 274; public string $username = "hammatime"; public int $orderCount = 23; }, ]; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_ORDER_LIST); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_ORDER_LIST); $orderList = $document->querySelector("ul"); $templateElement = new TemplateElement($document->querySelector("ul li[data-template]")); @@ -328,7 +333,7 @@ public function getTotalOrders():int { } }, ]; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_ORDER_LIST); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_ORDER_LIST); $orderList = $document->querySelector("ul"); $templateElement = new TemplateElement($document->querySelector("ul li[data-template]")); @@ -384,7 +389,7 @@ public function testBindListData_kvpList_instanceObjectWithBindAttributeProperti public int $totalOrders = 23; }, ]; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_USER_ORDER_LIST); + $document = new HTMLDocument(DocumentTestFactory::HTML_USER_ORDER_LIST); $orderList = $document->querySelector("ul"); $templateElement = new TemplateElement($document->querySelector("ul li[data-template]")); @@ -405,7 +410,7 @@ public function testBindListData_kvpList_instanceObjectWithBindAttributeProperti } public function testBindListData_nestedList():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_MUSIC_NO_TEMPLATE_NAMES); + $document = new HTMLDocument(DocumentTestFactory::HTML_MUSIC_NO_TEMPLATE_NAMES); $templateCollection = new TemplateCollection($document); $sut = new ListBinder($templateCollection); $sut->bindListData(TestData::MUSIC, $document); @@ -439,7 +444,7 @@ public function testBindListData_nestedList():void { } public function testBindListData_nestedList_withKvps():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_STUDENT_LIST); + $document = new HTMLDocument(DocumentTestFactory::HTML_STUDENT_LIST); $templateCollection = new TemplateCollection($document); $sut = new ListBinder($templateCollection); $sut->bindListData(TestData::STUDENTS, $document); @@ -460,7 +465,7 @@ public function testBindListData_nestedList_withKvps():void { } public function testBindListData_iterativeSomething():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SEQUENCES); + $document = new HTMLDocument(DocumentTestFactory::HTML_SEQUENCES); $templateCollection = new TemplateCollection($document); $listData = [ "Primes" => new ArrayIterator([2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71]), @@ -480,7 +485,7 @@ public function testBindListData_iterativeSomething():void { } public function testBindListData_dateTime():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_DATES); + $document = new HTMLDocument(DocumentTestFactory::HTML_DATES); $templateCollection = new TemplateCollection($document); $listData = []; @@ -507,7 +512,7 @@ public function __toString():string { } public function testBindListData_todoList():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TODO); + $document = new HTMLDocument(DocumentTestFactory::HTML_TODO); $templateCollection = new TemplateCollection($document); $data = TestData::TODO_DATA; $sut = new ListBinder($templateCollection); @@ -526,7 +531,7 @@ public function testBindListData_todoList():void { } public function testBindListData_multipleTemplateSiblings():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_GOOD_BAD); + $document = new HTMLDocument(DocumentTestFactory::HTML_GOOD_BAD); $templateCollection = new TemplateCollection($document); $sut = new ListBinder($templateCollection); $sut->bindListData(["Good news 1", "Good news 2"], $document, "good"); @@ -572,7 +577,7 @@ public function testBindListData_callback():void { return $listItem; }; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SALES); + $document = new HTMLDocument(DocumentTestFactory::HTML_SALES); $templateCollection = new TemplateCollection($document); $sut = new ListBinder($templateCollection); $sut->bindListData( @@ -597,7 +602,7 @@ public function testBindListData_callback():void { public function testBindList_twoListsSeparatedByElement():void { $blueShades = ["Periwinkle", "Ultramarine", "Liberty", "Navy", "Blurple"]; $redShades = ["Brink pink", "Crimson", "Vermilion", "Scarlet"]; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TWO_SUB_LISTS_SEPARATED_BY_ELEMENT); + $document = new HTMLDocument(DocumentTestFactory::HTML_TWO_SUB_LISTS_SEPARATED_BY_ELEMENT); $templateCollection = new TemplateCollection($document); $sut = new ListBinder($templateCollection); $sut->bindListData($redShades, $document, "red"); diff --git a/test/phpunit/PartialExpanderTest.php b/test/phpunit/PartialExpanderTest.php index 47cf407..1286831 100644 --- a/test/phpunit/PartialExpanderTest.php +++ b/test/phpunit/PartialExpanderTest.php @@ -1,9 +1,7 @@ "this doesn't exist", ] ); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); + $document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); $sut = new PartialExpander( $document, $partialContent @@ -34,7 +32,7 @@ public function testExpand():void { "base-page" => DocumentTestFactory::HTML_PARTIAL_VIEW ] ); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); + $document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); $mainElement = $document->querySelector("body>main"); self::assertNull($mainElement); @@ -61,7 +59,7 @@ public function testExpand():void { } public function testExpand_noExtendsSectionOfCommentIni():void { - $document = DocumentTestFactory::createHTML(); + $document = new HTMLDocument(); $partialContent = self::createMock(PartialContent::class); $sut = new PartialExpander($document, $partialContent); @@ -69,7 +67,7 @@ public function testExpand_noExtendsSectionOfCommentIni():void { } public function testExpand_recursive():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW_RECURSIVE); + $document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW_RECURSIVE); $partialContent = self::mockPartialContent( "_partial", [ "extended-page" => DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW_RECURSIVE_BASE, @@ -95,7 +93,7 @@ public function testExpand_recursive():void { } public function testExpand_noDataPartialElement():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); + $document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); $partialContent = self::mockPartialContent( "_partial", [ // Here, the HTML_COMPONENT isn't expected, because there is no data-partial element. @@ -109,7 +107,7 @@ public function testExpand_noDataPartialElement():void { } public function testExpand_multipleDataPartialElements():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); + $document = new HTMLDocument(DocumentTestFactory::HTML_EXTENDS_PARTIAL_VIEW); $partialContent = self::mockPartialContent( "_partial", [ "base-page" => DocumentTestFactory::HTML_INCORRECT_PARTIAL_VIEW, diff --git a/test/phpunit/PlaceholderBinderTest.php b/test/phpunit/PlaceholderBinderTest.php index a7d9883..a6117a2 100644 --- a/test/phpunit/PlaceholderBinderTest.php +++ b/test/phpunit/PlaceholderBinderTest.php @@ -1,14 +1,14 @@ querySelector("#test2 .greeting"); $sut = new PlaceholderBinder(); // We can now bind text to the placeholder, and the text will @@ -19,7 +19,7 @@ public function testBind():void { } public function testBind_contextDoesNotLeak():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_PLACEHOLDER); + $document = new HTMLDocument(DocumentTestFactory::HTML_PLACEHOLDER); $greetingElement = $document->querySelector("#test2 .greeting"); $sut = new PlaceholderBinder(); $sut->bind("name", "Cody", $greetingElement); @@ -29,7 +29,7 @@ public function testBind_contextDoesNotLeak():void { } public function testBind_noContextBindsAll():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_PLACEHOLDER); + $document = new HTMLDocument(DocumentTestFactory::HTML_PLACEHOLDER); $sut = new PlaceholderBinder(); $sut->bind("name", "Cody", $document); self::assertStringContainsString("Cody", $document->querySelector("#test1 .greeting")->textContent); @@ -38,17 +38,16 @@ public function testBind_noContextBindsAll():void { } public function testBind_attribute():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_PLACEHOLDER); + $document = new HTMLDocument(DocumentTestFactory::HTML_PLACEHOLDER); $sut = new PlaceholderBinder(); $testElement = $document->getElementById("test3"); - /** @var HTMLAnchorElement $link */ $link = $testElement->querySelector("a"); $sut->bind("repoName", "domtemplate", $testElement); self::assertSame("https://www.php.gt/domtemplate", $link->href); } public function testBind_nullDefault():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_PLACEHOLDER); + $document = new HTMLDocument(DocumentTestFactory::HTML_PLACEHOLDER); $sut = new PlaceholderBinder(); $testElement = $document->getElementById("test2"); $greeting = $testElement->querySelector("p.greeting"); @@ -57,7 +56,7 @@ public function testBind_nullDefault():void { } public function testBind_emptyDefault():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_PLACEHOLDER); + $document = new HTMLDocument(DocumentTestFactory::HTML_PLACEHOLDER); $sut = new PlaceholderBinder(); $testElement = $document->getElementById("test2"); $greeting = $testElement->querySelector("p.greeting"); @@ -66,7 +65,7 @@ public function testBind_emptyDefault():void { } public function testBind_zeroNotDefault():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_PLACEHOLDER); + $document = new HTMLDocument(DocumentTestFactory::HTML_PLACEHOLDER); $sut = new PlaceholderBinder(); $testElement = $document->getElementById("test2"); $greeting = $testElement->querySelector("p.greeting"); @@ -75,10 +74,9 @@ public function testBind_zeroNotDefault():void { } public function testBind_multipleAttribute():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_PLACEHOLDER); + $document = new HTMLDocument(DocumentTestFactory::HTML_PLACEHOLDER); $sut = new PlaceholderBinder(); $testElement = $document->getElementById("test5"); - /** @var HTMLAnchorElement $link */ $link = $testElement->querySelector("a"); $sut->bind("org", "PhpGt", $link); $sut->bind("tierId", "47297", $link); diff --git a/test/phpunit/TableBinderTest.php b/test/phpunit/TableBinderTest.php index 6bc49bb..96c46de 100644 --- a/test/phpunit/TableBinderTest.php +++ b/test/phpunit/TableBinderTest.php @@ -1,11 +1,7 @@ getElementById("tbl1"); self::assertEmpty($table->innerHTML); @@ -35,7 +30,6 @@ public function testBindTable_emptyTable():void { self::assertSame("Column 2", $table->tHead->rows[0]->children[1]->textContent); self::assertSame("Column 3", $table->tHead->rows[0]->children[2]->textContent); - /** @var HTMLTableSectionElement $tBody */ $tBody = $table->tBodies[0]; self::assertCount(3, $tBody->children); @@ -50,9 +44,8 @@ public function testBindTable_emptyTable():void { */ public function testBindTable_existingTHead():void { $sut = new TableBinder(); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLES); - /** @var HTMLTableElement $table */ $table = $document->getElementById("tbl2"); $thead = $table->tHead; @@ -67,18 +60,13 @@ public function testBindTable_existingTHead():void { ]; $sut->bindTableData($tableData, $table); - /** @var HTMLTableSectionElement $tbody */ $tbody = $table->tBodies[0]; self::assertSame($originalTheadHTML, $thead->innerHTML); self::assertCount(count($tableData), $tbody->rows); - /** @var HTMLTableRowElement $row0 */ $row0 = $tbody->rows[0]; - /** @var HTMLTableRowElement $row1 */ $row1 = $tbody->rows[1]; - /** @var HTMLTableRowElement $row2 */ $row2 = $tbody->rows[2]; - /** @var HTMLTableRowElement $row3 */ $row3 = $tbody->rows[3]; self::assertCount(3, $row0->cells); self::assertCount(3, $row1->cells); @@ -101,9 +89,8 @@ public function testBindTable_existingTHead():void { public function testBindTable_dataNormalised():void { $sut = new TableBinder(); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLES); - /** @var HTMLTableElement $table */ $table = $document->getElementById("tbl2"); $tableData = [ @@ -118,15 +105,10 @@ public function testBindTable_dataNormalised():void { $table ); - /** @var HTMLTableSectionElement $tbody */ $tbody = $table->tBodies[0]; - /** @var HTMLTableRowElement $row0 */ $row0 = $tbody->rows[0]; - /** @var HTMLTableRowElement $row1 */ $row1 = $tbody->rows[1]; - /** @var HTMLTableRowElement $row2 */ $row2 = $tbody->rows[2]; - /** @var HTMLTableRowElement $row3 */ $row3 = $tbody->rows[3]; self::assertSame("Greg", $row0->cells[0]->textContent); @@ -152,9 +134,8 @@ public function testBindTable_dataNormalised():void { */ public function testBindTable_doubleHeader_shouldEmitTHElementsInRows():void { $sut = new TableBinder(); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLES); - /** @var HTMLTableElement $table */ $table = $document->getElementById("tbl1"); $tableData = [ @@ -170,47 +151,40 @@ public function testBindTable_doubleHeader_shouldEmitTHElementsInRows():void { $table ); - /** @var HTMLTableSectionElement $tbody */ $tbody = $table->tBodies[0]; - /** @var HTMLTableRowElement $row0 */ $row0 = $tbody->rows[0]; - /** @var HTMLTableRowElement $row1 */ $row1 = $tbody->rows[1]; - /** @var HTMLTableRowElement $row2 */ $row2 = $tbody->rows[2]; foreach($row0->cells as $i => $cell) { - /** @var HTMLTableCellElement $cell */ if($i === 0) { - self::assertSame("TH", $cell->tagName); + self::assertSame("th", $cell->tagName); self::assertSame("Washing machine", $cell->textContent); } else { - self::assertSame("TD", $cell->tagName); + self::assertSame("td", $cell->tagName); self::assertEquals($tableData[1]["Washing machine"][$i - 1], $cell->textContent); } } foreach($row1->cells as $i => $cell) { - /** @var HTMLTableCellElement $cell */ if($i === 0) { - self::assertSame("TH", $cell->tagName); + self::assertSame("th", $cell->tagName); self::assertSame("Television", $cell->textContent); } else { - self::assertSame("TD", $cell->tagName); + self::assertSame("td", $cell->tagName); self::assertEquals($tableData[1]["Television"][$i - 1], $cell->textContent); } } foreach($row2->cells as $i => $cell) { - /** @var HTMLTableCellElement $cell */ if($i === 0) { - self::assertSame("TH", $cell->tagName); + self::assertSame("th", $cell->tagName); self::assertSame("Laptop", $cell->textContent); } else { - self::assertSame("TD", $cell->tagName); + self::assertSame("td", $cell->tagName); self::assertEquals($tableData[1]["Laptop"][$i - 1], $cell->textContent); } } @@ -218,9 +192,8 @@ public function testBindTable_doubleHeader_shouldEmitTHElementsInRows():void { public function testBindTable_nonIterableValue():void { $sut = new TableBinder(); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLES); - /** @var HTMLTableElement $table */ $table = $document->getElementById("tbl1"); $tableData = [ @@ -239,9 +212,8 @@ public function testBindTable_nonIterableValue():void { public function testBindTable_doubleHeaderNonIterableValue():void { $sut = new TableBinder(); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLES); - /** @var HTMLTableElement $table */ $table = $document->getElementById("tbl1"); $tableData = [ @@ -260,9 +232,8 @@ public function testBindTable_doubleHeaderNonIterableValue():void { public function testBindTable_assocArrayWithoutIterableColumns():void { $sut = new TableBinder(); - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLES); - /** @var HTMLTableElement $table */ $table = $document->getElementById("tbl1"); $tableData = [ // This is emulating a common syntax mistake - the columns are not within an @@ -286,7 +257,7 @@ public function testBindTable_multipleTables():void { "email" => ["derek@php.net", "cmbecker69@php.net", "pollita@php.net"], ]; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLES); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLES); $sut->bindTableData( $tableData, $document->getElementById("multi-table-container") @@ -296,16 +267,13 @@ public function testBindTable_multipleTables():void { self::assertCount(3, $tableList); foreach($tableList as $table) { - /** @var HTMLTableElement $table */ if($table->parentElement->id === "s2") { continue; } - /** @var HTMLTableSectionElement $tbody */ $tbody = $table->tBodies[0]; $tableDataKeys = array_keys($tableData); foreach($tbody->rows as $rowIndex => $row) { - /** @var HTMLTableRowElement $row */ foreach($row->cells as $cellIndex => $cell) { $key = $tableDataKeys[$cellIndex]; self::assertEquals( @@ -327,8 +295,7 @@ public function testBindTable_keyNamesInTHead():void { "email" => ["derek@php.net", "cmbecker69@php.net", "pollita@php.net"], ]; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLES); - /** @var HTMLTableElement $table */ + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLES); $table = $document->getElementById("tbl3"); $sut->bindTableData( $tableData, @@ -337,7 +304,6 @@ public function testBindTable_keyNamesInTHead():void { $tableDataKeys = []; foreach($table->rows as $rowIndex => $row) { - /** @var $row HTMLTableRowElement */ if($rowIndex === 1) { self::assertEquals("Greg", $row->cells[0]->textContent); continue; @@ -369,7 +335,7 @@ public function testBindTableData_documentContext():void { "email" => ["derek@php.net", "cmbecker69@php.net", "pollita@php.net"], ]; - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLE_NO_BIND_KEY); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLE_NO_BIND_KEY); $sut->bindTableData($tableData, $document); self::assertCount(4, $document->querySelectorAll("table tr")); @@ -385,19 +351,15 @@ public function testBindTableData_emptyHeader():void { array_push($tableData, [$i, $name, md5($name)]); } - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLE_ID_NAME_CODE); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLE_ID_NAME_CODE); $sut->bindTableData($tableData, $document); - /** @var HTMLTableElement $table */ $table = $document->querySelector("table"); - /** @var HTMLTableRowElement $theadRow */ $theadRow = $table->tHead->rows[0]; self::assertCount(4, $theadRow->cells); self::assertSame("Delete", $theadRow->cells[3]->textContent); - /** @var HTMLTableSectionElement $tbody */ $tbody = $table->tBodies[0]; - /** @var HTMLTableRowElement $row */ foreach($tbody->rows as $rowIndex => $row) { foreach($row->cells as $cellIndex => $cell) { $expected = $tableData[$rowIndex + 1][$cellIndex] ?? ""; @@ -416,17 +378,15 @@ public function testBindTableData_existingBodyRow():void { array_push($tableData, [$i, md5($name), $name, $i % 3 === 0]); } - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TABLE_EXISTING_CELLS); + $document = new HTMLDocument(DocumentTestFactory::HTML_TABLE_EXISTING_CELLS); $sut = new TableBinder(); $sut->bindTableData($tableData, $document); - /** @var HTMLTableSectionElement $tbody */ $tbody = $document->querySelector("table tbody"); $headers = array_shift($tableData); - /** @var HTMLTableRowElement $tr */ foreach($tbody->rows as $rowIndex => $tr) { $rowData = array_combine($headers, $tableData[$rowIndex]); @@ -434,11 +394,9 @@ public function testBindTableData_existingBodyRow():void { self::assertSame((string)$rowData["name"], $tr->cells[2]->textContent); self::assertSame((string)$rowData["code"], $tr->cells[3]->textContent); - /** @var HTMLInputElement $input */ $input = $tr->cells[0]->querySelector("input"); self::assertSame((string)$rowData["id"], $input->value); - /** @var HTMLInputElement $input */ $input = $tr->cells[4]->querySelector("input"); self::assertSame((string)$rowData["id"], $input->value); diff --git a/test/phpunit/TemplateCollectionTest.php b/test/phpunit/TemplateCollectionTest.php index 8b66c79..f35d1dc 100644 --- a/test/phpunit/TemplateCollectionTest.php +++ b/test/phpunit/TemplateCollectionTest.php @@ -1,6 +1,7 @@ querySelector("ul"); $ol = $document->querySelector("ol"); self::assertCount(1, $ul->children); @@ -27,22 +28,22 @@ public function testGet_noName():void { self::assertCount(1, $ol->children); $templateElement = $sut->get($document); $inserted = $templateElement->insertTemplate(); - self::assertSame("LI", $inserted->tagName); + self::assertSame("li", $inserted->tagName); self::assertSame($ul, $templateElement->getTemplateParent()); self::assertSame($ul, $inserted->parentElement); } public function testGet_name_noMatch():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TWO_LISTS); + $document = new HTMLDocument(DocumentTestFactory::HTML_TWO_LISTS); $sut = new TemplateCollection($document); self::expectException(TemplateElementNotFoundInContextException::class); - self::expectExceptionMessage('Template element with name "unknown-list" can not be found within the context HTML element.'); + self::expectExceptionMessage('Template element with name "unknown-list" can not be found within the context html element.'); $sut->get($document, "unknown-list"); } public function testGet_name():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TWO_LISTS); + $document = new HTMLDocument(DocumentTestFactory::HTML_TWO_LISTS); $sut = new TemplateCollection($document); $templateElement = $sut->get($document, "prog-lang"); @@ -62,7 +63,7 @@ public function testGet_name():void { * ListBinderTest::testBindListData_nestedList() */ public function testBindListData_nestedList_manual():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_MUSIC_EXPLICIT_TEMPLATE_NAMES); + $document = new HTMLDocument(DocumentTestFactory::HTML_MUSIC_EXPLICIT_TEMPLATE_NAMES); $templateCollection = new TemplateCollection($document); $elementBinder = new ElementBinder(); diff --git a/test/phpunit/TemplateElementTest.php b/test/phpunit/TemplateElementTest.php index eb08e9d..dad7b35 100644 --- a/test/phpunit/TemplateElementTest.php +++ b/test/phpunit/TemplateElementTest.php @@ -1,6 +1,7 @@ createElement("div"); $originalElement->setAttribute("data-template", "/oh/dear/oh/dear"); $document->body->appendChild($originalElement); @@ -19,7 +20,7 @@ public function testGetTemplateName_forwardSlashStarter():void { } public function testNextElementSibling():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TEMPLATE_ELEMENT_WITH_MULTIPLE_DIVS); + $document = new HTMLDocument(DocumentTestFactory::HTML_TEMPLATE_ELEMENT_WITH_MULTIPLE_DIVS); $originalElement = $document->querySelector("[data-template]"); $originalElementNextElementSibling = $originalElement->nextElementSibling; @@ -29,7 +30,7 @@ public function testNextElementSibling():void { } public function testInsertTemplate():void { - $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_TEMPLATE_ELEMENT_WITH_MULTIPLE_DIVS); + $document = new HTMLDocument(DocumentTestFactory::HTML_TEMPLATE_ELEMENT_WITH_MULTIPLE_DIVS); $originalElement = $document->querySelector("[data-template]"); $originalElementNextElementSibling = $originalElement->nextElementSibling; diff --git a/test/phpunit/TestFactory/DocumentTestFactory.php b/test/phpunit/TestFactory/DocumentTestFactory.php index b1cca76..749d3fa 100644 --- a/test/phpunit/TestFactory/DocumentTestFactory.php +++ b/test/phpunit/TestFactory/DocumentTestFactory.php @@ -1,8 +1,6 @@