fix: add validation to HopsStamp and DelayStamp

HopsStamp:
- Validate hops amount must be >= 1 (positive integer)
- Negative or zero hops don't make logical sense

DelayStamp:
- Validate delay must be >= 0 (non-negative integer)
- Negative delays don't make logical sense

Both now throw InvalidArgumentException for invalid values, making
configuration errors fail fast with clear messages.
This commit is contained in:
Younes ENNAJI
2026-03-01 20:07:54 +00:00
parent ad5c0f56dd
commit 6d314dbc07
4 changed files with 50 additions and 0 deletions
+6
View File
@@ -6,8 +6,14 @@ namespace Flasher\Prime\Stamp;
final readonly class DelayStamp implements StampInterface final readonly class DelayStamp implements StampInterface
{ {
/**
* @throws \InvalidArgumentException
*/
public function __construct(private int $delay) public function __construct(private int $delay)
{ {
if ($delay < 0) {
throw new \InvalidArgumentException('Delay must be a non-negative integer (>= 0).');
}
} }
public function getDelay(): int public function getDelay(): int
+6
View File
@@ -6,8 +6,14 @@ namespace Flasher\Prime\Stamp;
final readonly class HopsStamp implements StampInterface final readonly class HopsStamp implements StampInterface
{ {
/**
* @throws \InvalidArgumentException
*/
public function __construct(private int $amount) public function __construct(private int $amount)
{ {
if ($amount < 1) {
throw new \InvalidArgumentException('Hops amount must be a positive integer (>= 1).');
}
} }
public function getAmount(): int public function getAmount(): int
+15
View File
@@ -26,4 +26,19 @@ final class DelayStampTest extends TestCase
$this->assertSame($this->testDelay, $delay); $this->assertSame($this->testDelay, $delay);
} }
public function testZeroDelayIsValid(): void
{
$stamp = new DelayStamp(0);
$this->assertSame(0, $stamp->getDelay());
}
public function testNegativeDelayThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Delay must be a non-negative integer (>= 0).');
new DelayStamp(-100);
}
} }
+23
View File
@@ -20,4 +20,27 @@ final class HopsStampTest extends TestCase
$this->assertSame($initialAmount, $hopsStamp->getAmount()); $this->assertSame($initialAmount, $hopsStamp->getAmount());
} }
public function testZeroHopsThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Hops amount must be a positive integer (>= 1).');
new HopsStamp(0);
}
public function testNegativeHopsThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Hops amount must be a positive integer (>= 1).');
new HopsStamp(-5);
}
public function testMinimumValidHops(): void
{
$stamp = new HopsStamp(1);
$this->assertSame(1, $stamp->getAmount());
}
} }