Skip to content

Commit

Permalink
test invalid argument, fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed Nov 22, 2023
1 parent d77d552 commit 80094f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,13 @@ public function mod(Money|int|string $divisor): Money
if ($this->currency != $divisor->currency) {
throw new InvalidArgumentException('Currencies must be identical');
}
$amount = $divisor->amount;

$divisor = $divisor->amount;
} else {
if (is_int($divisor)) {
$divisor = (string) $divisor;
}
$amount = $divisor;
$divisor = (string) Number::fromNumber($divisor);
}

return new self(self::$calculator::mod($this->amount, $amount), $this->currency);
return new self(self::$calculator::mod($this->amount, $divisor), $this->currency);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,19 @@ public function itCalculatesTheModulusOfNumber($left, $right, $expected): void
self::assertEquals($expected, $money->getAmount());
}

/**
* @test
*/
public function itThrowsWhenDivisorIsInvalidStringArgument(): void
{
$money = new Money(self::AMOUNT, new Currency(self::CURRENCY));

$this->expectException(InvalidArgumentException::class);

/** @psalm-suppress UnusedMethodCall this method throws */
$money->mod('test');
}

/**
* @test
*/
Expand Down

0 comments on commit 80094f7

Please sign in to comment.