Skip to content

Commit

Permalink
Merge pull request #17 from whitecube/fix-vat-not-initialized-error
Browse files Browse the repository at this point in the history
Fix property not initialized error on jsonSerialize
  • Loading branch information
voidgraphics authored Oct 11, 2024
2 parents 50bbf15 + 0e47fe5 commit 03c8f83
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Price implements \JsonSerializable
/**
* The VAT definition
*/
protected ?Vat $vat;
protected ?Vat $vat = null;

/**
* The price modifiers to apply
Expand Down Expand Up @@ -241,7 +241,7 @@ public function jsonSerialize(): array
'base' => $this->getRational($this->base)->getAmount(),
'currency' => $this->base->getCurrency()->getCurrencyCode(),
'units' => $this->units,
'vat' => $this->vat->percentage(),
'vat' => $this->vat?->percentage(),
'total' => [
'exclusive' => $this->getRational($excl)->getAmount(),
'inclusive' => $this->getRational($incl)->getAmount(),
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/HasVatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@
expect($instance->inclusive(true)->__toString())->toBe('EUR 5.50');
});

it('does not throw an exception when serializing a price with no vat', function () {
$instance = Price::ofMinor(500, 'EUR')->setUnits(3);

expect($instance->jsonSerialize())
->toBeArray();
});

0 comments on commit 03c8f83

Please sign in to comment.