-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[11.x] Allows Unit & Backed Enums for registering named
RateLimiter
…
… & `RateLimited` middleware (#52935) * added tests * Update RateLimiter.php * Update RateLimited.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
28ff0e3
commit ead6020
Showing
4 changed files
with
154 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Cache; | ||
|
||
use Illuminate\Cache\RateLimiter; | ||
use Illuminate\Cache\RateLimiting\Limit; | ||
use Illuminate\Contracts\Cache\Repository as Cache; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use ReflectionProperty; | ||
|
||
class RateLimiterTest extends TestCase | ||
{ | ||
public static function registerNamedRateLimiterDataProvider(): array | ||
{ | ||
return [ | ||
'uses BackedEnum' => [BackedEnumNamedRateLimiter::API, 'api'], | ||
'uses UnitEnum' => [UnitEnumNamedRateLimiter::THIRD_PARTY, 'THIRD_PARTY'], | ||
'uses normal string' => ['yolo', 'yolo'], | ||
'uses int' => [100, '100'], | ||
]; | ||
} | ||
|
||
#[DataProvider('registerNamedRateLimiterDataProvider')] | ||
public function testRegisterNamedRateLimiter(mixed $name, string $expected): void | ||
{ | ||
$reflectedLimitersProperty = new ReflectionProperty(RateLimiter::class, 'limiters'); | ||
$reflectedLimitersProperty->setAccessible(true); | ||
|
||
$rateLimiter = new RateLimiter($this->createMock(Cache::class)); | ||
$rateLimiter->for($name, fn () => Limit::perMinute(100)); | ||
|
||
$limiters = $reflectedLimitersProperty->getValue($rateLimiter); | ||
|
||
$this->assertArrayHasKey($expected, $limiters); | ||
|
||
$limiterClosure = $rateLimiter->limiter($name); | ||
|
||
$this->assertNotNull($limiterClosure); | ||
} | ||
} | ||
|
||
enum BackedEnumNamedRateLimiter: string | ||
{ | ||
case API = 'api'; | ||
} | ||
|
||
enum UnitEnumNamedRateLimiter | ||
{ | ||
case THIRD_PARTY; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters