Skip to content

Commit

Permalink
Improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Oct 13, 2024
1 parent d0cc000 commit 5db8ac8
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions tests/testcases/OrderDocumentPdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace horstoeko\orderx\tests\testcases;

use horstoeko\orderx\codelists\OrderDocumentTypes;
use horstoeko\orderx\OrderDocumentPdfBuilder;
use horstoeko\orderx\OrderSettings;
use horstoeko\orderx\tests\TestCase;
use horstoeko\orderx\tests\traits\HandlesCreateTestDocument;
use horstoeko\stringmanagement\FileUtils;
use horstoeko\stringmanagement\PathUtils;
use horstoeko\orderx\OrderDocumentPdfBuilder;
use setasign\Fpdi\PdfParser\PdfParserException;
use horstoeko\orderx\codelists\OrderDocumentTypes;
use horstoeko\orderx\exception\OrderFileNotFoundException;
use horstoeko\orderx\tests\traits\HandlesCreateTestDocument;

class OrderDocumentPdfBuilderTest extends TestCase
{
Expand Down Expand Up @@ -266,4 +268,56 @@ public function testPdfSavingFromStringBasedPdf(): void

$this->assertFileExists($destinationPdfFilename);
}

public function testFromPdfFile(): void
{
$sourcePdfFilename = PathUtils::combinePathWithFile(OrderSettings::getAssetDirectory(), "empty.pdf");
$destinationPdfFilename = PathUtils::combinePathWithFile(OrderSettings::getAssetDirectory(), "final.pdf");
$orderDocument = $this->createTestDocument(OrderDocumentTypes::ORDER);

$pdfBuilder = OrderDocumentPdfBuilder::fromPdfFile($orderDocument, $sourcePdfFilename);
$pdfBuilder->generateDocument();
$pdfBuilder->downloadString($destinationPdfFilename);

$this->assertIsString($destinationPdfFilename);
}

public function testFromNotExistingPdfFile(): void
{
$this->expectException(OrderFileNotFoundException::class);

$orderDocument = $this->createTestDocument(OrderDocumentTypes::ORDER);

$pdfBuilder = OrderDocumentPdfBuilder::fromPdfFile($orderDocument, '/tmp/anonexisting.pdf');
}

public function testFromPdfString(): void
{
$sourcePdfFilename = PathUtils::combinePathWithFile(OrderSettings::getAssetDirectory(), "empty.pdf");
$destinationPdfFilename = PathUtils::combinePathWithFile(OrderSettings::getAssetDirectory(), "final.pdf");
$orderDocument = $this->createTestDocument(OrderDocumentTypes::ORDER);

$pdfString = file_get_contents($sourcePdfFilename);

$pdfBuilder = OrderDocumentPdfBuilder::fromPdfString($orderDocument, $pdfString);
$pdfBuilder->generateDocument();
$pdfBuilder->downloadString($destinationPdfFilename);

$this->assertIsString($destinationPdfFilename);
}

public function testFromPdfStringWhichIsInvalid(): void
{
$orderDocument = $this->createTestDocument(OrderDocumentTypes::ORDER);
$destinationPdfFilename = PathUtils::combinePathWithFile(OrderSettings::getAssetDirectory(), "final.pdf");

$this->expectException(PdfParserException::class);
$this->expectExceptionMessage('Unable to find PDF file header.');

$pdfString = 'this_is_not_a_pdf_string';

$pdfBuilder = OrderDocumentPdfBuilder::fromPdfString($orderDocument, $pdfString);
$pdfBuilder->generateDocument();
$pdfBuilder->downloadString($destinationPdfFilename);
}
}

0 comments on commit 5db8ac8

Please sign in to comment.