Skip to content

Commit

Permalink
Fixed price instanciation with minor value of 0
Browse files Browse the repository at this point in the history
  • Loading branch information
voidgraphics committed Jul 20, 2023
1 parent 55b1dca commit b617314
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Models/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public function __construct(

public function fill(array $attributes)
{
if ($minor = $attributes['minor'] ?? null) {
$minor = $attributes['minor'] ?? null;

if (! is_null($minor)) {
$attributes['amount'] = $minor;
} else if (($amount = $attributes['amount'] ?? null) && ($currency = $attributes['currency'] ?? null)) {
$attributes['amount'] = Money::of($amount, $currency)->getMinorAmount()->toInt();
Expand Down
16 changes: 16 additions & 0 deletions tests/Feature/InstanciationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@
$this->assertSame('EUR', $price->currency);
});

test('a price instance can be created with a minor value of 0', function() {
$price = new Price(
type: 'selling',
minor: 0,
currency: 'EUR',
activated_at: now()->addWeek()
);

$this->assertNotNull($price);
$this->assertInstanceOf(Price::class, $price);
$this->assertSame('selling', $price->type);
$this->assertNotNull($price->amount);
$this->assertSame(0, $price->amount);
$this->assertSame('EUR', $price->currency);
});

test('a price instance can be created from a minor value using a constructor arguments array', function() {
$price = new Price([
'type' => 'selling',
Expand Down

0 comments on commit b617314

Please sign in to comment.