From 74fb8670d2e92493d9eca582178d57991161797b Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Mon, 25 Mar 2024 12:13:29 +0100 Subject: [PATCH] remove obsolete legacy support no longer needed --- src/SymfonyCache/RefreshListener.php | 7 +- .../PHPUnit/AbstractCacheConstraint.php | 22 ------ src/Test/PHPUnit/AbstractCacheConstraint.php | 61 ++++++++++++--- .../PHPUnit/AbstractCacheConstraintTrait.php | 74 ------------------- 4 files changed, 50 insertions(+), 114 deletions(-) delete mode 100644 src/Test/Legacy/PHPUnit/AbstractCacheConstraint.php delete mode 100644 src/Test/PHPUnit/AbstractCacheConstraintTrait.php diff --git a/src/SymfonyCache/RefreshListener.php b/src/SymfonyCache/RefreshListener.php index 995638e3b..36ef91aab 100644 --- a/src/SymfonyCache/RefreshListener.php +++ b/src/SymfonyCache/RefreshListener.php @@ -39,12 +39,7 @@ public static function getSubscribedEvents(): array public function handleRefresh(CacheEvent $event): void { $request = $event->getRequest(); - // BC - we can drop this check when we only support Symfony 3.1 and newer - $cacheable = method_exists(Request::class, 'isMethodCacheable') - ? $request->isMethodCacheable() - : $request->isMethodSafe(false); - - if (!$cacheable + if (!$request->isMethodCacheable() || !$request->isNoCache() || !$this->isRequestAllowed($request) ) { diff --git a/src/Test/Legacy/PHPUnit/AbstractCacheConstraint.php b/src/Test/Legacy/PHPUnit/AbstractCacheConstraint.php deleted file mode 100644 index 5f6a9967a..000000000 --- a/src/Test/Legacy/PHPUnit/AbstractCacheConstraint.php +++ /dev/null @@ -1,22 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\HttpCache\Test\Legacy\PHPUnit; - -use FOS\HttpCache\Test\PHPUnit\AbstractCacheConstraintTrait; - -/** - * Abstract cache constraint. - */ -abstract class AbstractCacheConstraint extends \PHPUnit_Framework_Constraint -{ - use AbstractCacheConstraintTrait; -} diff --git a/src/Test/PHPUnit/AbstractCacheConstraint.php b/src/Test/PHPUnit/AbstractCacheConstraint.php index 10dde724e..c61a81020 100644 --- a/src/Test/PHPUnit/AbstractCacheConstraint.php +++ b/src/Test/PHPUnit/AbstractCacheConstraint.php @@ -12,19 +12,56 @@ namespace FOS\HttpCache\Test\PHPUnit; use PHPUnit\Framework\Constraint\Constraint; +use Psr\Http\Message\ResponseInterface; -if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) { - /* - * Using an early return instead of a else does not work when using the PHPUnit phar due to some weird PHP behavior - * (the class gets defined without executing the code before it and so the definition is not properly conditional) - */ - class_alias('FOS\HttpCache\Test\Legacy\PHPUnit\AbstractCacheConstraint', 'FOS\HttpCache\Test\PHPUnit\AbstractCacheConstraint'); -} else { - /** - * Abstract cache constraint. - */ - abstract class AbstractCacheConstraint extends Constraint +abstract class AbstractCacheConstraint extends Constraint +{ + protected string $header; + + public function __construct(string $header = 'X-Cache') + { + $this->header = $header; + } + + public function matches($other): bool + { + if (!$other instanceof ResponseInterface) { + throw new \InvalidArgumentException('compare must compare with '.ResponseInterface::class.' got '.get_debug_type($other)); + } + if (!$other->hasHeader($this->header)) { + $message = sprintf( + 'Response has no "%s" header. Configure your caching proxy ' + .'to set the header with cache hit/miss status.', + $this->header + ); + if (200 !== $other->getStatusCode()) { + $message .= sprintf("\nStatus code of response is %s.", $other->getStatusCode()); + } + + $message .= "\nThe response headers are:\n\n"; + + foreach ($other->getHeaders() as $name => $values) { + foreach ($values as $value) { + $message .= $name.': '.$value."\n"; + } + } + + $body = $other->getBody(); + $body->rewind(); + $message .= sprintf("\nThe response body is:\n\n %s", $body->getContents()); + + throw new \RuntimeException($message); + } + + return str_contains($other->getHeaderLine($this->header), $this->getValue()); + } + + public function failureDescription($other): string { - use AbstractCacheConstraintTrait; + return sprintf( + 'response (with status code %s) %s', + $other->getStatusCode(), + $this->toString() + ); } } diff --git a/src/Test/PHPUnit/AbstractCacheConstraintTrait.php b/src/Test/PHPUnit/AbstractCacheConstraintTrait.php deleted file mode 100644 index db18ef45b..000000000 --- a/src/Test/PHPUnit/AbstractCacheConstraintTrait.php +++ /dev/null @@ -1,74 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\HttpCache\Test\PHPUnit; - -use PHPUnit\Runner\Version; -use Psr\Http\Message\ResponseInterface; - -/** - * This trait is used to have the same code and behavior between AbstractCacheConstraint and its legacy version. - */ -trait AbstractCacheConstraintTrait -{ - protected string $header; - - public function __construct(string $header = 'X-Cache') - { - $this->header = $header; - - if (version_compare(Version::id(), '8.0.0', '<')) { - parent::__construct(); - } - } - - public function matches($other): bool - { - if (!$other instanceof ResponseInterface) { - throw new \InvalidArgumentException('compare must compare with '.ResponseInterface::class.' got '.get_debug_type($other)); - } - if (!$other->hasHeader($this->header)) { - $message = sprintf( - 'Response has no "%s" header. Configure your caching proxy ' - .'to set the header with cache hit/miss status.', - $this->header - ); - if (200 !== $other->getStatusCode()) { - $message .= sprintf("\nStatus code of response is %s.", $other->getStatusCode()); - } - - $message .= "\nThe response headers are:\n\n"; - - foreach ($other->getHeaders() as $name => $values) { - foreach ($values as $value) { - $message .= $name.': '.$value."\n"; - } - } - - $body = $other->getBody(); - $body->rewind(); - $message .= sprintf("\nThe response body is:\n\n %s", $body->getContents()); - - throw new \RuntimeException($message); - } - - return str_contains($other->getHeaderLine($this->header), $this->getValue()); - } - - public function failureDescription($other): string - { - return sprintf( - 'response (with status code %s) %s', - $other->getStatusCode(), - $this->toString() - ); - } -}