Skip to content

Commit

Permalink
[HttpFoundation] Add tests for uncovered sections
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Jul 26, 2024
1 parent 9249ad7 commit 9c375b2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Tests/InputBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,22 @@ public function testFilterArrayWithoutArrayFlagIsDeprecated()
$this->expectDeprecation('Since symfony/http-foundation 5.1: Filtering an array value with "Symfony\Component\HttpFoundation\InputBag::filter()" without passing the FILTER_REQUIRE_ARRAY or FILTER_FORCE_ARRAY flag is deprecated');
$bag->filter('foo', \FILTER_VALIDATE_INT);
}

public function testAdd()
{
$bag = new InputBag(['foo' => 'bar']);
$bag->add(['baz' => 'qux']);

$this->assertSame('bar', $bag->get('foo'), '->add() does not remove existing parameters');
$this->assertSame('qux', $bag->get('baz'), '->add() adds new parameters');
}

public function testReplace()
{
$bag = new InputBag(['foo' => 'bar']);
$bag->replace(['baz' => 'qux']);

$this->assertNull($bag->get('foo'), '->replace() removes existing parameters');
$this->assertSame('qux', $bag->get('baz'), '->replace() adds new parameters');
}
}
29 changes: 29 additions & 0 deletions Tests/RateLimiter/AbstractRequestRateLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Component\RateLimiter\Policy\NoLimiter;
use Symfony\Component\RateLimiter\RateLimit;

class AbstractRequestRateLimiterTest extends TestCase
Expand All @@ -33,6 +34,34 @@ public function testConsume(array $rateLimits, ?RateLimit $expected)
$this->assertSame($expected, $rateLimiter->consume(new Request()));
}

public function testConsumeWithoutLimiterAddsSpecialNoLimiter()
{
$rateLimiter = new MockAbstractRequestRateLimiter([]);

try {
$this->assertSame(\PHP_INT_MAX, $rateLimiter->consume(new Request())->getLimit());
} catch (\TypeError $error) {
if (str_contains($error->getMessage(), 'RateLimit::__construct(): Argument #1 ($availableTokens) must be of type int, float given')) {
$this->markTestSkipped('This test cannot be run on a version of the RateLimiter component that uses \INF instead of \PHP_INT_MAX in NoLimiter.');
}

throw $error;
}
}

public function testResetLimiters()
{
$rateLimiter = new MockAbstractRequestRateLimiter([
$limiter1 = $this->createMock(LimiterInterface::class),
$limiter2 = $this->createMock(LimiterInterface::class),
]);

$limiter1->expects($this->once())->method('reset');
$limiter2->expects($this->once())->method('reset');

$rateLimiter->reset(new Request());
}

public static function provideRateLimits()
{
$now = new \DateTimeImmutable();
Expand Down

0 comments on commit 9c375b2

Please sign in to comment.