Skip to content

Commit

Permalink
Improve pretty-print formatting of negative amount of money (#4)
Browse files Browse the repository at this point in the history
* Improve pretty-print formatting of negative amount of money
  • Loading branch information
VladimirRebilly authored Mar 4, 2019
1 parent fd5985f commit 812b419
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ $config = PhpCsFixer\Config::create()
'standardize_not_equals' => true,
'strict_comparison' => true,
'strict_param' => true,
'ternary_operator_spaces' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ Security - in case of vulnerabilities.

_TBD_

## 1.0.1 (2019-03-05)

### Fixed
+ Fixed pretty-print formatting of negative amount of money

## 1.0.0 (2018-12-08)

Initial Release
6 changes: 5 additions & 1 deletion src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ public function getFormattedAmount(string $decimalPoint = '.', string $thousands
*/
public function getPrettyPrint(string $decimalPoint = '.', string $thousandsSeparator = ','): string
{
return $this->getCurrency()->getSign() . $this->getFormattedAmount($decimalPoint, $thousandsSeparator);
$currencySign = $this->getCurrency()->getSign();
$formattedAmount = ltrim($this->getFormattedAmount($decimalPoint, $thousandsSeparator), '-');
$amountSign = $this->isNegative() ? '-' : '';

return "{$amountSign}{$currencySign}{$formattedAmount}";
}

/**
Expand Down
11 changes: 8 additions & 3 deletions tests/Exchange/RateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public function testExceptionIsRaisedForInvalidConstructorArgument(): void
{
$cp = new CurrencyPair(new Currency('USD'), new Currency('EUR'));
$this->expectException(InvalidArgumentException::class);
new Rate($cp, new DateTime(), 0);
new Rate($cp, $this->getRateDate(), 0);
}

public function testCanBeConstructed()
{
$cp = new CurrencyPair(new Currency('USD'), new Currency('EUR'));
$rate = new Rate($cp, new DateTime(), 0.92);
$rate = new Rate($cp, $this->getRateDate(), 0.92);

$this->assertInstanceOf(Rate::class, $rate);

Expand All @@ -36,7 +36,7 @@ public function testCanBeConstructed()
public function testCanBeSerialized(Rate $rate): void
{
$this->assertSame(
'{"baseCurrency":"USD","quoteCurrency":"EUR","date":"' . date('c') . '","ratio":0.92}',
'{"baseCurrency":"USD","quoteCurrency":"EUR","date":"' . $this->getRateDate()->format('c') . '","ratio":0.92}',
json_encode($rate)
);
}
Expand Down Expand Up @@ -112,4 +112,9 @@ public function testCanConvertMoneyWithMarkup(Rate $rate): void
$this->assertSame(966, $newMoney->getAmount());
$this->assertSame('EUR', $newMoney->getCurrency()->getCurrencyCode());
}

private function getRateDate(): DateTime
{
return new DateTime('2019-03-05 13:32');
}
}
7 changes: 7 additions & 0 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ public function testFormattedAmountCanBeRetrieved(): void
$this->assertSame('€1,200.34', $m->getPrettyPrint('.', ','));
}

public function testFormattedNegativeAmountCanBeRetrieved(): void
{
$m = new Money(-120034, new Currency('EUR'));
$this->assertSame('-1 200.34', $m->getFormattedAmount('.', ' '));
$this->assertSame('-€1,200.34', $m->getPrettyPrint('.', ','));
}

/**
* @depends testObjectCanBeConstructedFromIntegerValueAndCurrencyObject
*
Expand Down

0 comments on commit 812b419

Please sign in to comment.