diff --git a/tests/Feature/PdfMaker/Business.php b/tests/Feature/PdfMaker/Business.php new file mode 100644 index 000000000000..6429bb9468f1 --- /dev/null +++ b/tests/Feature/PdfMaker/Business.php @@ -0,0 +1,13 @@ +data = $data; + } + + public function design(string $design) + { + $this->design = new $design(); + + $this->initializeDomDocument(); + + return $this; + } + + public function build() + { + $raw = $this->design->html(); + + foreach ($this->data['template'] as $element) { + foreach ($element['properties'] as $property => $value) { + $this->updateElementProperty($element['id'], $property, $value); + } + } + + foreach ($this->data['variables'] as $entry) { + $this->updateVariable($entry['id'], $entry['variable'], $entry['value']); + } + + return $this; + } + + public function getCompiledHTML() + { + return $this->document->saveHTML(); + } +} diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php new file mode 100644 index 000000000000..2ebb6f640d89 --- /dev/null +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -0,0 +1,106 @@ +design(Business::class); + + $this->assertInstanceOf(Business::class, $maker->design); + } + + public function testHtmlDesignLoadsCorrectly() + { + $maker = new PdfMaker(); + + $maker + ->design(Business::class) + ->build(); + + $this->assertStringContainsString('', $maker->html); + } + + public function testGetSectionUtility() + { + $maker = new PdfMaker(); + + $maker + ->design(Business::class) + ->build(); + + $this->assertEquals('table', $maker->getSectionNode('product-table')->nodeName); + } + + public function testTableAttributesAreInjected() + { + $state = [ + 'template' => [ + 'product-table' => [ + 'id' => 'product-table', + 'properties' => [ + 'class' => 'my-awesome-class', + 'style' => 'margin-top: 10px;', + 'script' => 'console.log(1)', + ], + ], + 'header' => [ + 'id' => 'header', + 'properties' => [ + 'class' => 'header-class', + ], + ], + ], + 'variables' => [], + ]; + + $maker = new PdfMaker($state); + + $maker + ->design(Business::class) + ->build(); + + $this->assertStringContainsString('my-awesome-class', $maker->getSection('product-table', 'class')); + $this->assertStringContainsString('margin-top: 10px;', $maker->getSection('product-table', 'style')); + $this->assertStringContainsString('console.log(1)', $maker->getSection('product-table', 'script')); + } + + public function testVariablesAreReplaced() + { + $state = [ + 'template' => [ + 'product-table' => [ + 'id' => 'product-table', + 'properties' => [ + 'class' => 'my-awesome-class', + 'style' => 'margin-top: 10px;', + 'script' => 'console.log(1)', + ], + ], + 'header' => [ + 'id' => 'header', + 'properties' => [ + 'class' => 'header-class', + ], + ], + ], + 'variables' => [ + ['id' => 'header', 'variable' => '$title', 'value' => 'Invoice Ninja'], + ], + ]; + + $maker = new PdfMaker($state); + + $maker + ->design(Business::class) + ->build(); + + $this->assertStringContainsString('Invoice Ninja', $maker->getCompiledHTML()); + $this->assertStringContainsString('Invoice Ninja', $maker->getSection('header')); + } +} diff --git a/tests/Feature/PdfMaker/PdfMakerUtilities.php b/tests/Feature/PdfMaker/PdfMakerUtilities.php new file mode 100644 index 000000000000..4931072f5178 --- /dev/null +++ b/tests/Feature/PdfMaker/PdfMakerUtilities.php @@ -0,0 +1,70 @@ +validateOnParse = true; + @$document->loadHTML($this->design->html()); + + $this->document = $document; + $this->xpath = new DOMXPath($document); + } + + public function getSection(string $selector, string $section = null) + { + $element = $this->document->getElementById($selector); + + if ($section) { + return $element->getAttribute($section); + } + + return $element->nodeValue; + } + + public function getSectionNode(string $selector) + { + return $this->document->getElementById($selector); + } + + public function updateElementProperty(string $element, string $attribute, string $value) + { + $element = $this->document->getElementById($element); + + $element->setAttribute($attribute, $value); + + if ($element->getAttribute($attribute) === $value) { + return $element; + } + + info('Setting element property failed.'); + + return $element; + } + + public function updateVariable(string $element, string $variable, string $value) + { + $element = $this->document->getElementById($element); + + $original = $element->nodeValue; + + info([$variable => $value]); + + $element->nodeValue = ''; + + $replaced = strtr($original, [$variable => $value]); + + $element->appendChild( + $this->document->createTextNode($replaced) + ); + + return $element; + } +} \ No newline at end of file diff --git a/tests/Feature/PdfMaker/business.html b/tests/Feature/PdfMaker/business.html new file mode 100644 index 000000000000..f7dd62b71001 --- /dev/null +++ b/tests/Feature/PdfMaker/business.html @@ -0,0 +1,3 @@ + +