From 5307569bba25e1abb428c529c2e188a4f1344672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 14 Jul 2020 13:50:00 +0200 Subject: [PATCH] wrap up engine for setting properties on elements --- tests/Feature/PdfMaker/PdfMaker.php | 11 +++++++---- tests/Feature/PdfMaker/PdfMakerTest.php | 16 +++++++++++----- tests/Feature/PdfMaker/PdfMakerUtilities.php | 12 ++++++++---- tests/Feature/PdfMaker/business.html | 1 + 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/tests/Feature/PdfMaker/PdfMaker.php b/tests/Feature/PdfMaker/PdfMaker.php index 487be1ba1da4..cba480b4f179 100644 --- a/tests/Feature/PdfMaker/PdfMaker.php +++ b/tests/Feature/PdfMaker/PdfMaker.php @@ -32,11 +32,14 @@ class PdfMaker public function build() { - // $raw = $this->design->html(); - - $this->updateElementProperties($this->data['template']); - $this->updateVariables($this->data['variables']); + if (isset($this->data['template'])) { + $this->updateElementProperties($this->data['template']); + } + if (isset($this->data['variables'])) { + $this->updateVariables($this->data['variables']); + } + return $this; } diff --git a/tests/Feature/PdfMaker/PdfMakerTest.php b/tests/Feature/PdfMaker/PdfMakerTest.php index 97cf08795edf..6d4d53b92af6 100644 --- a/tests/Feature/PdfMaker/PdfMakerTest.php +++ b/tests/Feature/PdfMaker/PdfMakerTest.php @@ -120,12 +120,18 @@ class PdfMakerTest extends TestCase ['element' => 'thead', 'content' => '', 'elements' => [ ['element' => 'th', 'content' => 'Company',], ['element' => 'th', 'content' => 'Contact'], - ['element' => 'th', 'content' => 'Country'], + ['element' => 'th', 'content' => 'Country', 'properties' => [ + 'colspan' => 3, + ]], ]], ['element' => 'tr', 'content' => '', 'elements' => [ ['element' => 'td', 'content' => '$company'], ['element' => 'td', 'content' => '$email'], - ['element' => 'td', 'content' => '$country'], + ['element' => 'td', 'content' => '$country', 'elements' => [ + ['element' => 'a', 'content' => 'Click here for a link', 'properties' => [ + 'href' => 'https://github.com/invoiceninja/invoiceninja', + ]], + ]], ]], ], ], @@ -133,7 +139,7 @@ class PdfMakerTest extends TestCase 'variables' => [ '$company' => 'Invoice Ninja', '$email' => 'contact@invoiceninja.com', - '$country' => 'UK', + '$country' => 'UK', ], ]; @@ -143,8 +149,8 @@ class PdfMakerTest extends TestCase ->design(Business::class) ->build(); - info($maker->getCompiledHTML()); + $compiled = 'contact@invoiceninja.com'; - $this->assertTrue(true); + $this->assertStringContainsString($compiled, $maker->getCompiledHTML()); } } diff --git a/tests/Feature/PdfMaker/PdfMakerUtilities.php b/tests/Feature/PdfMaker/PdfMakerUtilities.php index d810185bf0b0..22ce8760ae5a 100644 --- a/tests/Feature/PdfMaker/PdfMakerUtilities.php +++ b/tests/Feature/PdfMaker/PdfMakerUtilities.php @@ -41,7 +41,7 @@ trait PdfMakerUtilities if (isset($element['properties'])) { foreach ($element['properties'] as $property => $value) { - $this->updateElementProperty($element['id'], $property, $value); + $this->updateElementProperty($node, $property, $value); } } @@ -51,10 +51,8 @@ trait PdfMakerUtilities } } - public function updateElementProperty(string $element, string $attribute, string $value) + public function updateElementProperty($element, string $attribute, string $value) { - $element = $this->document->getElementById($element); - $element->setAttribute($attribute, $value); if ($element->getAttribute($attribute) === $value) { @@ -69,6 +67,12 @@ trait PdfMakerUtilities foreach ($children as $child) { $_child = $this->document->createElement($child['element'], $child['content']); $element->appendChild($_child); + + if (isset($child['properties'])) { + foreach ($child['properties'] as $property => $value) { + $this->updateElementProperty($_child, $property, $value); + } + } if (isset($child['elements'])) { $this->createElementContent($_child, $child['elements']); diff --git a/tests/Feature/PdfMaker/business.html b/tests/Feature/PdfMaker/business.html index f7dd62b71001..0ecd32a30766 100644 --- a/tests/Feature/PdfMaker/business.html +++ b/tests/Feature/PdfMaker/business.html @@ -1,3 +1,4 @@ +
\ No newline at end of file