Skip to content

[RateLimiter] final class Factory does not implement an interface #46644

Closed as not planned
@nathansalter

Description

@nathansalter

Description

A fairly small issue, but when using the RateLimiter component, it's impossible to build UnitTests as the Symfony\Component\RateLimiter\RateLimiterFactory class is marked as final but doesn't implement an interface. This can be mitigated with reflection, but causes a poor developer experience. As there is also no way to inject a new type of limiter into this factory, this becomes non-trivial to implement a unit test, as you have to mock both the StorageInterface and LockFactory classes, inspecting how they are used in the standard implementations.

Simple fix: Add a RateLimiterFactoryInterface
More complete fix: Add a method in which to inject new types of rate limiter into the Factory. This would allow for alternative rate limiting strategies such as the Leaky Bucket which currently doesn't have an implementation.

Example

Running this code:

<?php
class RateLimitFactoryTest extends TestCase
{
    /** @var RateLimiterFactory|MockObject */
    private RateLimiterFactory $globalLimiter;
    protected function testLimitFactory()
    {
        $this->createMock(RateLimiterFactory::class);
    }
}

Throws an exception because the class is final.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions