Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(currency): allow enums to be passed as currency #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/CurrenciesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
return new Currency($currency);
}

if ($currency instanceof \StringBackedEnum) {
return new Currency($currency->value);

Check warning on line 38 in src/CurrenciesTrait.php

View check run for this annotation

Codecov / codecov/patch

src/CurrenciesTrait.php#L38

Added line #L38 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not being covered by the tests

}

if ($currency instanceof \UnitEnum) {
return new Currency($currency->name);
}

return $currency;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Enums/CurrencyIntBacked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cknow\Money\Tests\Enums;

enum CurrencyIntBacked: int
{
case USD = 1;
}
8 changes: 8 additions & 0 deletions tests/Enums/CurrencyNotBacked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cknow\Money\Tests\Enums;

enum CurrencyNotBacked
{
case USD;
}
8 changes: 8 additions & 0 deletions tests/Enums/CurrencyStringBacked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cknow\Money\Tests\Enums;

enum CurrencyStringBacked: string
{
case USD = 'USD';
}
11 changes: 11 additions & 0 deletions tests/MoneyParserTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Cknow\Money\Tests;

use Cknow\Money\Money;
use Cknow\Money\Tests\Enums\CurrencyIntBacked;
use Cknow\Money\Tests\Enums\CurrencyNotBacked;
use Cknow\Money\Tests\Enums\CurrencyStringBacked;
use InvalidArgumentException;
use Money\Currency;
use Money\Exception\ParserException;
Expand Down Expand Up @@ -101,6 +104,14 @@ public function test_parse_invalid_money_value()
Money::parse(new stdClass);
}

public function test_parse_currency()
{
static::assertEquals(Money::parseCurrency('USD'), new Currency('USD'));
static::assertEquals(Money::parseCurrency(CurrencyStringBacked::USD), new Currency('USD'));
static::assertEquals(Money::parseCurrency(CurrencyIntBacked::USD), new Currency('USD'));
static::assertEquals(Money::parseCurrency(CurrencyNotBacked::USD), new Currency('USD'));
}

public function test_parse_invalid_money()
{
$this->expectException(ParserException::class);
Expand Down