Skip to content

Commit

Permalink
Fill the Currrency Field without checking (#128)
Browse files Browse the repository at this point in the history
* No Need to check if currency field exists

the currency field might not exist (initialized) yet, so it doesn't fill the currency field when a new instance is created

* add a test for filling currencies
add check if the field is a currency or an attribute

* fix style issues

* Update MoneyCastTest.php

Co-authored-by: Ahmed Ghorab <[email protected]>
  • Loading branch information
A-Ghorab and AGhorabHekouky authored Nov 9, 2022
1 parent 13a87ac commit 8c3ed1c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Casts/MoneyCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function set($model, string $key, $value, array $attributes)

$amount = $this->getFormatter($money);

if (array_key_exists($this->currency, $attributes)) {
if ($this->currency && ! Money::isValidCurrency($this->currency)) {
return [$key => $amount, $this->currency => $money->getCurrency()->getCode()];
}

Expand Down
11 changes: 11 additions & 0 deletions tests/MoneyCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,15 @@ public function testFailsToParseInvalidMoney()

new User(['money' => 'abc']);
}

public function testSetCurrencyForNewEmptyUser()
{
$user = new User();

$user->debits = 100.99;

static::assertSame('10099', $user->debits->getAmount());
static::assertSame('USD', $user->debits->getCurrency()->getCode());
static::assertSame('USD', $user->currency);
}
}

0 comments on commit 8c3ed1c

Please sign in to comment.