From 00f2fc45ce7084edd32c20e9aa99b1fbae33a2dc Mon Sep 17 00:00:00 2001 From: Bostjan Date: Fri, 1 Mar 2024 20:15:05 +0100 Subject: [PATCH] Make due_date optional Fixes #3 --- src/UpnGenerator.php | 12 +++++++----- tests/Pest.php | 2 +- tests/Unit/GdResourceTest.php | 9 +++++++++ tests/Unit/QrCodeTest.php | 9 +++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/UpnGenerator.php b/src/UpnGenerator.php index 5b6a935..8f3be70 100644 --- a/src/UpnGenerator.php +++ b/src/UpnGenerator.php @@ -31,7 +31,7 @@ class UpnGenerator private string $purpose = ''; - private \DateTime $due_date; + private ?\DateTime $due_date = null; private const FONT = __DIR__.'/courbd.ttf'; @@ -77,8 +77,10 @@ public function gdResource(): \GdImage $this->writeText(528, 340, $this->purpose ?? ''); $this->writeText(30, 165, $this->purpose ?? '', 10); - $this->writeText(1155, 340, $this->due_date->format('d.m.Y')); - $this->writeText(30, 195, $this->due_date->format('d.m.Y'), self::FONT_SMALL); + if ($this->due_date) { + $this->writeText(1155, 340, $this->due_date->format('d.m.Y')); + $this->writeText(30, 195, $this->due_date->format('d.m.Y'), self::FONT_SMALL); + } $this->writeText(110, 247, '***'.$this->getFormattedPrice(), self::FONT_SMALL); $this->writeText(750, 285, '***'.$this->getFormattedPrice()); @@ -142,7 +144,7 @@ public function getQRCodeText(): string '', $this->code ?? '', $this->purpose ?? '', - $this->due_date->format('d.m.Y') ?? '', + $this->due_date?->format('d.m.Y') ?? '', $this->receiver_iban ?? '', $this->reference ?? '', $this->receiver_name ?? '', @@ -340,7 +342,7 @@ public function getDueDate(): \DateTime return $this->due_date; } - public function setDueDate(\DateTime $due_date): self + public function setDueDate(?\DateTime $due_date): self { $this->due_date = $due_date; diff --git a/tests/Pest.php b/tests/Pest.php index 1077561..b8a7c0a 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -53,5 +53,5 @@ function getDefaultUpn(): \Media24si\UpnGenerator\UpnGenerator ->setCode('RENT') ->setReference('SI121234567890120') ->setPurpose('Plačilo najemnine za marec') - ->setDueDate(new DateTime('+1 month')); + ->setDueDate(new DateTime('2024-03-24')); } diff --git a/tests/Unit/GdResourceTest.php b/tests/Unit/GdResourceTest.php index f41754c..cf24cbb 100644 --- a/tests/Unit/GdResourceTest.php +++ b/tests/Unit/GdResourceTest.php @@ -13,3 +13,12 @@ expect(md5($png))->toBe(md5_file(__DIR__.'/../snapshots/default.png')); }); + +it('generates GD resource for empty duedate', function () { + $upn = getDefaultUpn(); + $upn->setDueDate(null); + + $gdResource = $upn->gdResource(); + + expect($gdResource)->toBeInstanceOf(\GdImage::class); +}); diff --git a/tests/Unit/QrCodeTest.php b/tests/Unit/QrCodeTest.php index aede435..11cbb85 100644 --- a/tests/Unit/QrCodeTest.php +++ b/tests/Unit/QrCodeTest.php @@ -27,3 +27,12 @@ expect($qrCodeContent[8])->toBeString()->toBe('00000003330'); }); + +it('sets empty string for empty duedate', function () { + $upn = getDefaultUpn(); + $upn->setDueDate(null); + + $qrCodeContent = explode("\n", $upn->getQRCodeText()); + + expect($qrCodeContent[13])->toBeString()->toBe(''); +});