From fc89b2d50d52df3b7b164ad45c76c5293da43255 Mon Sep 17 00:00:00 2001 From: soyuka Date: Thu, 26 Jan 2023 11:39:48 +0100 Subject: [PATCH] fix(symfony): wrong purger clients type fixes #5365 --- src/HttpCache/SouinPurger.php | 2 +- src/HttpCache/SurrogateKeysPurger.php | 2 +- src/HttpCache/VarnishPurger.php | 2 +- src/HttpCache/VarnishXKeyPurger.php | 2 +- tests/HttpCache/VarnishPurgerTest.php | 12 ++++++++++++ tests/HttpCache/VarnishXKeyPurgerTest.php | 12 ++++++++++++ 6 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/HttpCache/SouinPurger.php b/src/HttpCache/SouinPurger.php index 8877c76c6a3..70b3727b5a7 100644 --- a/src/HttpCache/SouinPurger.php +++ b/src/HttpCache/SouinPurger.php @@ -29,7 +29,7 @@ class SouinPurger extends SurrogateKeysPurger /** * @param HttpClientInterface[] $clients */ - public function __construct(array $clients) + public function __construct(iterable $clients) { parent::__construct($clients, self::MAX_HEADER_SIZE_PER_BATCH, self::HEADER, self::SEPARATOR); } diff --git a/src/HttpCache/SurrogateKeysPurger.php b/src/HttpCache/SurrogateKeysPurger.php index 9f55c95eefa..982c1c76128 100644 --- a/src/HttpCache/SurrogateKeysPurger.php +++ b/src/HttpCache/SurrogateKeysPurger.php @@ -31,7 +31,7 @@ class SurrogateKeysPurger implements PurgerInterface /** * @param HttpClientInterface[] $clients */ - public function __construct(protected readonly array $clients, protected readonly int $maxHeaderLength = self::MAX_HEADER_SIZE_PER_BATCH, protected readonly string $header = self::HEADER, protected readonly string $separator = self::SEPARATOR) + public function __construct(protected readonly iterable $clients, protected readonly int $maxHeaderLength = self::MAX_HEADER_SIZE_PER_BATCH, protected readonly string $header = self::HEADER, protected readonly string $separator = self::SEPARATOR) { } diff --git a/src/HttpCache/VarnishPurger.php b/src/HttpCache/VarnishPurger.php index a8a0167ba5e..6b4ddc2059e 100644 --- a/src/HttpCache/VarnishPurger.php +++ b/src/HttpCache/VarnishPurger.php @@ -29,7 +29,7 @@ final class VarnishPurger implements PurgerInterface /** * @param HttpClientInterface[] $clients */ - public function __construct(private readonly array $clients, int $maxHeaderLength = self::DEFAULT_VARNISH_MAX_HEADER_LENGTH) + public function __construct(private readonly iterable $clients, int $maxHeaderLength = self::DEFAULT_VARNISH_MAX_HEADER_LENGTH) { $this->maxHeaderLength = $maxHeaderLength - mb_strlen(self::REGEXP_PATTERN) + 2; // 2 for %s } diff --git a/src/HttpCache/VarnishXKeyPurger.php b/src/HttpCache/VarnishXKeyPurger.php index e97c90b1bbe..510d335036e 100644 --- a/src/HttpCache/VarnishXKeyPurger.php +++ b/src/HttpCache/VarnishXKeyPurger.php @@ -28,7 +28,7 @@ final class VarnishXKeyPurger extends SurrogateKeysPurger /** * @param HttpClientInterface[] $clients */ - public function __construct(array $clients, int $maxHeaderLength = self::VARNISH_MAX_HEADER_LENGTH, string $xkeyGlue = self::VARNISH_SEPARATOR) + public function __construct(iterable $clients, int $maxHeaderLength = self::VARNISH_MAX_HEADER_LENGTH, string $xkeyGlue = self::VARNISH_SEPARATOR) { parent::__construct($clients, $maxHeaderLength, 'xkey', $xkeyGlue); } diff --git a/tests/HttpCache/VarnishPurgerTest.php b/tests/HttpCache/VarnishPurgerTest.php index faca342dc91..6590084060e 100644 --- a/tests/HttpCache/VarnishPurgerTest.php +++ b/tests/HttpCache/VarnishPurgerTest.php @@ -21,6 +21,7 @@ use Prophecy\PhpUnit\ProphecyTrait; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Symfony\Component\DependencyInjection\Argument\RewindableGenerator; use Symfony\Contracts\HttpClient\HttpClientInterface; /** @@ -175,4 +176,15 @@ public function provideChunkHeaderCases(): \Generator ], ]; } + + public function testConstructor(): void + { + $clientProphecy = $this->prophesize(HttpClientInterface::class); + $clientProphecy->request('BAN', '', ['headers' => ['ApiPlatform-Ban-Regex' => '(/foo)($|\,)']])->shouldBeCalled(); + $purger = new VarnishPurger(new RewindableGenerator(static function () use ($clientProphecy) { + yield $clientProphecy->reveal(); + }, 1)); + + $purger->purge(['/foo']); + } } diff --git a/tests/HttpCache/VarnishXKeyPurgerTest.php b/tests/HttpCache/VarnishXKeyPurgerTest.php index 96e380752b9..ec9e3e0dc02 100644 --- a/tests/HttpCache/VarnishXKeyPurgerTest.php +++ b/tests/HttpCache/VarnishXKeyPurgerTest.php @@ -21,6 +21,7 @@ use Prophecy\PhpUnit\ProphecyTrait; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; +use Symfony\Component\DependencyInjection\Argument\RewindableGenerator; use Symfony\Contracts\HttpClient\HttpClientInterface; /** @@ -205,4 +206,15 @@ public function provideChunkHeaderCases(): \Generator ], ]; } + + public function testConstructor(): void + { + $clientProphecy = $this->prophesize(ClientInterface::class); + $clientProphecy->request('PURGE', '', ['headers' => ['xkey' => '/foo']])->willReturn(new Response())->shouldBeCalled(); + $purger = new VarnishXKeyPurger(new RewindableGenerator(static function () use ($clientProphecy) { + yield $clientProphecy->reveal(); + }, 1)); + + $purger->purge(['/foo']); + } }