1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/tests/Feature/PdfMaker/PdfMakerTest.php

319 lines
11 KiB
PHP
Raw Normal View History

<?php
2020-09-14 13:11:46 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-09-14 13:11:46 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Feature\PdfMaker;
use App\Services\PdfMaker\Design;
2020-07-22 14:34:15 +02:00
use App\Services\PdfMaker\PdfMaker;
use Tests\TestCase;
class PdfMakerTest extends TestCase
{
2020-07-13 13:51:54 +02:00
public $state = [
'template' => [],
2020-08-10 18:06:12 +02:00
'variables' => [
'labels' => [],
'values' => [],
],
2020-07-13 13:51:54 +02:00
];
public function testDesignLoadsCorrectly()
{
2020-09-04 13:17:30 +02:00
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
2020-07-13 13:51:54 +02:00
$maker = new PdfMaker($this->state);
$maker->design($design);
$this->assertInstanceOf(Design::class, $maker->design);
}
public function testHtmlDesignLoadsCorrectly()
{
2020-09-04 13:17:30 +02:00
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
2020-07-13 13:51:54 +02:00
$maker = new PdfMaker($this->state);
$maker
->design($design)
->build();
$this->assertStringContainsString('Template: Example', $maker->getCompiledHTML());
}
public function testGetSectionUtility()
{
2020-09-04 13:17:30 +02:00
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
2020-07-13 13:51:54 +02:00
$maker = new PdfMaker($this->state);
$maker
->design($design)
->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)',
],
],
],
2020-08-10 18:06:12 +02:00
'variables' => [
'labels' => [],
'values' => [],
],
];
2020-09-04 13:17:30 +02:00
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($state);
$maker
->design($design)
->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',
],
],
'variables' => [
2020-08-10 18:06:12 +02:00
'labels' => [],
'values' => [
'$company.name' => 'Invoice Ninja',
2020-08-10 18:06:12 +02:00
],
],
];
2020-09-04 13:17:30 +02:00
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($state);
$maker
->design($design)
->build();
$this->assertStringContainsString('Invoice Ninja', $maker->getCompiledHTML());
$this->assertStringContainsString('Invoice Ninja', $maker->getSection('header'));
}
public function testElementContentIsGenerated()
{
$state = [
'template' => [
'product-table' => [
'id' => 'product-table',
'properties' => [],
'elements' => [
['element' => 'thead', 'content' => '', 'elements' => [
['element' => 'th', 'content' => 'Company'],
['element' => 'th', 'content' => 'Contact'],
['element' => 'th', 'content' => 'Country', 'properties' => [
'colspan' => 3,
]],
]],
['element' => 'tr', 'content' => '', 'elements' => [
['element' => 'td', 'content' => '$company'],
['element' => 'td', 'content' => '$email'],
['element' => 'td', 'content' => '$country', 'elements' => [
['element' => 'a', 'content' => 'Click here for a link', 'properties' => [
'href' => 'https://github.com/invoiceninja/invoiceninja',
]],
]],
]],
],
],
],
'variables' => [
2020-08-10 18:06:12 +02:00
'labels' => [],
'values' => [
'$company' => 'Invoice Ninja',
'$email' => 'contact@invoiceninja.com',
'$country' => 'UK',
],
],
];
2020-09-04 13:17:30 +02:00
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$maker = new PdfMaker($state);
$maker
->design($design)
->build();
$compiled = 'contact@invoiceninja.com';
$this->assertStringContainsString($compiled, $maker->getCompiledHTML());
}
2020-07-14 14:02:02 +02:00
public function testConditionalRenderingOfElements()
{
2020-09-04 13:17:30 +02:00
$design1 = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
2020-08-07 08:34:34 +02:00
2020-07-14 14:02:02 +02:00
$maker1 = new PdfMaker([
'template' => [
'header' => [
'id' => 'header',
'properties' => [],
],
],
]);
$maker1
->design($design1)
2020-07-14 14:02:02 +02:00
->build();
$output1 = $maker1->getCompiledHTML();
$this->assertStringContainsString('<div id="header">', $output1);
2020-07-14 14:02:02 +02:00
2020-09-04 13:17:30 +02:00
$design2 = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
2020-07-14 14:02:02 +02:00
$maker2 = new PdfMaker([
'template' => [
'header' => [
'id' => 'header',
'properties' => ['hidden' => 'true'],
2020-07-14 14:02:02 +02:00
],
],
]);
$maker2
->design($design2)
2020-07-14 14:02:02 +02:00
->build();
$output2 = $maker2->getCompiledHTML();
$this->assertStringContainsString('<div id="header" hidden="true">$company.name</div>', $output2);
2020-07-14 14:02:02 +02:00
$this->assertNotSame($output1, $output2);
}
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']],
]],
2020-07-14 15:13:46 +02:00
]],
['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%']],
]],
]],
],
],
2020-07-14 15:13:46 +02:00
],
2020-08-10 18:06:12 +02:00
'variables' => [
'labels' => [],
'values' => [
'$title' => 'Invoice Ninja',
],
],
2020-07-14 15:13:46 +02:00
];
2020-09-04 13:17:30 +02:00
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
2020-07-14 15:13:46 +02:00
$maker = new PdfMaker($state);
$maker
->design($design)
2020-07-14 15:13:46 +02:00
->build();
$this->assertTrue(true);
}
public function testGetSectionHTMLWorks()
{
2020-09-04 13:17:30 +02:00
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$html = $design
->document()
->getSectionHTML('product-table');
$this->assertStringContainsString('id="product-table"', $html);
}
public function testWrapperHTMLWorks()
{
2020-09-04 13:17:30 +02:00
$design = new Design('example', ['custom_path' => base_path('tests/Feature/PdfMaker/')]);
$state = [
2020-08-26 09:02:38 +02:00
'template' => [
'product-table' => [
'id' => 'product-table',
'elements' => [
['element' => 'p', 'content' => 'Example paragraph'],
],
],
],
'variables' => [
'labels' => [],
'values' => [],
],
'options' => [
'all_pages_header' => true,
'all_pages_footer' => true,
],
];
$maker = new PdfMaker($state);
$maker
->design($design)
->build();
2020-09-03 13:01:04 +02:00
// exec('echo "" > storage/logs/laravel.log');
2020-12-29 22:10:03 +01:00
// nlog($maker->getCompiledHTML(true));
$this->assertTrue(true);
}
}