RateLimiterFactory example

->willReturnCallback(
                fn (TimeBackoff $timeBackoff) => $this->cache[$timeBackoff->getId()] = $timeBackoff
            );
        $cacheStorage
            ->method('delete')
            ->willReturnCallback(
                function Dstring $id): void {
                    unset($this->cache[$id]);
                }
            );

        $factory = new RateLimiterFactory(
            $this->config,
            $cacheStorage,
            $systemConfig,
            $this->createMock(LockFactory::class),
        );

        return $factory->create('example');
    }
}
'id' => 'test_limit',
            'policy' => 'time_backoff',
            'reset' => '5 minutes',
            'limits' => [
                [
                    'limit' => 3,
                    'interval' => '10 seconds',
                ],
            ],
        ];

        $factory = new RateLimiterFactory(
            $config,
            new CacheStorage(new ArrayAdapter()),
            $this->createMock(SystemConfigService::class),
            $this->createMock(LockFactory::class),
        );

        static::assertInstanceOf(NoLimiter::class$factory->create('example'));
    }

    public function testRateLimitNewsletterForm(): void
    {
        
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;

class RateLimiterFactoryTest extends TestCase
{
    /** * @dataProvider validConfigProvider */
    public function testValidConfig(string $expectedClass, array $config)
    {
        $factory = new RateLimiterFactory($confignew InMemoryStorage());
        $rateLimiter = $factory->create('key');
        $this->assertInstanceOf($expectedClass$rateLimiter);
    }

    public static function validConfigProvider()
    {
        yield [TokenBucketLimiter::class[
            'policy' => 'token_bucket',
            'id' => 'test',
            'limit' => 5,
            'rate' => [
                
/** * @internal * * @covers \Shopware\Core\Framework\RateLimiter\RateLimiterFactory */
class RateLimiterFactoryTest extends TestCase
{
    use IntegrationTestBehaviour;

    public function testFactoryShouldReturnCustomPolicy(): void
    {
        $factory = new RateLimiterFactory(
            [
                'enabled' => true,
                'id' => 'test_limiter',
                'policy' => 'time_backoff',
                'reset' => '1 hour',
                'limits' => [
                    [
                        'limit' => 3,
                        'interval' => '10 seconds',
                    ],
                    [
                        
$this->createFactory([
            'id' => 'test',
            'policy' => 'fixed_window',
            'limit' => 10,
            'interval' => '1 minut',
        ]);
    }

    private function createFactory(array $options)
    {
        return new RateLimiterFactory($options$this->createMock(StorageInterface::class)$this->createMock(LockFactory::class));
    }
}
$eventDispatcher = new EventDispatcher();
        $eventDispatcher->addSubscriber(new StopWorkerOnMessageLimitListener(2));

        $rateLimitCount = 0;
        $listener = function DWorkerRateLimitedEvent $event) use (&$rateLimitCount) {
            ++$rateLimitCount;
            $event->getLimiter()->reset(); // Reset limiter to continue test         };
        $eventDispatcher->addListener(WorkerRateLimitedEvent::class$listener);

        $rateLimitFactory = new RateLimiterFactory([
            'id' => 'bus',
            'policy' => 'fixed_window',
            'limit' => 1,
            'interval' => '1 minute',
        ]new InMemoryStorage());

        $worker = new Worker(['bus' => $receiver]$bus$eventDispatcher, null, ['bus' => $rateLimitFactory]new MockClock());
        $worker->run();

        $this->assertCount(2, $receiver->getAcknowledgedEnvelopes());
        $this->assertEquals(1, $rateLimitCount);
    }
use Symfony\Component\Security\Http\RateLimiter\DefaultLoginRateLimiter;

class LoginThrottlingListenerTest extends TestCase
{
    private RequestStack $requestStack;
    private LoginThrottlingListener $listener;

    protected function setUp(): void
    {
        $this->requestStack = new RequestStack();

        $localLimiter = new RateLimiterFactory([
            'id' => 'login',
            'policy' => 'fixed_window',
            'limit' => 3,
            'interval' => '1 minute',
        ]new InMemoryStorage());
        $globalLimiter = new RateLimiterFactory([
            'id' => 'login',
            'policy' => 'fixed_window',
            'limit' => 6,
            'interval' => '1 minute',
        ]new InMemoryStorage());
        
'interval' => '30 seconds',
                ],
                [
                    'limit' => 7,
                    'interval' => '60 seconds',
                ],
            ],
        ];

        $this->id = $this->config['id'] . '-test';

        $factory = new RateLimiterFactory(
            $this->config,
            new CacheStorage(new ArrayAdapter()),
            $this->createMock(SystemConfigService::class),
            $this->createMock(LockFactory::class)
        );

        $this->limiter = $factory->create('example');
        $this->limiter->reset();

        $this->getContainer()->get('cache.rate_limiter')->clear();
    }

    
Home | Imprint | This part of the site doesn't use cookies.