diff --git a/test/phpunit/DocumentBinderTest.php b/test/phpunit/DocumentBinderTest.php index 50075be..776ce74 100644 --- a/test/phpunit/DocumentBinderTest.php +++ b/test/phpunit/DocumentBinderTest.php @@ -663,4 +663,47 @@ public function testBindList_complexHTML():void { } } } + + public function testBindListData_callback():void { + $salesData = [ + [ + "name" => "Cactus", + "count" => 14, + "price" => 5.50, + "cost" => 3.55, + ], + [ + "name" => "Succulent", + "count" => 9, + "price" => 3.50, + "cost" => 2.10, + ] + ]; + $salesCallback = function(Element $template, array $listItem, string $key):array { + $totalPrice = $listItem["price"] * $listItem["count"]; + $totalCost = $listItem["cost"] * $listItem["count"]; + + $listItem["profit"] = round($totalPrice - $totalCost, 2); + return $listItem; + }; + + $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SALES); + $sut = new DocumentBinder($document); + $sut->bindListCallback( + $salesData, + $salesCallback + ); + + $salesLiList = $document->querySelectorAll("ul>li"); + self::assertCount(count($salesData), $salesLiList); + foreach($salesData as $i => $sale) { + $li = $salesLiList[$i]; + $profitValue = round(($sale["count"] * $sale["price"]) - ($sale["count"] * $sale["cost"]), 2); + self::assertEquals($sale["name"], $li->querySelector(".name span")->textContent); + self::assertEquals($sale["count"], $li->querySelector(".count span")->textContent); + self::assertEquals($sale["price"], $li->querySelector(".price span")->textContent); + self::assertEquals($sale["cost"], $li->querySelector(".cost span")->textContent); + self::assertEquals($profitValue, $li->querySelector(".profit span")->textContent); + } + } } diff --git a/test/phpunit/ListBinderTest.php b/test/phpunit/ListBinderTest.php index fa7cc3d..46c5f2b 100644 --- a/test/phpunit/ListBinderTest.php +++ b/test/phpunit/ListBinderTest.php @@ -498,4 +498,49 @@ public function testBindListData_multipleTemplateSiblings():void { self::assertEquals($expected[$i], $li->querySelector("span")->textContent); } } + + public function testBindListData_callback():void { + $salesData = [ + [ + "name" => "Cactus", + "count" => 14, + "price" => 5.50, + "cost" => 3.55, + ], + [ + "name" => "Succulent", + "count" => 9, + "price" => 3.50, + "cost" => 2.10, + ] + ]; + $salesCallback = function(Element $template, array $listItem, string $key):array { + $totalPrice = $listItem["price"] * $listItem["count"]; + $totalCost = $listItem["cost"] * $listItem["count"]; + + $listItem["profit"] = round($totalPrice - $totalCost, 2); + return $listItem; + }; + + $document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SALES); + $templateCollection = new TemplateCollection($document); + $sut = new ListBinder($templateCollection); + $sut->bindListData( + $salesData, + $document, + callback: $salesCallback + ); + + $salesLiList = $document->querySelectorAll("ul>li"); + self::assertCount(count($salesData), $salesLiList); + foreach($salesData as $i => $sale) { + $li = $salesLiList[$i]; + $profitValue = round(($sale["count"] * $sale["price"]) - ($sale["count"] * $sale["cost"]), 2); + self::assertEquals($sale["name"], $li->querySelector(".name span")->textContent); + self::assertEquals($sale["count"], $li->querySelector(".count span")->textContent); + self::assertEquals($sale["price"], $li->querySelector(".price span")->textContent); + self::assertEquals($sale["cost"], $li->querySelector(".cost span")->textContent); + self::assertEquals($profitValue, $li->querySelector(".profit span")->textContent); + } + } } diff --git a/test/phpunit/TestFactory/DocumentTestFactory.php b/test/phpunit/TestFactory/DocumentTestFactory.php index dc1cb5f..9ffe611 100644 --- a/test/phpunit/TestFactory/DocumentTestFactory.php +++ b/test/phpunit/TestFactory/DocumentTestFactory.php @@ -520,6 +520,19 @@ class DocumentTestFactory { HTML; + const HTML_SALES = << +

Sales

+ +HTML;