|
2 | 2 |
|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
| 5 | +use Amp\Redis\Connection\RedisLink; |
| 6 | +use Amp\Redis\Protocol\RedisResponse; |
| 7 | +use Amp\Redis\RedisClient; |
| 8 | +use Kelunik\RateLimit\PrefixRateLimit; |
5 | 9 | use Kelunik\RateLimit\RedisRateLimit; |
6 | 10 | use Phenix\Cache\Constants\Store; |
7 | 11 | use Phenix\Cache\RateLimit\RateLimitManager; |
| 12 | +use Phenix\Database\Constants\Connection; |
8 | 13 | use Phenix\Facades\Config; |
| 14 | +use Phenix\Redis\ClientWrapper; |
9 | 15 |
|
10 | 16 | beforeEach(function (): void { |
11 | 17 | Config::set('cache.default', Store::REDIS->value); |
12 | 18 | Config::set('cache.rate_limit.store', Store::REDIS->value); |
13 | 19 | }); |
14 | 20 |
|
15 | | -it('call redis rate limit factory', function (): void { |
| 21 | +it('prefixes redis rate limit keys with the cache namespace', function (): void { |
| 22 | + Config::set('cache.prefix', 'cache-prefix:'); |
| 23 | + |
16 | 24 | $manager = new RateLimitManager(); |
| 25 | + $limiter = $manager->limiter(); |
| 26 | + |
| 27 | + expect($limiter)->toBeInstanceOf(PrefixRateLimit::class); |
| 28 | + |
| 29 | + $reflection = new ReflectionClass($limiter); |
| 30 | + |
| 31 | + $prefix = $reflection->getProperty('prefix'); |
| 32 | + $prefix->setAccessible(true); |
| 33 | + |
| 34 | + $rateLimit = $reflection->getProperty('rateLimit'); |
| 35 | + $rateLimit->setAccessible(true); |
| 36 | + |
| 37 | + expect($prefix->getValue($limiter))->toBe('cache-prefix:'); |
| 38 | + expect($rateLimit->getValue($limiter))->toBeInstanceOf(RedisRateLimit::class); |
| 39 | +}); |
| 40 | + |
| 41 | +it('isolates redis rate limit state across cache prefixes', function (): void { |
| 42 | + $incrementResponse = $this->createStub(RedisResponse::class); |
| 43 | + $incrementResponse->method('unwrap')->willReturn(1); |
| 44 | + |
| 45 | + $expireResponse = $this->createStub(RedisResponse::class); |
| 46 | + $expireResponse->method('unwrap')->willReturn(1); |
| 47 | + |
| 48 | + $getResponse = $this->createStub(RedisResponse::class); |
| 49 | + $getResponse->method('unwrap')->willReturn(null); |
| 50 | + |
| 51 | + $link = $this->createMock(RedisLink::class); |
| 52 | + |
| 53 | + $link->expects($this->exactly(3)) |
| 54 | + ->method('execute') |
| 55 | + ->withConsecutive( |
| 56 | + [ |
| 57 | + $this->equalTo('incr'), |
| 58 | + $this->equalTo(['first-prefix:route:client']), |
| 59 | + ], |
| 60 | + [ |
| 61 | + $this->equalTo('expire'), |
| 62 | + $this->equalTo(['first-prefix:route:client', 60]), |
| 63 | + ], |
| 64 | + [ |
| 65 | + $this->equalTo('get'), |
| 66 | + $this->equalTo(['second-prefix:route:client']), |
| 67 | + ] |
| 68 | + ) |
| 69 | + ->willReturnOnConsecutiveCalls($incrementResponse, $expireResponse, $getResponse); |
| 70 | + |
| 71 | + $client = new RedisClient($link); |
| 72 | + $this->app->swap(Connection::redis('default'), new ClientWrapper($client)); |
| 73 | + |
| 74 | + Config::set('cache.prefix', 'first-prefix:'); |
| 75 | + (new RateLimitManager())->prefixed('route:')->increment('client'); |
| 76 | + |
| 77 | + Config::set('cache.prefix', 'second-prefix:'); |
17 | 78 |
|
18 | | - expect($manager->limiter())->toBeInstanceOf(RedisRateLimit::class); |
| 79 | + expect((new RateLimitManager())->prefixed('route:')->get('client'))->toBe(0); |
19 | 80 | }); |
0 commit comments