fix: validate limit criteria must be positive integer

Previously, negative limits caused array_slice() to return unexpected
results (all except last N elements) and zero returned an empty array
without clear intent.

Now throws InvalidArgumentException for values < 1, making invalid
configurations fail fast with a clear error message.
This commit is contained in:
Younes ENNAJI
2026-03-01 20:06:53 +00:00
parent fd36c2ec0c
commit ad5c0f56dd
2 changed files with 12 additions and 20 deletions
@@ -59,18 +59,12 @@ final class LimitCriteriaTest extends TestCase
$this->assertCount(2, $result);
}
public function testLimitZeroReturnsEmptyArray(): void
public function testLimitZeroThrowsException(): void
{
$envelopes = [
new Envelope(new Notification()),
new Envelope(new Notification()),
];
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Criteria 'limit' must be a positive integer (>= 1).");
$criteria = new LimitCriteria(0);
$result = $criteria->apply($envelopes);
$this->assertCount(0, $result);
new LimitCriteria(0);
}
public function testEmptyArrayReturnsEmpty(): void
@@ -121,17 +115,11 @@ final class LimitCriteriaTest extends TestCase
$this->assertCount(1, $result);
}
public function testNegativeLimitReturnsEmptyArray(): void
public function testNegativeLimitThrowsException(): void
{
$envelopes = [
new Envelope(new Notification()),
new Envelope(new Notification()),
];
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Criteria 'limit' must be a positive integer (>= 1).");
$criteria = new LimitCriteria(-5);
$result = $criteria->apply($envelopes);
$this->assertCount(0, $result);
new LimitCriteria(-5);
}
}