From f5f8189afe2991b761c3a8a180b147e0c9d99825 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 30 Nov 2023 00:32:52 +0100 Subject: [PATCH 1/3] Allow installation with Symfony 7 --- composer.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index a9a64adb..8f369e37 100644 --- a/composer.json +++ b/composer.json @@ -22,8 +22,8 @@ ], "require": { "php": "^7.2 || ^8.0", - "symfony/event-dispatcher": "^4.3 || ^5.0 || ^6.0", - "symfony/options-resolver": "^4.3 || ^5.0 || ^6.0", + "symfony/event-dispatcher": "^4.3 || ^5.0 || ^6.0 || ^7.0", + "symfony/options-resolver": "^4.3 || ^5.0 || ^6.0 || ^7.0", "php-http/client-implementation": "^1.0 || ^2.0", "php-http/client-common": "^1.1.0 || ^2.0", "php-http/message": "^1.0 || ^2.0", @@ -35,9 +35,9 @@ "monolog/monolog": "^1.0", "php-http/guzzle7-adapter": "^0.1.1", "php-http/mock-client": "^1.2", - "symfony/process": "^4.3 || ^5.0 || ^6.0", - "symfony/http-kernel": "^4.3 || ^5.0 || ^6.0", - "symfony/phpunit-bridge": "^5.0 || ^6.0" + "symfony/process": "^4.3 || ^5.0 || ^6.0|| ^7.0", + "symfony/http-kernel": "^4.3 || ^5.0 || ^6.0|| ^7.0", + "symfony/phpunit-bridge": "^5.0 || ^6.0|| ^7.0" }, "conflict": { "toflar/psr6-symfony-http-cache-store": "<2.2.1" From 0761a0b164f99a9f11f57e4a7f875f95a07f995d Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Thu, 30 Nov 2023 00:35:53 +0100 Subject: [PATCH 2/3] Increase test matrix --- .github/workflows/ci.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b2fcb56e..6380f3da 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,6 +33,16 @@ jobs: - php: '8.1' varnish-version: '7.1' varnish-modules-version: '0.20.0' + - php: '8.2' + symfony-version: '7.*' + - php: '8.2' + varnish-version: '7.1' + varnish-modules-version: '0.20.0' + - php: '8.3' + symfony-version: '7.*' + - php: '8.3' + varnish-version: '7.1' + varnish-modules-version: '0.20.0' steps: - name: Setup PHP From 98737deb839edf531e710e00920be9ad6026baa2 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Mon, 11 Dec 2023 11:42:02 +0100 Subject: [PATCH 3/3] adjust for symfony 7 --- .github/workflows/ci.yml | 68 +++---------- .github/workflows/static.yml | 6 +- CHANGELOG.md | 15 +++ composer.json | 16 +-- src/SymfonyCache/AccessControlledListener.php | 13 +-- src/SymfonyCache/CacheEvent.php | 40 +++----- .../EventDispatchingHttpCache.php | 26 ++--- src/SymfonyCache/KernelDispatcher.php | 15 ++- .../EventDispatchingHttpCacheTestCase.php | 99 +++++++++---------- .../Functional/Fixtures/Symfony/AppKernel.php | 2 +- .../Symfony/EventDispatchingHttpCacheTest.php | 2 +- tests/Unit/SymfonyCache/CacheEventTest.php | 2 +- tests/Unit/SymfonyCache/PurgeListenerTest.php | 13 +-- .../SymfonyCache/PurgeTagsListenerTest.php | 9 +- .../Unit/SymfonyCache/RefreshListenerTest.php | 6 +- 15 files changed, 122 insertions(+), 210 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6380f3da..54af1ca1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,12 +22,8 @@ jobs: matrix: varnish-version: ['6.6'] varnish-modules-version: ['0.18.0'] - php: ['7.3', '7.4', '8.0', '8.1'] + php: ['8.1', '8.2', '8.3'] include: - - php: '7.4' - symfony-version: '4.3.*' - - php: '7.4' - symfony-version: '5.0.*' - php: '8.1' symfony-version: '6.*' - php: '8.1' @@ -66,42 +62,9 @@ jobs: run: | composer require --no-update ${DEPENDENCIES} composer update --prefer-dist --no-interaction --no-progress - vendor/bin/simple-phpunit install - name: Execute tests - run: vendor/bin/simple-phpunit -v - - legacyPHP: - name: PHP 7.2 Varnish 6 - runs-on: ubuntu-20.04 - env: - VARNISH_VERSION: '6.6' - VARNISH_MODULES_VERSION: '0.18.0' - - steps: - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: 7.2 - tools: composer:v2 - coverage: none - - - name: Checkout code - uses: actions/checkout@v2 - - - name: Setup Varnish and Nginx - run: | - sh ${GITHUB_WORKSPACE}/.github/workflows/setup-varnish.sh - sh ${GITHUB_WORKSPACE}/.github/workflows/setup-nginx.sh - - - name: Install composer dependencies - run: | - composer require --no-update ${DEPENDENCIES} - composer update --prefer-dist --no-interaction --no-progress - vendor/bin/simple-phpunit install - - - name: Execute tests - run: vendor/bin/simple-phpunit -v + run: vendor/bin/phpunit varnish5: name: PHP ${{ matrix.php }} Legacy Varnish 5 @@ -114,7 +77,7 @@ jobs: fail-fast: false matrix: include: - - php: '7.4' + - php: '8.1' steps: - name: Setup PHP @@ -135,10 +98,9 @@ jobs: - name: Install composer dependencies run: | composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress - vendor/bin/simple-phpunit install - name: Execute tests - run: vendor/bin/simple-phpunit -v + run: vendor/bin/phpunit varnish4: name: PHP ${{ matrix.php }} Legacy Varnish 4 @@ -151,7 +113,7 @@ jobs: fail-fast: false matrix: include: - - php: '7.4' + - php: '8.1' steps: - name: Setup PHP @@ -172,10 +134,9 @@ jobs: - name: Install composer dependencies run: | composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress - vendor/bin/simple-phpunit install - name: Execute tests - run: vendor/bin/simple-phpunit -v + run: vendor/bin/phpunit lowest: name: PHP ${{ matrix.php }} Lowest, Varnish 3 @@ -188,7 +149,7 @@ jobs: strategy: fail-fast: false matrix: - php: ['7.2'] + php: ['8.1'] steps: - name: Setup PHP @@ -209,10 +170,9 @@ jobs: - name: Install composer dependencies run: | composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress - vendor/bin/simple-phpunit install - name: Execute tests - run: vendor/bin/simple-phpunit -v + run: vendor/bin/phpunit coverage: name: Code Coverage @@ -225,7 +185,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 7.4 + php-version: 8.2 tools: composer:v2 coverage: xdebug @@ -239,14 +199,8 @@ jobs: - name: Install dependencies run: | - composer require "friends-of-phpspec/phpspec-code-coverage:^4.3.2" --no-interaction --no-update + composer require "friends-of-phpspec/phpspec-code-coverage:^6.3.0" --no-interaction --no-update composer update --prefer-dist --no-interaction --no-progress - vendor/bin/simple-phpunit install - name: Execute tests - run: vendor/bin/simple-phpunit -v --coverage-text --coverage-clover build/coverage.xml - - - name: Upload coverage - run: | - wget https://scrutinizer-ci.com/ocular.phar - php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml --revision=${{ github.event.pull_request.head.sha || github.sha }} + run: vendor/bin/phpunit --coverage-text --coverage-clover build/coverage.xml diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 5d9c92cd..9c3a4ce5 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -10,15 +10,13 @@ jobs: phpstan-src: name: PHPStan src runs-on: ubuntu-latest - env: - REQUIRE_DEV: "true" steps: - name: Checkout code uses: actions/checkout@v2 - name: Pull in optional dependencies - run: composer require --no-update phpunit/phpunit toflar/psr6-symfony-http-cache-store:^3.0 + run: composer require --no-update phpunit/phpunit toflar/psr6-symfony-http-cache-store:^4.2 - name: PHPStan uses: docker://oskarstark/phpstan-ga @@ -36,7 +34,7 @@ jobs: uses: actions/checkout@v2 - name: Pull in optional dependencies - run: composer require --no-update phpunit/phpunit toflar/psr6-symfony-http-cache-store:^3.0 + run: composer require --no-update phpunit/phpunit toflar/psr6-symfony-http-cache-store:^4.2 - name: PHPStan uses: docker://oskarstark/phpstan-ga diff --git a/CHANGELOG.md b/CHANGELOG.md index e388e6bf..8f89bf82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,21 @@ Changelog See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpCache/releases). +3.x +=== + +3.0.0 (unreleased) +----- + +* Support Symfony 7 +* Drop support for Symfony < 6.4 +* Test with PHP 8.2 and 8.3 +* Drop support for PHP < 8.1 +* Parameter and return type declarations where possible. + +2.x +=== + 2.15.3 ------ diff --git a/composer.json b/composer.json index 8f369e37..273053e3 100644 --- a/composer.json +++ b/composer.json @@ -21,9 +21,9 @@ } ], "require": { - "php": "^7.2 || ^8.0", - "symfony/event-dispatcher": "^4.3 || ^5.0 || ^6.0 || ^7.0", - "symfony/options-resolver": "^4.3 || ^5.0 || ^6.0 || ^7.0", + "php": "^8.1", + "symfony/event-dispatcher": "^6.4 || ^7.0", + "symfony/options-resolver": "^6.4 || ^7.0", "php-http/client-implementation": "^1.0 || ^2.0", "php-http/client-common": "^1.1.0 || ^2.0", "php-http/message": "^1.0 || ^2.0", @@ -31,13 +31,13 @@ "php-http/discovery": "^1.12" }, "require-dev": { - "mockery/mockery": "^1.3.1", + "mockery/mockery": "^1.6.0", "monolog/monolog": "^1.0", "php-http/guzzle7-adapter": "^0.1.1", - "php-http/mock-client": "^1.2", - "symfony/process": "^4.3 || ^5.0 || ^6.0|| ^7.0", - "symfony/http-kernel": "^4.3 || ^5.0 || ^6.0|| ^7.0", - "symfony/phpunit-bridge": "^5.0 || ^6.0|| ^7.0" + "php-http/mock-client": "^1.6.0", + "symfony/process": "^6.4|| ^7.0", + "symfony/http-kernel": "^6.4|| ^7.0", + "phpunit/phpunit": "^9" }, "conflict": { "toflar/psr6-symfony-http-cache-store": "<2.2.1" diff --git a/src/SymfonyCache/AccessControlledListener.php b/src/SymfonyCache/AccessControlledListener.php index c3ef3d5e..faa497a4 100644 --- a/src/SymfonyCache/AccessControlledListener.php +++ b/src/SymfonyCache/AccessControlledListener.php @@ -13,7 +13,6 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\RequestMatcher; use Symfony\Component\HttpFoundation\RequestMatcher\IpsRequestMatcher; use Symfony\Component\HttpFoundation\RequestMatcherInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -26,10 +25,7 @@ */ abstract class AccessControlledListener implements EventSubscriberInterface { - /** - * @var RequestMatcherInterface - */ - private $clientMatcher; + private RequestMatcherInterface $clientMatcher; /** * When creating this event listener, you can configure a number of options. @@ -38,7 +34,7 @@ abstract class AccessControlledListener implements EventSubscriberInterface * - client_ips: IP or array of IPs of clients that are allowed to send requests. * * Only one of request matcher or IPs may be a non-null value. If you use a - * RequestMatcher, configure your IPs into it. + * RequestMatcherInterface, include an IpsRequestMatcher with your IPs. * * If neither parameter is set, the filter is IP 127.0.0.1 * @@ -55,10 +51,7 @@ public function __construct(array $options = []) throw new \InvalidArgumentException('You may not set both a request matcher and an IP.'); } if (!$clientMatcher) { - $clientMatcher = class_exists(IpsRequestMatcher::class) - ? new IpsRequestMatcher($options['client_ips'] ?: '127.0.0.1') - : new RequestMatcher(null, null, null, $options['client_ips'] ?: '127.0.0.1') - ; + $clientMatcher = new IpsRequestMatcher($options['client_ips'] ?: '127.0.0.1'); } $this->clientMatcher = $clientMatcher; diff --git a/src/SymfonyCache/CacheEvent.php b/src/SymfonyCache/CacheEvent.php index 7a9e02be..c6fb7693 100644 --- a/src/SymfonyCache/CacheEvent.php +++ b/src/SymfonyCache/CacheEvent.php @@ -23,25 +23,13 @@ */ class CacheEvent extends BaseEvent { - /** - * @var CacheInvalidation - */ - private $kernel; + private CacheInvalidation $kernel; - /** - * @var Request - */ - private $request; + private Request $request; - /** - * @var Response - */ - private $response; + private ?Response $response; - /** - * @var int - */ - private $requestType; + private int $requestType; /** * Make sure your $kernel implements CacheInvalidationInterface. @@ -49,9 +37,9 @@ class CacheEvent extends BaseEvent * @param CacheInvalidation $kernel the kernel raising with this event * @param Request $request the request being processed * @param Response|null $response the response, if available - * @param int $requestType the request type (default HttpKernelInterface::MASTER_REQUEST) + * @param int $requestType the request type (default HttpKernelInterface::MAIN_REQUEST) */ - public function __construct(CacheInvalidation $kernel, Request $request, Response $response = null, $requestType = HttpKernelInterface::MASTER_REQUEST) + public function __construct(CacheInvalidation $kernel, Request $request, Response $response = null, int $requestType = HttpKernelInterface::MAIN_REQUEST) { $this->kernel = $kernel; $this->request = $request; @@ -71,20 +59,16 @@ public function getKernel() /** * Get the request that is being processed. - * - * @return Request */ - public function getRequest() + public function getRequest(): Request { return $this->request; } /** - * One of the constants HttpKernelInterface::MASTER_REQUEST or SUB_REQUEST. - * - * @return int + * One of the constants HttpKernelInterface::MAIN_REQUEST or SUB_REQUEST. */ - public function getRequestType() + public function getRequestType(): int { return $this->requestType; } @@ -92,10 +76,8 @@ public function getRequestType() /** * Events that occur after the response is created provide the default response. * Event listeners can also set the response to make it available here. - * - * @return Response|null the response if one was set */ - public function getResponse() + public function getResponse(): ?Response { return $this->response; } @@ -105,7 +87,7 @@ public function getResponse() * * Setting a response stops propagation of the event to further event handlers. */ - public function setResponse(Response $response) + public function setResponse(Response $response): void { $this->response = $response; diff --git a/src/SymfonyCache/EventDispatchingHttpCache.php b/src/SymfonyCache/EventDispatchingHttpCache.php index adba06eb..3c06023a 100644 --- a/src/SymfonyCache/EventDispatchingHttpCache.php +++ b/src/SymfonyCache/EventDispatchingHttpCache.php @@ -40,17 +40,9 @@ */ trait EventDispatchingHttpCache { - /** - * @var EventDispatcherInterface - */ - private $eventDispatcher; + private ?EventDispatcherInterface $eventDispatcher = null; - /** - * Get event dispatcher. - * - * @return EventDispatcherInterface - */ - public function getEventDispatcher() + public function getEventDispatcher(): EventDispatcherInterface { if (!$this->eventDispatcher) { $this->eventDispatcher = new EventDispatcher(); @@ -64,7 +56,7 @@ public function getEventDispatcher() * * @see EventDispatcherInterface::addSubscriber */ - public function addSubscriber(EventSubscriberInterface $subscriber) + public function addSubscriber(EventSubscriberInterface $subscriber): void { $this->getEventDispatcher()->addSubscriber($subscriber); } @@ -74,7 +66,7 @@ public function addSubscriber(EventSubscriberInterface $subscriber) * * @see EventDispatcherInterface::addListener */ - public function addListener($eventName, $listener, $priority = 0) + public function addListener($eventName, $listener, $priority = 0): void { $this->getEventDispatcher()->addListener($eventName, $listener, $priority); } @@ -84,7 +76,7 @@ public function addListener($eventName, $listener, $priority = 0) * * Adding the Events::PRE_HANDLE and Events::POST_HANDLE events. */ - public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response + public function handle(Request $request, $type = HttpKernelInterface::MAIN_REQUEST, $catch = true): Response { // trigger loading the CacheEvent to avoid fatal error when HttpKernel::loadClassCache is used. class_exists(CacheEvent::class); @@ -102,10 +94,8 @@ class_exists(CacheEvent::class); * {@inheritdoc} * * Trigger event to alter response before storing it in the cache. - * - * @return void */ - protected function store(Request $request, Response $response) + protected function store(Request $request, Response $response): void { $response = $this->dispatch(Events::PRE_STORE, $request, $response); @@ -131,11 +121,11 @@ protected function invalidate(Request $request, $catch = false): Response * * @param string $name Name of the event to trigger. One of the constants in FOS\HttpCache\SymfonyCache\Events * @param Response|null $response If already available - * @param int $requestType The request type (default HttpKernelInterface::MASTER_REQUEST) + * @param int $requestType The request type (default HttpKernelInterface::MAIN_REQUEST) * * @return Response|null The response to return, which might be provided/altered by a listener */ - protected function dispatch($name, Request $request, Response $response = null, $requestType = HttpKernelInterface::MASTER_REQUEST): ?Response + protected function dispatch(string $name, Request $request, Response $response = null, int $requestType = HttpKernelInterface::MAIN_REQUEST): ?Response { if ($this->getEventDispatcher()->hasListeners($name)) { $event = new CacheEvent($this, $request, $response, $requestType); diff --git a/src/SymfonyCache/KernelDispatcher.php b/src/SymfonyCache/KernelDispatcher.php index 16e96222..789adf24 100644 --- a/src/SymfonyCache/KernelDispatcher.php +++ b/src/SymfonyCache/KernelDispatcher.php @@ -32,22 +32,19 @@ */ class KernelDispatcher implements Dispatcher { - /** - * @var HttpCacheProvider - */ - private $httpCacheProvider; + private HttpCacheProvider $httpCacheProvider; /** - * @var array + * @var Request[] */ - private $queue = []; + private array $queue = []; public function __construct(HttpCacheProvider $httpCacheProvider) { $this->httpCacheProvider = $httpCacheProvider; } - public function invalidate(RequestInterface $invalidationRequest, $validateHost = true) + public function invalidate(RequestInterface $invalidationRequest, $validateHost = true): void { $request = Request::create( $invalidationRequest->getUri(), @@ -82,7 +79,7 @@ public function invalidate(RequestInterface $invalidationRequest, $validateHost $this->queue[sha1($request)] = $request; } - public function flush() + public function flush(): int { if (!count($this->queue)) { return 0; @@ -98,7 +95,7 @@ public function flush() foreach ($queue as $request) { try { - $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, false); + $httpCache->handle($request, HttpKernelInterface::MAIN_REQUEST, false); } catch (\Exception $e) { $exceptions->add($e); } diff --git a/src/Test/EventDispatchingHttpCacheTestCase.php b/src/Test/EventDispatchingHttpCacheTestCase.php index d4a77aa6..50e09360 100644 --- a/src/Test/EventDispatchingHttpCacheTestCase.php +++ b/src/Test/EventDispatchingHttpCacheTestCase.php @@ -110,13 +110,12 @@ public function testHandleCalled() $testListener = new TestListener($this, $httpCache, $request); $httpCache->addSubscriber($testListener); $httpCache - ->expects($this->any()) ->method('lookup') ->with($request) - ->will($this->returnValue($response)) + ->willReturn($response) ; - $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); + $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MAIN_REQUEST, $catch)); $this->assertEquals(1, $testListener->preHandleCalls); $this->assertEquals(1, $testListener->postHandleCalls); } @@ -126,9 +125,8 @@ public function testHandleCalled() * * @depends testHandleCalled */ - public function testPreHandleReturnEarly() + public function testPreHandleReturnEarly(): void { - $catch = true; $request = Request::create('/foo', 'GET'); $response = new Response(); @@ -141,7 +139,7 @@ public function testPreHandleReturnEarly() ->method('lookup') ; - $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); + $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MAIN_REQUEST)); $this->assertEquals(1, $testListener->preHandleCalls); $this->assertEquals(1, $testListener->postHandleCalls); } @@ -151,7 +149,7 @@ public function testPreHandleReturnEarly() * * @depends testHandleCalled */ - public function testPostHandleReturn() + public function testPostHandleReturn(): void { $catch = true; $request = Request::create('/foo', 'GET'); @@ -163,13 +161,12 @@ public function testPostHandleReturn() $testListener->postHandleResponse = $postResponse; $httpCache->addSubscriber($testListener); $httpCache - ->expects($this->any()) ->method('lookup') ->with($request) - ->will($this->returnValue($regularResponse)) + ->willReturn($regularResponse) ; - $this->assertSame($postResponse, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); + $this->assertSame($postResponse, $httpCache->handle($request, HttpKernelInterface::MAIN_REQUEST, $catch)); $this->assertEquals(1, $testListener->preHandleCalls); $this->assertEquals(1, $testListener->postHandleCalls); } @@ -196,7 +193,7 @@ public function testPostHandleAfterPreHandle() ->method('lookup') ; - $this->assertSame($postResponse, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch)); + $this->assertSame($postResponse, $httpCache->handle($request, HttpKernelInterface::MAIN_REQUEST, $catch)); $this->assertEquals(1, $testListener->preHandleCalls); $this->assertEquals(1, $testListener->postHandleCalls); } @@ -258,10 +255,9 @@ public function testPreInvalidateCalled() $testListener = new TestListener($this, $httpCache, $request); $httpCache->addSubscriber($testListener); $httpCache - ->expects($this->any()) ->method('pass') ->with($request) - ->will($this->returnValue($response)) + ->willReturn($response) ; $refHttpCache = new \ReflectionObject($httpCache); $method = $refHttpCache->getMethod('invalidate'); @@ -276,7 +272,7 @@ public function testPreInvalidateCalled() * * @depends testPreInvalidateCalled */ - public function testPreInvalidateReturnEarly() + public function testPreInvalidateReturnEarly(): void { $catch = true; $request = Request::create('/foo', 'GET'); @@ -298,7 +294,7 @@ public function testPreInvalidateReturnEarly() $this->assertEquals(1, $testListener->preInvalidateCalls); } - public function testAddListener() + public function testAddListener(): void { $request = Request::create('/foo', 'GET'); $response = new Response(); @@ -308,13 +304,12 @@ public function testAddListener() $httpCache->addListener(Events::PRE_HANDLE, [$simpleListener, 'callback']); $httpCache - ->expects($this->any()) ->method('lookup') ->with($request) - ->will($this->returnValue($response)) + ->willReturn($response) ; - $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MASTER_REQUEST)); + $this->assertSame($response, $httpCache->handle($request, HttpKernelInterface::MAIN_REQUEST)); $this->assertEquals(1, $simpleListener->calls); } } @@ -322,59 +317,59 @@ public function testAddListener() class TestListener implements EventSubscriberInterface { /** - * @var int Count how many times preHandle has been called + * Count how many times preHandle has been called. */ - public $preHandleCalls = 0; + public int $preHandleCalls = 0; /** - * @var int Count how many times postHandle has been called + * Count how many times postHandle has been called. */ - public $postHandleCalls = 0; + public int $postHandleCalls = 0; /** - * @var int Count how many times preStore has been called + * Count how many times preStore has been called. */ - public $preStoreCalls = 0; + public int $preStoreCalls = 0; /** - * @var int Count how many times preInvalidate has been called + * Count how many times preInvalidate has been called. */ - public $preInvalidateCalls = 0; + public int $preInvalidateCalls = 0; /** - * @var Response A response to set during the preHandle + * A response to set during the preHandle. */ - public $preHandleResponse; + public ?Response $preHandleResponse = null; /** - * @var Response A response to set during the postHandle + * A response to set during the postHandle. */ - public $postHandleResponse; + public ?Response $postHandleResponse = null; /** - * @var Response A response to set during the preStore + * A response to set during the preStore. */ - public $preStoreResponse; + public ?Response $preStoreResponse = null; /** - * @var Response A response to set during the preInvalidate + * A response to set during the preInvalidate. */ - public $preInvalidateResponse; + public ?Response $preInvalidateResponse = null; /** - * @var EventDispatchingHttpCacheTestCase To do assertions + * To do assertions. */ - private $test; + private EventDispatchingHttpCacheTestCase $test; /** - * @var CacheInvalidation The kernel to ensure the event carries the correct kernel + * The kernel to ensure the event carries the correct kernel. */ - private $kernel; + private CacheInvalidation $kernel; /** - * @var Request The request to ensure the event carries the correct request + * The request to ensure the event carries the correct request. */ - private $request; + private Request $request; public function __construct( EventDispatchingHttpCacheTestCase $test, @@ -396,7 +391,7 @@ public static function getSubscribedEvents(): array ]; } - public function preHandle(CacheEvent $event) + public function preHandle(CacheEvent $event): void { $this->test->assertSame($this->kernel, $event->getKernel()); $this->test->assertSame($this->request, $event->getRequest()); @@ -406,7 +401,7 @@ public function preHandle(CacheEvent $event) ++$this->preHandleCalls; } - public function postHandle(CacheEvent $event) + public function postHandle(CacheEvent $event): void { $this->test->assertSame($this->kernel, $event->getKernel()); $this->test->assertSame($this->request, $event->getRequest()); @@ -416,7 +411,7 @@ public function postHandle(CacheEvent $event) ++$this->postHandleCalls; } - public function preStore(CacheEvent $event) + public function preStore(CacheEvent $event): void { $this->test->assertSame($this->kernel, $event->getKernel()); $this->test->assertSame($this->request, $event->getRequest()); @@ -426,7 +421,7 @@ public function preStore(CacheEvent $event) ++$this->preStoreCalls; } - public function preInvalidate(CacheEvent $event) + public function preInvalidate(CacheEvent $event): void { $this->test->assertSame($this->kernel, $event->getKernel()); $this->test->assertSame($this->request, $event->getRequest()); @@ -439,22 +434,22 @@ public function preInvalidate(CacheEvent $event) class SimpleListener { - public $calls = 0; + public int $calls = 0; /** - * @var EventDispatchingHttpCacheTestCase To do assertions + * To do assertions. */ - private $test; + private EventDispatchingHttpCacheTestCase $test; /** - * @var CacheInvalidation The kernel to ensure the event carries the correct kernel + * The kernel to ensure the event carries the correct kernel. */ - private $kernel; + private CacheInvalidation $kernel; /** - * @var Request The request to ensure the event carries the correct request + * The request to ensure the event carries the correct request. */ - private $request; + private Request $request; public function __construct( EventDispatchingHttpCacheTestCase $test, @@ -466,7 +461,7 @@ public function __construct( $this->request = $request; } - public function callback(CacheEvent $event) + public function callback(CacheEvent $event): void { $this->test->assertSame($this->kernel, $event->getKernel()); $this->test->assertSame($this->request, $event->getRequest()); diff --git a/tests/Functional/Fixtures/Symfony/AppKernel.php b/tests/Functional/Fixtures/Symfony/AppKernel.php index 1e606b5b..5adef220 100644 --- a/tests/Functional/Fixtures/Symfony/AppKernel.php +++ b/tests/Functional/Fixtures/Symfony/AppKernel.php @@ -25,7 +25,7 @@ class AppKernel implements HttpKernelInterface * * {@inheritdoc} */ - public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response + public function handle(Request $request, $type = HttpKernelInterface::MAIN_REQUEST, $catch = true): Response { switch ($request->getPathInfo()) { case '/cache': diff --git a/tests/Functional/Symfony/EventDispatchingHttpCacheTest.php b/tests/Functional/Symfony/EventDispatchingHttpCacheTest.php index f4a0e703..49a606e7 100644 --- a/tests/Functional/Symfony/EventDispatchingHttpCacheTest.php +++ b/tests/Functional/Symfony/EventDispatchingHttpCacheTest.php @@ -43,7 +43,7 @@ public function testEventListeners() $httpKernel = \Mockery::mock(HttpKernelInterface::class) ->shouldReceive('handle') - ->withArgs([$request, HttpKernelInterface::MASTER_REQUEST, true]) + ->withArgs([$request, HttpKernelInterface::MAIN_REQUEST, true]) ->andReturn($expectedResponse) ->getMock(); $store = \Mockery::mock(StoreInterface::class) diff --git a/tests/Unit/SymfonyCache/CacheEventTest.php b/tests/Unit/SymfonyCache/CacheEventTest.php index 54507307..a44fcbc3 100644 --- a/tests/Unit/SymfonyCache/CacheEventTest.php +++ b/tests/Unit/SymfonyCache/CacheEventTest.php @@ -40,7 +40,7 @@ public function testEventGetters() $this->assertSame($this->kernel, $event->getKernel()); $this->assertSame($request, $event->getRequest()); $this->assertNull($event->getResponse()); - $this->assertSame(HttpKernelInterface::MASTER_REQUEST, $event->getRequestType()); + $this->assertSame(HttpKernelInterface::MAIN_REQUEST, $event->getRequestType()); $response = new Response(); diff --git a/tests/Unit/SymfonyCache/PurgeListenerTest.php b/tests/Unit/SymfonyCache/PurgeListenerTest.php index bee8f9c2..f22cd583 100644 --- a/tests/Unit/SymfonyCache/PurgeListenerTest.php +++ b/tests/Unit/SymfonyCache/PurgeListenerTest.php @@ -18,7 +18,6 @@ use Mockery\MockInterface; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\RequestMatcher; use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher; use Symfony\Component\HttpFoundation\RequestMatcherInterface; use Symfony\Component\HttpFoundation\Response; @@ -37,7 +36,7 @@ public function testConstructorOverspecified() $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('You may not set both a request matcher and an IP'); new PurgeListener([ - 'client_matcher' => $this->createRequestMatcher('/forbidden'), + 'client_matcher' => new PathRequestMatcher('/forbidden'), 'client_ips' => ['1.2.3.4'], ]); } @@ -131,7 +130,7 @@ public function testPurgeForbiddenMatcher() { $kernel = $this->getUnusedKernelMock(); - $matcher = $this->createRequestMatcher('/forbidden'); + $matcher = new PathRequestMatcher('/forbidden'); $purgeListener = new PurgeListener(['client_matcher' => $matcher]); $request = Request::create('http://example.com/foo', 'PURGE'); $event = new CacheEvent($kernel, $request); @@ -186,14 +185,6 @@ public function testInvalidConfiguration() new PurgeListener(['stuff' => '1.2.3.4']); } - private function createRequestMatcher(string $path): RequestMatcherInterface - { - return class_exists(PathRequestMatcher::class) - ? new PathRequestMatcher($path) - : new RequestMatcher($path) - ; - } - /** * @return CacheInvalidation|MockInterface */ diff --git a/tests/Unit/SymfonyCache/PurgeTagsListenerTest.php b/tests/Unit/SymfonyCache/PurgeTagsListenerTest.php index 56498b46..d8998455 100644 --- a/tests/Unit/SymfonyCache/PurgeTagsListenerTest.php +++ b/tests/Unit/SymfonyCache/PurgeTagsListenerTest.php @@ -18,7 +18,8 @@ use Mockery\MockInterface; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\RequestMatcher; +use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher; +use Symfony\Component\HttpFoundation\RequestMatcherInterface; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpCache\StoreInterface; use Toflar\Psr6HttpCacheStore\Psr6Store; @@ -43,7 +44,7 @@ public function testConstructorOverspecified() $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('You may not set both a request matcher and an IP'); new PurgeTagsListener([ - 'client_matcher' => new RequestMatcher('/forbidden'), + 'client_matcher' => new PathRequestMatcher('/forbidden'), 'client_ips' => ['1.2.3.4'], ]); } @@ -122,7 +123,7 @@ public function testPurgeForbiddenMatcher() { $kernel = $this->getUnusedKernelMock(); - $matcher = new RequestMatcher('/forbidden'); + $matcher = new PathRequestMatcher('/forbidden'); $purgeTagsListener = new PurgeTagsListener(['client_matcher' => $matcher]); $request = Request::create('http://example.com/foo', 'PURGETAGS'); $event = new CacheEvent($kernel, $request); @@ -155,7 +156,7 @@ public function testPurgeForbiddenIp() public function testOtherMethod() { $kernel = $this->getUnusedKernelMock(); - $matcher = \Mockery::mock(RequestMatcher::class) + $matcher = \Mockery::mock(RequestMatcherInterface::class) ->shouldNotReceive('isRequestAllowed') ->getMock(); diff --git a/tests/Unit/SymfonyCache/RefreshListenerTest.php b/tests/Unit/SymfonyCache/RefreshListenerTest.php index 6e6b8b0f..57d4070c 100644 --- a/tests/Unit/SymfonyCache/RefreshListenerTest.php +++ b/tests/Unit/SymfonyCache/RefreshListenerTest.php @@ -17,7 +17,6 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\RequestMatcher; use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher; use Symfony\Component\HttpFoundation\Response; @@ -58,10 +57,7 @@ public function testRefreshForbiddenMatcher() ->method('fetch') ; - $matcher = class_exists(PathRequestMatcher::class) - ? new PathRequestMatcher('/forbidden') - : new RequestMatcher('/forbidden') - ; + $matcher = new PathRequestMatcher('/forbidden'); $refreshListener = new RefreshListener(['client_matcher' => $matcher]); $request = Request::create('http://example.com/foo'); $request->headers->addCacheControlDirective('no-cache');