Skip to content

Commit

Permalink
Fixed new functions exception message and added missing test function
Browse files Browse the repository at this point in the history
  • Loading branch information
NCatalani committed Mar 29, 2021
1 parent 5c6d80e commit 82c13b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,19 +581,19 @@ public function allocate(int ...$ratios) : array
public function allocateWithRemainder(int ...$ratios) : array
{
if (! $ratios) {
throw new \InvalidArgumentException('Cannot allocate() an empty list of ratios.');
throw new \InvalidArgumentException('Cannot allocateWithRemainder() an empty list of ratios.');
}

foreach ($ratios as $ratio) {
if ($ratio < 0) {
throw new \InvalidArgumentException('Cannot allocate() negative ratios.');
throw new \InvalidArgumentException('Cannot allocateWithRemainder() negative ratios.');
}
}

$total = array_sum($ratios);

if ($total === 0) {
throw new \InvalidArgumentException('Cannot allocate() to zero ratios only.');
throw new \InvalidArgumentException('Cannot allocateWithRemainder() to zero ratios only.');
}

$monies = [];
Expand Down
28 changes: 25 additions & 3 deletions tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public function testAllocateWithRemainderEmptyList() : void
$money = Money::of(50, 'USD');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot allocate() an empty list of ratios.');
$this->expectExceptionMessage('Cannot allocateWithRemainder() an empty list of ratios.');

$money->allocateWithRemainder();
}
Expand All @@ -454,7 +454,7 @@ public function testAllocateWithRemainderNegativeRatios() : void
$money = Money::of(50, 'USD');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot allocate() negative ratios.');
$this->expectExceptionMessage('Cannot allocateWithRemainder() negative ratios.');

$money->allocateWithRemainder(1, 2, -1);
}
Expand All @@ -464,7 +464,7 @@ public function testAllocateWithRemainderZeroRatios() : void
$money = Money::of(50, 'USD');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot allocate() to zero ratios only.');
$this->expectExceptionMessage('Cannot allocateWithRemainder() to zero ratios only.');

$money->allocateWithRemainder(0, 0, 0, 0, 0);
}
Expand Down Expand Up @@ -538,6 +538,28 @@ public function providerSplitIntoLessThanOnePart() : array
];
}

/**
* @dataProvider providerSplitWithRemainderIntoLessThanOnePart
*/
public function testSplitWithRemainderIntoLessThanOnePart(int $parts) : void
{
$money = Money::of(50, 'USD');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot splitWithRemainder() into less than 1 part.');

$money->splitWithRemainder($parts);
}

public function providerSplitWithRemainderIntoLessThanOnePart() : array
{
return [
[-1],
[0]
];
}


/**
* @dataProvider providerAbs
*/
Expand Down

0 comments on commit 82c13b7

Please sign in to comment.