mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
(preview) New table/pdf generation system
This commit is contained in:
parent
6472907251
commit
94976054dd
13
tests/Feature/PdfMaker/Business.php
Normal file
13
tests/Feature/PdfMaker/Business.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\PdfMaker;
|
||||
|
||||
class Business
|
||||
{
|
||||
public function html()
|
||||
{
|
||||
return file_get_contents(
|
||||
base_path('tests/Feature/PdfMaker/business.html')
|
||||
);
|
||||
}
|
||||
}
|
56
tests/Feature/PdfMaker/PdfMaker.php
Normal file
56
tests/Feature/PdfMaker/PdfMaker.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\PdfMaker;
|
||||
|
||||
use DOMDocument;
|
||||
|
||||
class PdfMaker
|
||||
{
|
||||
use PdfMakerUtilities;
|
||||
|
||||
protected $data;
|
||||
|
||||
public $design;
|
||||
|
||||
public $html;
|
||||
|
||||
public $document;
|
||||
|
||||
private $xpath;
|
||||
|
||||
public function __construct(array $data = [])
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
106
tests/Feature/PdfMaker/PdfMakerTest.php
Normal file
106
tests/Feature/PdfMaker/PdfMakerTest.php
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\PdfMaker;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class PdfMakerTest extends TestCase
|
||||
{
|
||||
public function testDesignLoadsCorrectly()
|
||||
{
|
||||
$maker = new PdfMaker();
|
||||
|
||||
$maker->design(Business::class);
|
||||
|
||||
$this->assertInstanceOf(Business::class, $maker->design);
|
||||
}
|
||||
|
||||
public function testHtmlDesignLoadsCorrectly()
|
||||
{
|
||||
$maker = new PdfMaker();
|
||||
|
||||
$maker
|
||||
->design(Business::class)
|
||||
->build();
|
||||
|
||||
$this->assertStringContainsString('<!-- Business -->', $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'));
|
||||
}
|
||||
}
|
70
tests/Feature/PdfMaker/PdfMakerUtilities.php
Normal file
70
tests/Feature/PdfMaker/PdfMakerUtilities.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\PdfMaker;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMXPath;
|
||||
|
||||
trait PdfMakerUtilities
|
||||
{
|
||||
private function initializeDomDocument()
|
||||
{
|
||||
$document = new DOMDocument();
|
||||
|
||||
$document->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;
|
||||
}
|
||||
}
|
3
tests/Feature/PdfMaker/business.html
Normal file
3
tests/Feature/PdfMaker/business.html
Normal file
@ -0,0 +1,3 @@
|
||||
<!-- Business -->
|
||||
<div id="header">This is $title</div>
|
||||
<table id="product-table"></table>
|
Loading…
x
Reference in New Issue
Block a user