mivita/tests/Feature/Pdf/PdfPipelineTest.php
Kevin Adametz 0a3eb791d4 PDF: Seitenzahlen auf allen Seiten, Lieferschein-Beschriftung
- page_script in invoice, delivery, credit und cancellation ans Ende des
  body verschoben (wirkte nur auf Seite 1), Beschriftung uebersetzt
- Gutschriften rendern mit enable_php wie Rechnungen
- Lieferschein: Schluessel pdf.delivery_note_no ohne ueberzaehligen Punkt
- Tests: Position des Skripts, alle pdf.*-Schluessel in allen Sprachen

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 13:06:55 +00:00

78 lines
3 KiB
PHP

<?php
use App\Libraries\InvoicePDF;
use App\Libraries\MyPDFMerger;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\View;
use setasign\Fpdi\Fpdi;
use Tests\TestCase;
uses(TestCase::class);
/**
* Rechnungen, Lieferscheine und Gutschriften laufen über dompdf mit
* eingebettetem PHP (Seitenzahlen per page_script/page_text) und werden
* anschließend mit FPDI auf das Briefpapier gelegt. Schützt diesen Weg bei
* Updates von dompdf oder FPDI.
*
* page_script wirkt nur auf Seiten, die beim Aufruf schon gerendert sind —
* das Skript muss deshalb am Ende des <body> stehen (wie in credit_details).
*/
beforeEach(function () {
$this->pdfDir = storage_path('framework/testing/pdf-pipeline-'.uniqid()).'/';
File::ensureDirectoryExists($this->pdfDir);
View::addLocation($this->pdfDir);
File::put($this->pdfDir.'pdf-probe.blade.php', <<<'BLADE'
<html><body>
<p>Seite eins</p>
<div style="page-break-before: always;">Seite zwei</div>
<script type="text/php">
if (isset($pdf)) {
$pdf->page_script('$GLOBALS["pdfProbePages"][] = $PAGE_NUM."/".$PAGE_COUNT;');
}
</script>
</body></html>
BLADE);
$GLOBALS['pdfProbePages'] = [];
});
afterEach(function () {
File::deleteDirectory($this->pdfDir);
unset($GLOBALS['pdfProbePages']);
});
test('embedded php runs on every page of an invoice pdf', function () {
$file = (new InvoicePDF('pdf-probe'))->create([], 'probe.pdf', 'save', $this->pdfDir);
expect($file)->toBe($this->pdfDir.'probe.pdf')
->and(file_get_contents($file))->toStartWith('%PDF')
->and($GLOBALS['pdfProbePages'])->toBe(['1/2', '2/2']);
});
test('page numbers are added at the end of the template so they reach every page', function (string $template) {
$view = file_get_contents(resource_path("views/pdf/{$template}.blade.php"));
expect(substr_count($view, 'type="text/php"'))->toBe(1)
->and($view)->toMatch('#</script>\s*</body>#');
})->with(['invoice', 'delivery', 'credit', 'cancellation', 'credit_details', 'credit_details_long']);
test('every pdf translation key used in the templates exists', function (string $locale) {
$keys = collect(File::files(resource_path('views/pdf')))
->flatMap(fn ($file) => preg_match_all("/__\\('(pdf\\.[^']+)'/", $file->getContents(), $matches) ? $matches[1] : [])
->unique();
expect($keys)->not->toBeEmpty()
->and($keys->reject(fn ($key) => trans()->hasForLocale($key, $locale))->values()->all())->toBe([]);
})->with(['de', 'en', 'es', 'fr']);
test('the rendered pdf can be laid onto the letterhead template', function () {
$file = (new InvoicePDF('pdf-probe'))->create([], 'probe.pdf', 'save', $this->pdfDir);
$merged = (new MyPDFMerger)->addPDF($file)->myMerge('string', 'probe.pdf', 'template_invoice_de');
File::put($this->pdfDir.'merged.pdf', $merged);
expect($merged)->toStartWith('%PDF')
->and((new Fpdi)->setSourceFile($this->pdfDir.'merged.pdf'))->toBe(2);
});