2020-07-09 16:05:17 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Feature\PdfMaker;
|
|
|
|
|
2020-07-14 15:13:46 +02:00
|
|
|
use Spatie\Browsershot\Browsershot;
|
2020-07-09 16:05:17 +02:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class PdfMakerTest extends TestCase
|
|
|
|
{
|
2020-07-13 13:51:54 +02:00
|
|
|
public $state = [
|
|
|
|
'template' => [],
|
|
|
|
'variables' => [],
|
|
|
|
];
|
|
|
|
|
2020-07-09 16:05:17 +02:00
|
|
|
public function testDesignLoadsCorrectly()
|
|
|
|
{
|
2020-07-13 13:51:54 +02:00
|
|
|
$maker = new PdfMaker($this->state);
|
2020-07-09 16:05:17 +02:00
|
|
|
|
|
|
|
$maker->design(Business::class);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Business::class, $maker->design);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHtmlDesignLoadsCorrectly()
|
|
|
|
{
|
2020-07-13 13:51:54 +02:00
|
|
|
$maker = new PdfMaker($this->state);
|
2020-07-09 16:05:17 +02:00
|
|
|
|
|
|
|
$maker
|
|
|
|
->design(Business::class)
|
|
|
|
->build();
|
|
|
|
|
2020-07-13 13:51:54 +02:00
|
|
|
$this->assertStringContainsString('<!-- Business -->', $maker->getCompiledHTML());
|
2020-07-09 16:05:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSectionUtility()
|
|
|
|
{
|
2020-07-13 13:51:54 +02:00
|
|
|
$maker = new PdfMaker($this->state);
|
2020-07-09 16:05:17 +02:00
|
|
|
|
|
|
|
$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' => [
|
2020-07-13 14:16:18 +02:00
|
|
|
'$title' => 'Invoice Ninja',
|
2020-07-09 16:05:17 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$maker = new PdfMaker($state);
|
|
|
|
|
|
|
|
$maker
|
|
|
|
->design(Business::class)
|
|
|
|
->build();
|
|
|
|
|
|
|
|
$this->assertStringContainsString('Invoice Ninja', $maker->getCompiledHTML());
|
|
|
|
$this->assertStringContainsString('Invoice Ninja', $maker->getSection('header'));
|
|
|
|
}
|
2020-07-13 17:49:28 +02:00
|
|
|
|
|
|
|
public function testElementContentIsGenerated()
|
|
|
|
{
|
|
|
|
$state = [
|
|
|
|
'template' => [
|
|
|
|
'product-table' => [
|
|
|
|
'id' => 'product-table',
|
|
|
|
'properties' => [],
|
|
|
|
'elements' => [
|
|
|
|
['element' => 'thead', 'content' => '', 'elements' => [
|
|
|
|
['element' => 'th', 'content' => 'Company',],
|
|
|
|
['element' => 'th', 'content' => 'Contact'],
|
2020-07-14 13:50:00 +02:00
|
|
|
['element' => 'th', 'content' => 'Country', 'properties' => [
|
|
|
|
'colspan' => 3,
|
|
|
|
]],
|
2020-07-13 17:49:28 +02:00
|
|
|
]],
|
|
|
|
['element' => 'tr', 'content' => '', 'elements' => [
|
|
|
|
['element' => 'td', 'content' => '$company'],
|
|
|
|
['element' => 'td', 'content' => '$email'],
|
2020-07-14 13:50:00 +02:00
|
|
|
['element' => 'td', 'content' => '$country', 'elements' => [
|
|
|
|
['element' => 'a', 'content' => 'Click here for a link', 'properties' => [
|
|
|
|
'href' => 'https://github.com/invoiceninja/invoiceninja',
|
|
|
|
]],
|
|
|
|
]],
|
2020-07-13 17:49:28 +02:00
|
|
|
]],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'variables' => [
|
|
|
|
'$company' => 'Invoice Ninja',
|
|
|
|
'$email' => 'contact@invoiceninja.com',
|
2020-07-14 13:50:00 +02:00
|
|
|
'$country' => 'UK',
|
2020-07-13 17:49:28 +02:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$maker = new PdfMaker($state);
|
|
|
|
|
|
|
|
$maker
|
|
|
|
->design(Business::class)
|
|
|
|
->build();
|
|
|
|
|
2020-07-14 13:50:00 +02:00
|
|
|
$compiled = 'contact@invoiceninja.com';
|
2020-07-13 17:49:28 +02:00
|
|
|
|
2020-07-14 13:50:00 +02:00
|
|
|
$this->assertStringContainsString($compiled, $maker->getCompiledHTML());
|
2020-07-13 17:49:28 +02:00
|
|
|
}
|
2020-07-14 14:02:02 +02:00
|
|
|
|
|
|
|
public function testConditionalRenderingOfElements()
|
|
|
|
{
|
|
|
|
$maker1 = new PdfMaker([
|
|
|
|
'template' => [
|
|
|
|
'header' => [
|
|
|
|
'id' => 'header',
|
|
|
|
'properties' => [],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$maker1
|
|
|
|
->design(Business::class)
|
|
|
|
->build();
|
|
|
|
|
|
|
|
$output1 = $maker1->getCompiledHTML();
|
|
|
|
|
2020-07-14 15:13:46 +02:00
|
|
|
$this->assertStringContainsString('<div id="header">$title</div>', $output1);
|
2020-07-14 14:02:02 +02:00
|
|
|
|
|
|
|
$maker2 = new PdfMaker([
|
|
|
|
'template' => [
|
|
|
|
'header' => [
|
|
|
|
'id' => 'header',
|
|
|
|
'properties' => ['hidden' => "true"],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$maker2
|
|
|
|
->design(Business::class)
|
|
|
|
->build();
|
|
|
|
|
|
|
|
$output2 = $maker2->getCompiledHTML();
|
|
|
|
|
2020-07-14 15:13:46 +02:00
|
|
|
$this->assertStringContainsString('<div id="header" hidden="true">$title</div>', $output2);
|
2020-07-14 14:02:02 +02:00
|
|
|
|
|
|
|
$this->assertNotSame($output1, $output2);
|
|
|
|
}
|
2020-07-14 14:35:27 +02:00
|
|
|
|
|
|
|
public function testOrderingElements()
|
|
|
|
{
|
|
|
|
$maker = new PdfMaker([
|
|
|
|
'template' => [
|
|
|
|
'header' => [
|
|
|
|
'id' => 'header',
|
|
|
|
'properties' => [],
|
|
|
|
'elements' => [
|
|
|
|
['element' => 'h1', 'content' => 'h1-element'],
|
|
|
|
['element' => 'span', 'content' => 'span-element'],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$maker
|
|
|
|
->design(Business::class)
|
|
|
|
->build();
|
|
|
|
|
|
|
|
$node = $maker->getSectionNode('header');
|
|
|
|
|
|
|
|
$before = [];
|
|
|
|
|
|
|
|
foreach ($node->childNodes as $child) {
|
|
|
|
$before[] = $child->nodeName;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals('h1', $before[1]);
|
|
|
|
|
|
|
|
$maker = new PdfMaker([
|
|
|
|
'template' => [
|
|
|
|
'header' => [
|
|
|
|
'id' => 'header',
|
|
|
|
'properties' => [],
|
|
|
|
'elements' => [
|
|
|
|
['element' => 'h1', 'content' => 'h1-element', 'order' => 1],
|
|
|
|
['element' => 'span', 'content' => 'span-element', 'order' => 0],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$maker
|
|
|
|
->design(Business::class)
|
|
|
|
->build();
|
|
|
|
|
|
|
|
$node = $maker->getSectionNode('header');
|
|
|
|
|
|
|
|
$after = [];
|
|
|
|
|
|
|
|
foreach ($node->childNodes as $child) {
|
|
|
|
$after[] = $child->nodeName;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals('span', $after[1]);
|
|
|
|
}
|
2020-07-14 15:13:46 +02:00
|
|
|
|
|
|
|
public function testGeneratingPdf()
|
|
|
|
{
|
|
|
|
$state = [
|
|
|
|
'template' => [
|
|
|
|
'header' => [
|
|
|
|
'id' => 'header',
|
|
|
|
'properties' => ['class' => 'text-white bg-blue-600 p-2'],
|
|
|
|
],
|
|
|
|
'product-table' => [
|
|
|
|
'id' => 'product-table',
|
|
|
|
'properties' => ['class' => 'table-auto'],
|
|
|
|
'elements' => [
|
|
|
|
['element' => 'thead', 'content' => '', 'elements' => [
|
|
|
|
['element' => 'tr', 'content' => '', 'elements' => [
|
|
|
|
['element' => 'th', 'content' => 'Title', 'properties' => ['class' => 'px-4 py-2']],
|
|
|
|
['element' => 'th', 'content' => 'Author', 'properties' => ['class' => 'px-4 py-2']],
|
|
|
|
['element' => 'th', 'content' => 'Views', 'properties' => ['class' => 'px-4 py-2']],
|
|
|
|
]]
|
|
|
|
]],
|
|
|
|
['element' => 'tbody', 'content' => '', 'elements' => [
|
|
|
|
['element' => 'tr', 'content' => '', 'elements' => [
|
|
|
|
['element' => 'td', 'content' => 'An amazing guy', 'properties' => ['class' => 'border px-4 py-2']],
|
|
|
|
['element' => 'td', 'content' => 'David Bomba', 'properties' => ['class' => 'border px-4 py-2']],
|
|
|
|
['element' => 'td', 'content' => '1M', 'properties' => ['class' => 'border px-4 py-2']],
|
|
|
|
]],
|
|
|
|
['element' => 'tr', 'content' => '', 'elements' => [
|
|
|
|
['element' => 'td', 'content' => 'Flutter master', 'properties' => ['class' => 'border px-4 py-2']],
|
|
|
|
['element' => 'td', 'content' => 'Hillel Coren', 'properties' => ['class' => 'border px-4 py-2']],
|
|
|
|
['element' => 'td', 'content' => '1M', 'properties' => ['class' => 'border px-4 py-2']],
|
|
|
|
]],
|
|
|
|
['element' => 'tr', 'content' => '', 'elements' => [
|
|
|
|
['element' => 'td', 'content' => 'Bosssssssss', 'properties' => ['class' => 'border px-4 py-2']],
|
|
|
|
['element' => 'td', 'content' => 'Shalom Stark', 'properties' => ['class' => 'border px-4 py-2']],
|
|
|
|
['element' => 'td', 'content' => '1M', 'properties' => ['class' => 'border px-4 py-2']],
|
|
|
|
]],
|
|
|
|
['element' => 'tr', 'content' => '', 'order' => 4, 'elements' => [
|
|
|
|
['element' => 'td', 'content' => 'Three amazing guys', 'properties' => ['class' => 'border px-4 py-2', 'colspan' => '100%']],
|
|
|
|
]],
|
|
|
|
]],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
|
|
|
'variables' =>[
|
|
|
|
'$title' => 'Invoice Ninja',
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$maker = new PdfMaker($state);
|
|
|
|
|
|
|
|
$maker
|
|
|
|
->design(Business::class)
|
|
|
|
->build();
|
|
|
|
|
|
|
|
info($maker->getCompiledHTML());
|
|
|
|
|
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
2020-07-09 16:05:17 +02:00
|
|
|
}
|