Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ public function exceeded(?int $releaseInSeconds = null): void

$this->hits = $this->allow;

if (isset($releaseInSeconds)) {
$interval = DateInterval::createFromDateString($releaseInSeconds . ' seconds');
$seconds = $releaseInSeconds ?? $this->releaseInSeconds;

if ($seconds > 0) {
$interval = DateInterval::createFromDateString($seconds . ' seconds');

if ($interval === false) {
return;
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/LimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,31 @@

expect($limit->getReleaseInSeconds())->toEqual($seconds);
});

test('exceeded without releaseInSeconds falls back to the configured interval', function () {
$limit = Limit::allow(10)->everySeconds(120);

$limit->exceeded();

expect($limit->wasManuallyExceeded())->toBeTrue()
->and($limit->getHits())->toBe(10)
->and($limit->getRemainingSeconds())->toBe(120);
});

test('exceeded with explicit releaseInSeconds uses the provided value', function () {
$limit = Limit::allow(10)->everySeconds(120);

$limit->exceeded(releaseInSeconds: 300);

expect($limit->wasManuallyExceeded())->toBeTrue()
->and($limit->getRemainingSeconds())->toBe(300);
});

test('custom limiter exceeded without releaseInSeconds falls back to default 60 seconds', function () {
$limit = Limit::custom(function () {});

$limit->exceeded();

expect($limit->wasManuallyExceeded())->toBeTrue()
->and($limit->getRemainingSeconds())->toBe(60);
});
Loading