Skip to content

Commit

Permalink
Make due_date optional
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
BostjanOb committed Mar 1, 2024
1 parent 81524c4 commit 00f2fc4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/UpnGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UpnGenerator

private string $purpose = '';

private \DateTime $due_date;
private ?\DateTime $due_date = null;

private const FONT = __DIR__.'/courbd.ttf';

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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 ?? '',
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
9 changes: 9 additions & 0 deletions tests/Unit/GdResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
9 changes: 9 additions & 0 deletions tests/Unit/QrCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
});

0 comments on commit 00f2fc4

Please sign in to comment.