Skip to content

Commit

Permalink
feat(Debug): Debug info of Currency and Price
Browse files Browse the repository at this point in the history
  • Loading branch information
4513 committed Aug 21, 2023
1 parent 8f0d277 commit 1bb996d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
45 changes: 45 additions & 0 deletions src/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ public function getDateTime(): ?DateTime
return $this->time;
}

/**
* @inheritDoc
*
* @return \MiBo\Prices\Units\Price\Currency
*/
public function getUnit(): Unit
{
/** @phpstan-var \MiBo\Prices\Units\Price\Currency */
return parent::getUnit();
}

// @codeCoverageIgnoreStart

/**
Expand Down Expand Up @@ -740,4 +751,38 @@ public function __clone(): void

$this->prices = array_map(fn (PriceInterface $price) => clone $price, $this->prices);
}

/**
* Debug info.
*
* @return array{
* price: int|float,
* priceWithVAT: int|float,
* valueOfVAT: int|float,
* currency: string,
* VAT: string,
* time: string|null,
* details: array{
* unit: \MiBo\Prices\Units\Price\Currency,
* VAT: \MiBo\VAT\VAT,
* prices: array<\MiBo\Prices\Contracts\PriceInterface>
* }
* }
*/
public function __debugInfo(): array
{
return [
'price' => $this->getValue(),
'priceWithVAT' => $this->getValueWithVAT(),
'valueOfVAT' => $this->getValueOfVAT(),
'currency' => $this->getUnit()->getAlphabeticalCode(),
'VAT' => $this->getVAT()->getRate()->name,
'time' => $this->getDateTime()?->format(DateTime::ATOM),
'details' => [
'unit' => $this->getUnit(),
'VAT' => $this->getVAT(),
'prices' => $this->getNestedPrices(),
],
];
}
}
20 changes: 20 additions & 0 deletions src/Units/Price/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,24 @@ public function is(Unit|CurrencyInterface $unit): bool
{
return $unit instanceof CurrencyInterface && $this->currency->is($unit);
}

/**
* Debug info.
*
* @return array{
* name: string,
* alphabeticalCode: string,
* minorUnitRate: int|null,
* numericalCode: string
* }
*/
public function __debugInfo(): array
{
return [
'name' => $this->getName(),
'alphabeticalCode' => $this->getAlphabeticalCode(),
'numericalCode' => $this->getNumericalCode(),
'minorUnitRate' => $this->getMinorUnitRate(),
];
}
}
22 changes: 20 additions & 2 deletions tests/Core/PriceProperty/CreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MiBo\Prices\Tests\Core\PriceProperty;

use DateTime;
use MiBo\Prices\Price;
use MiBo\Prices\Units\Price\Currency;
use MiBo\Properties\Value;
Expand Down Expand Up @@ -32,6 +33,8 @@ class CreationTest extends TestCase
* @covers ::getValue
* @covers ::getBaseValue
* @covers ::getDateTime
* @covers ::getUnit
* @covers ::__debugInfo
*
* @return void
*/
Expand All @@ -49,8 +52,23 @@ public function testConstruct(): void
$this->assertSame($price->getValue(), $price->getBaseValue());
$this->assertNull($price->getDateTime());

$price = new Price(50, Currency::get("EUR"), null, new \DateTime());
$time = new DateTime();
$price = new Price(50, Currency::get("EUR"), null, $time);

$this->assertLessThanOrEqual(time(), $price->getDateTime()->getTimestamp());
$this->assertEquals($time->getTimestamp(), $price->getDateTime()->getTimestamp());

$this->assertSame(
[
'price' => 50,
'priceWithVAT' => 50,
'valueOfVAT' => 0,
'currency' => "EUR",
'VAT' => VATRate::NONE->name,
'time' => $time->format(DateTime::ATOM),
],
array_filter($price->__debugInfo(), function(string $key): bool {
return $key !== 'details';
}, ARRAY_FILTER_USE_KEY)
);
}
}
12 changes: 12 additions & 0 deletions tests/Core/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function testCreation(): void
* @covers ::setCurrencyProvider
* @covers ::is
* @covers ::get
* @covers ::__debugInfo
*
* @return void
*/
Expand All @@ -74,5 +75,16 @@ public function testCommonUnitMethods(): void

Currency::get();
$this->assertNotNull(Currency::setCurrencyProvider(null));

$currency = Currency::get("EUR");
$this->assertSame(
[
"name" => "Euro",
"alphabeticalCode" => "EUR",
"numericalCode" => "978",
"minorUnitRate" => 2,
],
$currency->__debugInfo()
);
}
}

0 comments on commit 1bb996d

Please sign in to comment.