From 76d2bd927a9c7fd51e3d9f73a595feeba8897f36 Mon Sep 17 00:00:00 2001 From: Adrien Leloup Date: Fri, 11 Oct 2024 15:21:04 +0200 Subject: [PATCH 1/2] Fix property not initialized error on jsonSerialize --- src/Price.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Price.php b/src/Price.php index 2c5e2aa..3f9a2c2 100644 --- a/src/Price.php +++ b/src/Price.php @@ -39,7 +39,7 @@ class Price implements \JsonSerializable /** * The VAT definition */ - protected ?Vat $vat; + protected ?Vat $vat = null; /** * The price modifiers to apply From 0e47fe5288c2089ef9a830eb70340fea3db627ae Mon Sep 17 00:00:00 2001 From: Adrien Leloup Date: Fri, 11 Oct 2024 15:24:57 +0200 Subject: [PATCH 2/2] Added test --- src/Price.php | 2 +- tests/Unit/HasVatTest.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Price.php b/src/Price.php index 3f9a2c2..c4ee677 100644 --- a/src/Price.php +++ b/src/Price.php @@ -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(), diff --git a/tests/Unit/HasVatTest.php b/tests/Unit/HasVatTest.php index 2146faf..dc85736 100644 --- a/tests/Unit/HasVatTest.php +++ b/tests/Unit/HasVatTest.php @@ -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(); +});