Skip to content

Commit

Permalink
feat(randomizer): randomizer may throw a range exception
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesRezo committed Oct 30, 2024
1 parent 9f09c1b commit d4aca3a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Randomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ public function __construct(
) {
}

/**
* {@inheritDoc}
*/
public function random(): int
{
if ($this->min > $this->max) {
throw new \RangeException('max value is not greater than or equal to min value');
}

return \mt_rand($this->min, $this->max);
}
}
3 changes: 3 additions & 0 deletions src/RandomizerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@

interface RandomizerInterface
{
/**
* @throws \RangeException if a range error at connstruction or at draw.
*/
public function random(): int;
}
13 changes: 13 additions & 0 deletions tests/RandomizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,17 @@ public function testRandom()
$this->assertGreaterThanOrEqual(1, $actual);
$this->assertLessThanOrEqual(6, $actual);
}

public function testRangeException()
{
// Given
$this->expectException(\RangeException::class);
$this->expectExceptionMessage('max value is not greater than or equal to min value');

// When
(new Randomizer(2, 1))->random();

// Then
// An exception is thrown
}
}

0 comments on commit d4aca3a

Please sign in to comment.