From 838f471faa5d5e8607629c5f095aa4d3b8cb2b4c Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sat, 23 Mar 2024 10:24:47 +0100 Subject: [PATCH] static typing tests and remove legacy things --- composer.json | 3 +- src/CacheInvalidator.php | 32 +++++----- src/Event.php | 14 +---- src/ResponseTagger.php | 22 +++---- src/SymfonyCache/CacheInvalidation.php | 51 ++++++++------- .../Compatibility/CacheInvalidationLegacy.php | 47 -------------- .../Compatibility/CacheInvalidationS6.php | 47 -------------- tests/Functional/CacheInvalidatorTest.php | 2 +- .../Functional/ProxyClient/BanAssertions.php | 8 +-- .../ProxyClient/HttpDispatcherTest.php | 4 +- .../ProxyClient/InvalidateTagsAssertions.php | 6 +- .../ProxyClient/NginxProxyClientTest.php | 14 ++--- .../ProxyClient/PurgeAssertions.php | 6 +- .../ProxyClient/RefreshAssertions.php | 4 +- .../ProxyClient/SymfonyProxyClientTest.php | 14 ++--- .../ProxyClient/VarnishProxyClientTest.php | 18 +++--- .../Symfony/EventDispatchingHttpCacheTest.php | 2 +- tests/Functional/Varnish/CustomTtlTest.php | 14 ++--- .../Varnish/UserContextCacheTest.php | 16 +++-- .../Varnish/UserContextFailureTest.php | 14 ++--- .../Varnish/UserContextNocacheTest.php | 6 +- .../Varnish/UserContextTestCase.php | 10 +-- .../Exception/ExceptionCollectionTest.php | 4 +- tests/Unit/ProxyClient/CloudflareTest.php | 13 ++-- tests/Unit/ProxyClient/FastlyTest.php | 19 +++--- tests/Unit/ProxyClient/HttpDispatcherTest.php | 63 ++++++++----------- .../Unit/ProxyClient/HttpProxyClientTest.php | 4 +- .../ProxyClient/MultiplexerClientTest.php | 18 +++--- tests/Unit/ProxyClient/NoopTest.php | 19 +++--- tests/Unit/ProxyClient/SymfonyTest.php | 18 +++--- tests/Unit/ProxyClient/VarnishTest.php | 25 ++++---- tests/Unit/SymfonyCache/CacheEventTest.php | 7 +-- .../CleanupCacheTagsListenerTest.php | 8 +-- .../SymfonyCache/CustomTtlListenerTest.php | 17 +++-- tests/Unit/SymfonyCache/DebugListenerTest.php | 14 ++--- .../EventDispatchingHttpCacheTest.php | 2 +- .../SymfonyCache/KernelDispatcherTest.php | 14 ++--- tests/Unit/SymfonyCache/PurgeListenerTest.php | 28 ++++----- .../SymfonyCache/PurgeTagsListenerTest.php | 26 +++----- .../Unit/SymfonyCache/RefreshListenerTest.php | 17 +++-- .../SymfonyCache/UserContextListenerTest.php | 33 +++++----- .../CommaSeparatedTagHeaderFormatterTest.php | 12 ++-- .../MaxHeaderValueLengthFormatterTest.php | 17 ++--- .../Test/PHPUnit/IsCacheHitConstraintTest.php | 5 -- .../PHPUnit/IsCacheMissConstraintTest.php | 5 -- 45 files changed, 279 insertions(+), 463 deletions(-) delete mode 100644 src/SymfonyCache/Compatibility/CacheInvalidationLegacy.php delete mode 100644 src/SymfonyCache/Compatibility/CacheInvalidationS6.php diff --git a/composer.json b/composer.json index 273053e39..50577c411 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,8 @@ "phpunit/phpunit": "^9" }, "conflict": { - "toflar/psr6-symfony-http-cache-store": "<2.2.1" + "toflar/psr6-symfony-http-cache-store": "<2.2.1", + "phpunit/phpunit": "<8" }, "suggest": { "friendsofsymfony/http-cache-bundle": "For integration with the Symfony framework", diff --git a/src/CacheInvalidator.php b/src/CacheInvalidator.php index 12ee0f695..52477fc6a 100644 --- a/src/CacheInvalidator.php +++ b/src/CacheInvalidator.php @@ -91,11 +91,9 @@ public function __construct(ProxyClient $cache) * * @param string $operation one of the class constants * - * @return bool - * * @throws InvalidArgumentException */ - public function supports($operation) + public function supports(string $operation): bool { switch ($operation) { case self::PATH: @@ -123,7 +121,7 @@ public function supports($operation) * * @throws \Exception when trying to override the event dispatcher */ - public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) + public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void { if ($this->eventDispatcher) { // if you want to set a custom event dispatcher, do so right after instantiating @@ -135,10 +133,8 @@ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) /** * Get the event dispatcher used by the cache invalidator. - * - * @return EventDispatcherInterface */ - public function getEventDispatcher() + public function getEventDispatcher(): EventDispatcherInterface|EventDispatcher { if (!$this->eventDispatcher) { $this->eventDispatcher = new EventDispatcher(); @@ -157,7 +153,7 @@ public function getEventDispatcher() * * @throws UnsupportedProxyOperationException */ - public function invalidatePath($path, array $headers = []) + public function invalidatePath($path, array $headers = []): static { if (!$this->cache instanceof PurgeCapable) { throw UnsupportedProxyOperationException::cacheDoesNotImplement('PURGE'); @@ -180,7 +176,7 @@ public function invalidatePath($path, array $headers = []) * * @throws UnsupportedProxyOperationException */ - public function refreshPath($path, array $headers = []) + public function refreshPath($path, array $headers = []): static { if (!$this->cache instanceof RefreshCapable) { throw UnsupportedProxyOperationException::cacheDoesNotImplement('REFRESH'); @@ -205,7 +201,7 @@ public function refreshPath($path, array $headers = []) * * @throws UnsupportedProxyOperationException If HTTP cache does not support BAN requests */ - public function invalidate(array $headers) + public function invalidate(array $headers): static { if (!$this->cache instanceof BanCapable) { throw UnsupportedProxyOperationException::cacheDoesNotImplement('BAN'); @@ -227,7 +223,7 @@ public function invalidate(array $headers) * * @throws UnsupportedProxyOperationException If HTTP cache does not support Tags invalidation */ - public function invalidateTags(array $tags) + public function invalidateTags(array $tags): static { if (!$this->cache instanceof TagCapable) { throw UnsupportedProxyOperationException::cacheDoesNotImplement('Tags'); @@ -250,20 +246,20 @@ public function invalidateTags(array $tags) * ['example.com', 'other.net']. If the parameter is empty, all hosts * are matched. * - * @see BanCapable::banPath() - * * @param string $path Regular expression pattern for URI to * invalidate - * @param string $contentType Regular expression pattern for the content + * @param string|null $contentType Regular expression pattern for the content * type to limit banning, for instance 'text' - * @param array|string $hosts Regular expression of a host name or list of + * @param array|string|null $hosts Regular expression of a host name or list of * exact host names to limit banning * * @return $this * * @throws UnsupportedProxyOperationException If HTTP cache does not support BAN requests + *@see BanCapable::banPath() + * */ - public function invalidateRegex($path, $contentType = null, $hosts = null) + public function invalidateRegex(string $path, ?string $contentType = null, array|string|null $hosts = null): static { if (!$this->cache instanceof BanCapable) { throw UnsupportedProxyOperationException::cacheDoesNotImplement('BAN'); @@ -281,7 +277,7 @@ public function invalidateRegex($path, $contentType = null, $hosts = null) * * @throws UnsupportedProxyOperationException if HTTP cache does not support clearing the cache completely */ - public function clearCache() + public function clearCache(): static { if (!$this->cache instanceof ClearCapable) { throw UnsupportedProxyOperationException::cacheDoesNotImplement('CLEAR'); @@ -299,7 +295,7 @@ public function clearCache() * * @throws ExceptionCollection if any errors occurred during flush */ - public function flush() + public function flush(): int { try { return $this->cache->flush(); diff --git a/src/Event.php b/src/Event.php index f4adb56f1..52b92a25a 100644 --- a/src/Event.php +++ b/src/Event.php @@ -15,22 +15,14 @@ class Event extends BaseEvent { - private $exception; + private \Throwable $exception; - /** - * Set exception. - */ - public function setException(\Exception $exception) + public function setException(\Throwable $exception): void { $this->exception = $exception; } - /** - * Get exception. - * - * @return \Exception - */ - public function getException() + public function getException(): \Throwable { return $this->exception; } diff --git a/src/ResponseTagger.php b/src/ResponseTagger.php index 7bc15fc05..a75976d7b 100644 --- a/src/ResponseTagger.php +++ b/src/ResponseTagger.php @@ -80,7 +80,7 @@ public function __construct(array $options = []) * * @return string */ - public function getTagsHeaderName() + public function getTagsHeaderName(): string { return $this->headerFormatter->getTagsHeaderName(); } @@ -89,10 +89,8 @@ public function getTagsHeaderName() * Get the value for the HTTP tag header. * * This concatenates all tags and ensures correct encoding. - * - * @return string */ - public function getTagsHeaderValue() + public function getTagsHeaderValue(): string { return $this->headerFormatter->getTagsHeaderValue($this->tags); } @@ -104,7 +102,7 @@ public function getTagsHeaderValue() * * @return string[] */ - protected function parseTagsHeaderValue($headers): array + protected function parseTagsHeaderValue(array|string $headers): array { if ($this->headerFormatter instanceof TagHeaderParser) { return $this->headerFormatter->parseTagsHeaderValue($headers); @@ -117,10 +115,8 @@ protected function parseTagsHeaderValue($headers): array /** * Check whether the tag handler has any tags to set on the response. - * - * @return bool True if this handler will set at least one tag */ - public function hasTags() + public function hasTags(): bool { return 0 < count($this->tags); } @@ -136,7 +132,7 @@ public function hasTags() * * @throws InvalidTagException */ - public function addTags(array $tags) + public function addTags(array $tags): static { $filtered = array_filter($tags, 'is_string'); $filtered = array_filter($filtered, 'strlen'); @@ -156,7 +152,7 @@ public function addTags(array $tags) * This is usually called after adding the tags header to a response. It is * automatically called by the tagResponse method. */ - public function clear() + public function clear(): void { $this->tags = []; } @@ -165,12 +161,10 @@ public function clear() * Set tags on a response and then clear the tags. * * @param ResponseInterface $response Original response - * @param bool $replace Whether to replace the current tags + * @param bool $replace Whether to replace the current tags * on the response - * - * @return ResponseInterface Tagged response */ - public function tagResponse(ResponseInterface $response, $replace = false) + public function tagResponse(ResponseInterface $response, bool $replace = false): ResponseInterface { if (!$this->hasTags()) { return $response; diff --git a/src/SymfonyCache/CacheInvalidation.php b/src/SymfonyCache/CacheInvalidation.php index 9d37cac4b..21cc6bf2c 100644 --- a/src/SymfonyCache/CacheInvalidation.php +++ b/src/SymfonyCache/CacheInvalidation.php @@ -11,36 +11,35 @@ namespace FOS\HttpCache\SymfonyCache; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\HttpCache\StoreInterface; use Symfony\Component\HttpKernel\HttpKernelInterface; -use Symfony\Component\HttpKernel\Kernel; -if (interface_exists(CacheInvalidation::class)) { - return; -} - -/* - * Symfony 6 introduced a BC break in the signature of the protected method HttpKernelInterface::fetch. - * Load the correct interface to match the signature. +/** + * Interface for a HttpCache that supports active cache invalidation. */ -if (\class_exists(Kernel::class) && Kernel::MAJOR_VERSION >= 6) { - // Load class for Symfony >=6.0 - \class_alias( - Compatibility\CacheInvalidationS6::class, - CacheInvalidation::class - ); -} else { - // Load class for any other cases - \class_alias( - Compatibility\CacheInvalidationLegacy::class, - CacheInvalidation::class - ); -} +interface CacheInvalidation extends HttpKernelInterface +{ + /** + * Forwards the Request to the backend and determines whether the response should be stored. + * + * This methods is triggered when the cache missed or a reload is required. + * + * This method is present on HttpCache but must be public to allow event listeners to do + * refresh operations. + * + * @param Request $request A Request instance + * @param bool $catch Whether to process exceptions + * + * @return Response A Response instance + */ + public function fetch(Request $request, bool $catch = false): Response; -if (!interface_exists(CacheInvalidation::class)) { /** - * Provide an empty interface for code scanners. + * Gets the store for cached responses. + * + * @return StoreInterface $store The store used by the HttpCache */ - interface CacheInvalidation extends HttpKernelInterface - { - } + public function getStore(): StoreInterface; } diff --git a/src/SymfonyCache/Compatibility/CacheInvalidationLegacy.php b/src/SymfonyCache/Compatibility/CacheInvalidationLegacy.php deleted file mode 100644 index f76959549..000000000 --- a/src/SymfonyCache/Compatibility/CacheInvalidationLegacy.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\HttpCache\SymfonyCache\Compatibility; - -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\HttpCache\StoreInterface; -use Symfony\Component\HttpKernel\HttpKernelInterface; - -/** - * Interface for a HttpCache that supports active cache invalidation. - * - * Method signature of `fetch` and `getStore` compatible with Symfony 5 and older. - */ -interface CacheInvalidationLegacy extends HttpKernelInterface -{ - /** - * Forwards the Request to the backend and determines whether the response should be stored. - * - * This methods is triggered when the cache missed or a reload is required. - * - * This method is present on HttpCache but must be public to allow event listeners to do - * refresh operations. - * - * @param Request $request A Request instance - * @param bool $catch Whether to process exceptions - * - * @return Response A Response instance - */ - public function fetch(Request $request, $catch = false); - - /** - * Gets the store for cached responses. - * - * @return StoreInterface $store The store used by the HttpCache - */ - public function getStore(); -} diff --git a/src/SymfonyCache/Compatibility/CacheInvalidationS6.php b/src/SymfonyCache/Compatibility/CacheInvalidationS6.php deleted file mode 100644 index a7bbc49c6..000000000 --- a/src/SymfonyCache/Compatibility/CacheInvalidationS6.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\HttpCache\SymfonyCache\Compatibility; - -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\HttpKernel\HttpCache\StoreInterface; -use Symfony\Component\HttpKernel\HttpKernelInterface; - -/** - * Interface for a HttpCache that supports active cache invalidation. - * - * Method signature of `fetch` and `getStore` compatible with Symfony 6 or newer. - */ -interface CacheInvalidationS6 extends HttpKernelInterface -{ - /** - * Forwards the Request to the backend and determines whether the response should be stored. - * - * This methods is triggered when the cache missed or a reload is required. - * - * This method is present on HttpCache but must be public to allow event listeners to do - * refresh operations. - * - * @param Request $request A Request instance - * @param bool $catch Whether to process exceptions - * - * @return Response A Response instance - */ - public function fetch(Request $request, bool $catch = false): Response; - - /** - * Gets the store for cached responses. - * - * @return StoreInterface $store The store used by the HttpCache - */ - public function getStore(): StoreInterface; -} diff --git a/tests/Functional/CacheInvalidatorTest.php b/tests/Functional/CacheInvalidatorTest.php index bd0b4101b..e55074f4d 100644 --- a/tests/Functional/CacheInvalidatorTest.php +++ b/tests/Functional/CacheInvalidatorTest.php @@ -19,7 +19,7 @@ */ class CacheInvalidatorTest extends VarnishTestCase { - public function testInvalidateTags() + public function testInvalidateTags(): void { if (getenv('VARNISH_MODULES_VERSION')) { $uri = '/tags_xkey.php'; diff --git a/tests/Functional/ProxyClient/BanAssertions.php b/tests/Functional/ProxyClient/BanAssertions.php index e5f5c7169..6c5a6add5 100644 --- a/tests/Functional/ProxyClient/BanAssertions.php +++ b/tests/Functional/ProxyClient/BanAssertions.php @@ -25,7 +25,7 @@ trait BanAssertions * @param string $header The header that holds the URLs * @param array $paths The paths to get, defaults to [/cache.php, json.php] */ - protected function assertBanAll(BanCapable $proxyClient, $header, array $paths = ['/cache.php', '/json.php']) + protected function assertBanAll(BanCapable $proxyClient, string $header, array $paths = ['/cache.php', '/json.php']): void { foreach ($paths as $path) { $this->assertMiss($this->getResponse($path)); @@ -47,7 +47,7 @@ protected function assertBanAll(BanCapable $proxyClient, $header, array $paths = * @param string $hostname Name of the host so we can invalidate that host * @param string $path The path to get, defaults to /cache.php */ - protected function assertBanHost(BanCapable $proxyClient, $header, $hostname, $path = '/cache.php') + protected function assertBanHost(BanCapable $proxyClient, string $header, string $hostname, string $path = '/cache.php'): void { $this->assertMiss($this->getResponse($path)); $this->assertHit($this->getResponse($path)); @@ -65,7 +65,7 @@ protected function assertBanHost(BanCapable $proxyClient, $header, $hostname, $p * @param BanCapable $proxyClient The client to send ban instructions to the cache * @param array $paths The paths to get, defaults to [/cache.php, json.php] */ - protected function assertBanPath(BanCapable $proxyClient, array $paths = ['/cache.php', '/json.php']) + protected function assertBanPath(BanCapable $proxyClient, array $paths = ['/cache.php', '/json.php']): void { foreach ($paths as $path) { $this->assertMiss($this->getResponse($path)); @@ -86,7 +86,7 @@ protected function assertBanPath(BanCapable $proxyClient, array $paths = ['/cach * @param string $htmlPath Path to a HTML content, defaults to /cache.php * @param string $otherPath Path to a non-HTML content, defaults to json.php */ - protected function assertBanPathContentType(BanCapable $proxyClient, $htmlPath = '/cache.php', $otherPath = '/json.php') + protected function assertBanPathContentType(BanCapable $proxyClient, string $htmlPath = '/cache.php', string $otherPath = '/json.php'): void { $this->assertMiss($this->getResponse($htmlPath)); $this->assertHit($this->getResponse($htmlPath)); diff --git a/tests/Functional/ProxyClient/HttpDispatcherTest.php b/tests/Functional/ProxyClient/HttpDispatcherTest.php index 962cbddef..c416c085c 100644 --- a/tests/Functional/ProxyClient/HttpDispatcherTest.php +++ b/tests/Functional/ProxyClient/HttpDispatcherTest.php @@ -20,7 +20,7 @@ class HttpDispatcherTest extends TestCase { - public function testNetworkError() + public function testNetworkError(): void { $requestFactory = MessageFactoryDiscovery::find(); $dispatcher = new HttpDispatcher(['localhost:1']); @@ -34,7 +34,7 @@ public function testNetworkError() } } - public function testClientError() + public function testClientError(): void { $requestFactory = MessageFactoryDiscovery::find(); $dispatcher = new HttpDispatcher(['http://foshttpcache.readthedocs.io']); diff --git a/tests/Functional/ProxyClient/InvalidateTagsAssertions.php b/tests/Functional/ProxyClient/InvalidateTagsAssertions.php index 3d0c4371d..f028f3306 100644 --- a/tests/Functional/ProxyClient/InvalidateTagsAssertions.php +++ b/tests/Functional/ProxyClient/InvalidateTagsAssertions.php @@ -22,10 +22,10 @@ trait InvalidateTagsAssertions /** * Asserting that purging cache tags leads to invalidated content. * - * @param PurgeCapable $proxyClient The client to send purge instructions to the cache - * @param string $path The path to get and purge, defaults to /tags.php + * @param TagCapable $proxyClient The client to send purge instructions to the cache + * @param string $path The path to get and purge, defaults to /tags.php */ - protected function assertInvalidateTags(TagCapable $proxyClient, array $cacheTags, $path = '/tags.php') + protected function assertInvalidateTags(TagCapable $proxyClient, array $cacheTags, string $path = '/tags.php'): void { $this->assertMiss($this->getResponse($path)); $this->assertHit($this->getResponse($path)); diff --git a/tests/Functional/ProxyClient/NginxProxyClientTest.php b/tests/Functional/ProxyClient/NginxProxyClientTest.php index c5a30e638..4942de897 100644 --- a/tests/Functional/ProxyClient/NginxProxyClientTest.php +++ b/tests/Functional/ProxyClient/NginxProxyClientTest.php @@ -22,39 +22,39 @@ class NginxProxyClientTest extends NginxTestCase use PurgeAssertions; use RefreshAssertions; - public function testPurgeSeparateLocation() + public function testPurgeSeparateLocation(): void { $this->assertPurge($this->getProxyClient('/purge')); } - public function testPurgeSameLocation() + public function testPurgeSameLocation(): void { $this->assertPurge($this->getProxyClient()); } - public function testPurgeContentType() + public function testPurgeContentType(): void { $this->markTestSkipped('Not working with nginx, it can only purge one type'); $this->assertPurgeContentType($this->getProxyClient()); } - public function testPurgeSeparateLocationHost() + public function testPurgeSeparateLocationHost(): void { $this->assertPurgeHost($this->getProxyClient('/purge'), sprintf('http://%s', $this->getHostName())); } - public function testPurgeSameLocationHost() + public function testPurgeSameLocationHost(): void { $this->assertPurgeHost($this->getProxyClient(), sprintf('http://%s', $this->getHostName())); } - public function testRefresh() + public function testRefresh(): void { $this->assertRefresh($this->getProxyClient()); } - public function testRefreshContentType() + public function testRefreshContentType(): void { $this->markTestSkipped('TODO: is nginx mixing up variants?'); diff --git a/tests/Functional/ProxyClient/PurgeAssertions.php b/tests/Functional/ProxyClient/PurgeAssertions.php index 39468891c..7856564d8 100644 --- a/tests/Functional/ProxyClient/PurgeAssertions.php +++ b/tests/Functional/ProxyClient/PurgeAssertions.php @@ -24,7 +24,7 @@ trait PurgeAssertions * @param PurgeCapable $proxyClient The client to send purge instructions to the cache * @param string $path The path to get and purge, defaults to /cache.php */ - protected function assertPurge(PurgeCapable $proxyClient, $path = '/cache.php') + protected function assertPurge(PurgeCapable $proxyClient, string $path = '/cache.php'): void { $this->assertMiss($this->getResponse($path)); $this->assertHit($this->getResponse($path)); @@ -40,7 +40,7 @@ protected function assertPurge(PurgeCapable $proxyClient, $path = '/cache.php') * @param string $host The host name to use in the purge request * @param string $path The path to get and purge, defaults to /cache.php */ - protected function assertPurgeHost(PurgeCapable $proxyClient, $host, $path = '/cache.php') + protected function assertPurgeHost(PurgeCapable $proxyClient, string $host, string $path = '/cache.php'): void { $this->assertMiss($this->getResponse($path)); $this->assertHit($this->getResponse($path)); @@ -49,7 +49,7 @@ protected function assertPurgeHost(PurgeCapable $proxyClient, $host, $path = '/c $this->assertMiss($this->getResponse($path)); } - protected function assertPurgeContentType(PurgeCapable $proxyClient, $path = '/negotiation.php') + protected function assertPurgeContentType(PurgeCapable $proxyClient, string $path = '/negotiation.php'): void { $json = ['Accept' => 'application/json']; $html = ['Accept' => 'text/html']; diff --git a/tests/Functional/ProxyClient/RefreshAssertions.php b/tests/Functional/ProxyClient/RefreshAssertions.php index a76769450..2269fc718 100644 --- a/tests/Functional/ProxyClient/RefreshAssertions.php +++ b/tests/Functional/ProxyClient/RefreshAssertions.php @@ -25,7 +25,7 @@ trait RefreshAssertions * @param RefreshCapable $proxyClient The client to send refresh instructions to the cache * @param string $path The path to get and refresh, defaults to /cache.php */ - protected function assertRefresh(RefreshCapable $proxyClient, $path = '/cache.php') + protected function assertRefresh(RefreshCapable $proxyClient, string $path = '/cache.php'): void { $this->assertMiss($this->getResponse($path)); $response = $this->getResponse($path); @@ -50,7 +50,7 @@ protected function assertRefresh(RefreshCapable $proxyClient, $path = '/cache.ph * @param RefreshCapable $proxyClient The client to send refresh instructions to the cache * @param string $path The path to get and refresh, defaults to /negotiation.php */ - protected function assertRefreshContentType(RefreshCapable $proxyClient, $path = '/negotiation.php') + protected function assertRefreshContentType(RefreshCapable $proxyClient, string $path = '/negotiation.php'): void { $json = ['Accept' => 'application/json']; $html = ['Accept' => 'text/html']; diff --git a/tests/Functional/ProxyClient/SymfonyProxyClientTest.php b/tests/Functional/ProxyClient/SymfonyProxyClientTest.php index 5445f10e4..bf401762b 100644 --- a/tests/Functional/ProxyClient/SymfonyProxyClientTest.php +++ b/tests/Functional/ProxyClient/SymfonyProxyClientTest.php @@ -24,32 +24,32 @@ class SymfonyProxyClientTest extends SymfonyTestCase use PurgeAssertions; use RefreshAssertions; - public function testPurge() + public function testPurge(): void { $this->assertPurge($this->getProxyClient(), '/symfony.php/cache'); } - public function testPurgeContentType() + public function testPurgeContentType(): void { $this->assertPurge($this->getProxyClient(), '/symfony.php/negotiation'); } - public function testPurgeHost() + public function testPurgeHost(): void { $this->assertPurgeHost($this->getProxyClient(), 'http://localhost:8080', '/symfony.php/cache'); } - public function testRefresh() + public function testRefresh(): void { $this->assertRefresh($this->getProxyClient(), '/symfony.php/cache'); } - public function testRefreshContentType() + public function testRefreshContentType(): void { $this->assertRefresh($this->getProxyClient(), '/symfony.php/negotiation'); } - public function testInvalidateTags() + public function testInvalidateTags(): void { if (!class_exists(Psr6Store::class)) { $this->markTestSkipped('Needs PSR-6 store to be installed.'); @@ -58,7 +58,7 @@ public function testInvalidateTags() $this->assertInvalidateTags($this->getProxyClient(), ['tag1'], '/symfony.php/tags'); } - public function testInvalidateTagsMultiHeader() + public function testInvalidateTagsMultiHeader(): void { if (!class_exists(Psr6Store::class)) { $this->markTestSkipped('Needs PSR-6 store to be installed.'); diff --git a/tests/Functional/ProxyClient/VarnishProxyClientTest.php b/tests/Functional/ProxyClient/VarnishProxyClientTest.php index 07a009525..6064f32d7 100644 --- a/tests/Functional/ProxyClient/VarnishProxyClientTest.php +++ b/tests/Functional/ProxyClient/VarnishProxyClientTest.php @@ -24,47 +24,47 @@ class VarnishProxyClientTest extends VarnishTestCase use PurgeAssertions; use RefreshAssertions; - public function testBanAll() + public function testBanAll(): void { $this->assertBanAll($this->getProxyClient(), Varnish::HTTP_HEADER_URL); } - public function testBanHost() + public function testBanHost(): void { $this->assertBanHost($this->getProxyClient(), Varnish::HTTP_HEADER_HOST, $this->getHostName()); } - public function testBanPathAll() + public function testBanPathAll(): void { $this->assertBanPath($this->getProxyClient()); } - public function testBanPathContentType() + public function testBanPathContentType(): void { $this->assertBanPathContentType($this->getProxyClient()); } - public function testPurge() + public function testPurge(): void { $this->assertPurge($this->getProxyClient()); } - public function testPurgeContentType() + public function testPurgeContentType(): void { $this->assertPurgeContentType($this->getProxyClient()); } - public function testPurgeHost() + public function testPurgeHost(): void { $this->assertPurgeHost($this->getProxyClient(), 'http://localhost:6181'); } - public function testRefresh() + public function testRefresh(): void { $this->assertRefresh($this->getProxyClient()); } - public function testRefreshContentType() + public function testRefreshContentType(): void { $this->assertRefreshContentType($this->getProxyClient()); } diff --git a/tests/Functional/Symfony/EventDispatchingHttpCacheTest.php b/tests/Functional/Symfony/EventDispatchingHttpCacheTest.php index 49a606e77..d5e58dcd1 100644 --- a/tests/Functional/Symfony/EventDispatchingHttpCacheTest.php +++ b/tests/Functional/Symfony/EventDispatchingHttpCacheTest.php @@ -34,7 +34,7 @@ class EventDispatchingHttpCacheTest extends TestCase { use MockeryPHPUnitIntegration; - public function testEventListeners() + public function testEventListeners(): void { $request = new Request(); $expectedResponse = new Response(); diff --git a/tests/Functional/Varnish/CustomTtlTest.php b/tests/Functional/Varnish/CustomTtlTest.php index 6fdbe7284..dd21b4165 100644 --- a/tests/Functional/Varnish/CustomTtlTest.php +++ b/tests/Functional/Varnish/CustomTtlTest.php @@ -19,17 +19,15 @@ */ class CustomTtlTest extends VarnishTestCase { - protected function getConfigFile() + protected function getConfigFile(): string { - switch ((int) $this->getVarnishVersion()) { - case 3: - return dirname(__DIR__).'/Fixtures/varnish-3/custom_ttl.vcl'; - default: - return dirname(__DIR__).'/Fixtures/varnish/custom_ttl.vcl'; - } + return match ((int)$this->getVarnishVersion()) { + 3 => dirname(__DIR__).'/Fixtures/varnish-3/custom_ttl.vcl', + default => dirname(__DIR__).'/Fixtures/varnish/custom_ttl.vcl', + }; } - public function testCustomTtl() + public function testCustomTtl(): void { $this->assertMiss($this->getResponse('/custom-ttl.php')); $this->assertHit($this->getResponse('/custom-ttl.php')); diff --git a/tests/Functional/Varnish/UserContextCacheTest.php b/tests/Functional/Varnish/UserContextCacheTest.php index 91c24d980..be5514207 100644 --- a/tests/Functional/Varnish/UserContextCacheTest.php +++ b/tests/Functional/Varnish/UserContextCacheTest.php @@ -17,18 +17,16 @@ */ class UserContextCacheTest extends UserContextTestCase { - protected function getConfigFile() + protected function getConfigFile(): string { - switch ((int) $this->getVarnishVersion()) { - case 3: - return dirname(__DIR__).'/Fixtures/varnish-3/user_context_cache.vcl'; - default: - return dirname(__DIR__).'/Fixtures/varnish/user_context_cache.vcl'; - } + return match ((int)$this->getVarnishVersion()) { + 3 => dirname(__DIR__).'/Fixtures/varnish-3/user_context_cache.vcl', + default => dirname(__DIR__).'/Fixtures/varnish/user_context_cache.vcl', + }; } - protected function assertContextCache($status) + protected function assertContextCache(string $hashCache): void { - $this->assertEquals('HIT', $status); + $this->assertEquals('HIT', $hashCache); } } diff --git a/tests/Functional/Varnish/UserContextFailureTest.php b/tests/Functional/Varnish/UserContextFailureTest.php index cc256ddfc..977d3e262 100644 --- a/tests/Functional/Varnish/UserContextFailureTest.php +++ b/tests/Functional/Varnish/UserContextFailureTest.php @@ -23,10 +23,8 @@ class UserContextFailureTest extends VarnishTestCase { /** * Can be "cache" or "failure" and is used to determine the correct .vcl file. - * - * @var string */ - private $mode = 'cache'; + private string $mode = 'cache'; public function setUp(): void { @@ -41,7 +39,7 @@ public function setUp(): void /** * The user hash must not be exposed to the client under any circumstances. */ - public function testUserContextNoExposeHash() + public function testUserContextNoExposeHash(): void { $response = $this->getResponse( '/user_context_hash_nocache.php', @@ -57,7 +55,7 @@ public function testUserContextNoExposeHash() /** * A hash sent by the client must not be used by varnish. */ - public function testUserContextNoForgedHash() + public function testUserContextNoForgedHash(): void { $response = $this->getResponse( '/user_context_hash_nocache.php', @@ -72,7 +70,7 @@ public function testUserContextNoForgedHash() /** * A request on POST should not use the context. */ - public function testUserContextNotUsed() + public function testUserContextNotUsed(): void { // First request in GET $this->getResponse('/user_context.php', ['Cookie' => '0=foo']); @@ -89,13 +87,13 @@ public function testUserContextNotUsed() $this->assertMiss($postResponse); } - public function testHashRequestFailure() + public function testHashRequestFailure(): void { $response = $this->getResponse('/user_context.php', ['Cookie' => '0=foo']); $this->assertEquals(503, $response->getStatusCode()); } - protected function getConfigFile() + protected function getConfigFile(): ?string { switch ((int) $this->getVarnishVersion()) { case 3: diff --git a/tests/Functional/Varnish/UserContextNocacheTest.php b/tests/Functional/Varnish/UserContextNocacheTest.php index d8b7a0749..e93313856 100644 --- a/tests/Functional/Varnish/UserContextNocacheTest.php +++ b/tests/Functional/Varnish/UserContextNocacheTest.php @@ -17,7 +17,7 @@ */ class UserContextNocacheTest extends UserContextTestCase { - protected function getConfigFile() + protected function getConfigFile(): ?string { switch ((int) $this->getVarnishVersion()) { case 3: @@ -27,8 +27,8 @@ protected function getConfigFile() } } - protected function assertContextCache($status) + protected function assertContextCache(string $hashCache): void { - $this->assertEquals('MISS', $status); + $this->assertEquals('MISS', $hashCache); } } diff --git a/tests/Functional/Varnish/UserContextTestCase.php b/tests/Functional/Varnish/UserContextTestCase.php index 984d8dc14..b2428d2e7 100644 --- a/tests/Functional/Varnish/UserContextTestCase.php +++ b/tests/Functional/Varnish/UserContextTestCase.php @@ -26,13 +26,13 @@ abstract class UserContextTestCase extends VarnishTestCase * * @param string $hashCache the cache status of the context request */ - abstract protected function assertContextCache($hashCache); + abstract protected function assertContextCache(string $hashCache): void; /** * Sending requests without an Accept: header so none should arrive at the * backend for the actual request. */ - public function testUserContextHash() + public function testUserContextHash(): void { $response1 = $this->getResponse('/user_context.php', ['Cookie' => ['0=foo']]); $this->assertEquals('foo', (string) $response1->getBody()); @@ -66,7 +66,7 @@ public function testUserContextHash() /** * Making sure that non-authenticated and authenticated cache are not mixed up. */ - public function testUserContextNoAuth() + public function testUserContextNoAuth(): void { $response1 = $this->getResponse('/user_context_anon.php'); $this->assertEquals('anonymous', $response1->getBody()); @@ -86,7 +86,7 @@ public function testUserContextNoAuth() $this->assertHit($cachedResponse2); } - public function testAcceptHeader() + public function testAcceptHeader(): void { $response1 = $this->getResponse( '/user_context.php?accept=text/plain', @@ -98,7 +98,7 @@ public function testAcceptHeader() $this->assertEquals('foo', $response1->getBody()); } - public function testUserContextUnauthorized() + public function testUserContextUnauthorized(): void { $response = $this->getResponse('/user_context.php', ['Cookie' => ['0=miam']]); $this->assertEquals(403, $response->getStatusCode()); diff --git a/tests/Unit/Exception/ExceptionCollectionTest.php b/tests/Unit/Exception/ExceptionCollectionTest.php index 6751d6978..88eaa76f7 100644 --- a/tests/Unit/Exception/ExceptionCollectionTest.php +++ b/tests/Unit/Exception/ExceptionCollectionTest.php @@ -16,7 +16,7 @@ class ExceptionCollectionTest extends TestCase { - public function testCollectionConstructor() + public function testCollectionConstructor(): void { $e1 = new \RuntimeException(); $e2 = new \RuntimeException('Message'); @@ -33,7 +33,7 @@ public function testCollectionConstructor() $this->assertEquals([$e1, $e2], $actual); } - public function testCollectionAdd() + public function testCollectionAdd(): void { $collection = new ExceptionCollection(); $this->assertNull($collection->getFirst()); diff --git a/tests/Unit/ProxyClient/CloudflareTest.php b/tests/Unit/ProxyClient/CloudflareTest.php index 9b19f8008..a109c9bb9 100644 --- a/tests/Unit/ProxyClient/CloudflareTest.php +++ b/tests/Unit/ProxyClient/CloudflareTest.php @@ -25,10 +25,7 @@ class CloudflareTest extends TestCase public const AUTH_TOKEN = 'abc123'; public const ZONE_IDENTIFIER = 'abcdef123abcdef123'; - /** - * @var HttpDispatcher|MockInterface - */ - private $httpDispatcher; + private HttpDispatcher&MockInterface $httpDispatcher; protected function setUp(): void { @@ -42,7 +39,7 @@ protected function tearDown(): void parent::tearDown(); } - protected function getProxyClient(array $options = []) + protected function getProxyClient(array $options = []): Cloudflare { $options = [ 'authentication_token' => self::AUTH_TOKEN, @@ -52,7 +49,7 @@ protected function getProxyClient(array $options = []) return new Cloudflare($this->httpDispatcher, $options); } - public function testInvalidateTagsPurge() + public function testInvalidateTagsPurge(): void { $cloudflare = $this->getProxyClient(); @@ -74,7 +71,7 @@ function (RequestInterface $request) { $cloudflare->invalidateTags(['tag-one', 'tag-two']); } - public function testPurge() + public function testPurge(): void { $cloudflare = $this->getProxyClient(); @@ -105,7 +102,7 @@ function (RequestInterface $request) use ($expected) { $cloudflare->flush(); } - public function testClear() + public function testClear(): void { $cloudflare = $this->getProxyClient(); diff --git a/tests/Unit/ProxyClient/FastlyTest.php b/tests/Unit/ProxyClient/FastlyTest.php index 0fba63526..58c6c9271 100644 --- a/tests/Unit/ProxyClient/FastlyTest.php +++ b/tests/Unit/ProxyClient/FastlyTest.php @@ -22,10 +22,7 @@ class FastlyTest extends TestCase { use MockeryPHPUnitIntegration; - /** - * @var HttpDispatcher|MockInterface - */ - private $httpDispatcher; + private HttpDispatcher&MockInterface $httpDispatcher; protected function setUp(): void { @@ -39,7 +36,7 @@ protected function tearDown(): void parent::tearDown(); } - protected function getProxyClient(array $options = []) + protected function getProxyClient(array $options = []): Fastly { $options = [ 'authentication_token' => 'o43r8j34hr', @@ -49,7 +46,7 @@ protected function getProxyClient(array $options = []) return new Fastly($this->httpDispatcher, $options); } - public function testInvalidateTagsDefaultSoftPurge() + public function testInvalidateTagsDefaultSoftPurge(): void { $fastly = $this->getProxyClient(); @@ -76,7 +73,7 @@ function (RequestInterface $request) { $fastly->invalidateTags(['post-1', 'post,type-3']); } - public function testInvalidateTagsHardPurge() + public function testInvalidateTagsHardPurge(): void { $fastly = $this->getProxyClient(['soft_purge' => false]); @@ -99,7 +96,7 @@ function (RequestInterface $request) { $fastly->invalidateTags(['post-1', 'post,type-3']); } - public function testInvalidateTagsHeadersSplit() + public function testInvalidateTagsHeadersSplit(): void { $fastly = $this->getProxyClient(); @@ -112,7 +109,7 @@ public function testInvalidateTagsHeadersSplit() $fastly->invalidateTags($tags); } - public function testPurge() + public function testPurge(): void { $fastly = $this->getProxyClient(); @@ -139,7 +136,7 @@ function (RequestInterface $request) { $fastly->purge('/url', ['X-Foo' => 'bar']); } - public function testRefresh() + public function testRefresh(): void { $fastly = $this->getProxyClient(); @@ -159,7 +156,7 @@ function (RequestInterface $request) { $fastly->refresh('/fresh'); } - public function testClear() + public function testClear(): void { $fastly = $this->getProxyClient(); diff --git a/tests/Unit/ProxyClient/HttpDispatcherTest.php b/tests/Unit/ProxyClient/HttpDispatcherTest.php index 26c671cf4..198082bfe 100644 --- a/tests/Unit/ProxyClient/HttpDispatcherTest.php +++ b/tests/Unit/ProxyClient/HttpDispatcherTest.php @@ -36,22 +36,11 @@ class HttpDispatcherTest extends TestCase { use MockeryPHPUnitIntegration; - /** - * Mock HTTP client. - * - * @var Client - */ - private $httpClient; + private Client $httpClient; - /** - * @var MessageFactory - */ - private $messageFactory; + private MessageFactory $messageFactory; - /** - * @var UriFactory - */ - private $uriFactory; + private UriFactory $uriFactory; protected function setUp(): void { @@ -60,7 +49,7 @@ protected function setUp(): void $this->uriFactory = UriFactoryDiscovery::find(); } - public function testInstantiateWithNonUri() + public function testInstantiateWithNonUri(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('URI parameter must be a string, object given'); @@ -74,11 +63,11 @@ public function testInstantiateWithNonUri() /** * @dataProvider exceptionProvider * - * @param \Exception $exception Exception thrown by HTTP client - * @param string $type The returned exception class to be expected - * @param string $message Optional exception message to match against + * @param \Exception $exception Exception thrown by HTTP client + * @param string $type The returned exception class to be expected + * @param string|null $message Optional exception message to match against */ - public function testExceptions(\Exception $exception, $type, $message = null) + public function testExceptions(\Exception $exception, string $type, ?string $message = null): void { $this->doTestException($exception, $type, $message); } @@ -88,12 +77,12 @@ public function testExceptions(\Exception $exception, $type, $message = null) * * @group legacy */ - public function testLegacyException() + public function testLegacyException(): void { $this->doTestException(new \Exception('something went completely wrong'), InvalidArgumentException::class, 'something went completely wrong'); } - private function doTestException(\Exception $exception, $type, $message) + private function doTestException(\Exception $exception, string $type, string $message): void { $this->httpClient->addException($exception); $httpDispatcher = new HttpDispatcher( @@ -119,7 +108,7 @@ private function doTestException(\Exception $exception, $type, $message) $httpDispatcher->flush(); } - public function exceptionProvider() + public function exceptionProvider(): array { /** @var RequestInterface $request */ $request = \Mockery::mock(RequestInterface::class) @@ -153,7 +142,7 @@ public function exceptionProvider() ]; } - public function testMissingHostExceptionIsThrown() + public function testMissingHostExceptionIsThrown(): void { $this->expectException(MissingHostException::class); $this->expectExceptionMessage('cannot be invalidated without a host'); @@ -168,7 +157,7 @@ public function testMissingHostExceptionIsThrown() $httpDispatcher->invalidate($request); } - public function testBanWithoutBaseUri() + public function testBanWithoutBaseUri(): void { $httpDispatcher = new HttpDispatcher( ['127.0.0.1:123'], @@ -184,7 +173,7 @@ public function testBanWithoutBaseUri() $this->assertSame('127.0.0.1:123', $requests[0]->getHeaderLine('Host')); } - public function testSetBasePathWithHost() + public function testSetBasePathWithHost(): void { $httpDispatcher = new HttpDispatcher( ['127.0.0.1'], @@ -200,7 +189,7 @@ public function testSetBasePathWithHost() $this->assertEquals('fos.lo', $requests[0]->getHeaderLine('Host')); } - public function testServerWithUserInfo() + public function testServerWithUserInfo(): void { $httpDispatcher = new HttpDispatcher( ['http://userone:passone@127.0.0.1', 'http://127.0.0.2', 'http://usertwo:passtwo@127.0.0.2'], @@ -220,7 +209,7 @@ public function testServerWithUserInfo() $this->assertEquals('usertwo:passtwo', $requests[2]->getUri()->getUserInfo()); } - public function testSetBasePathWithPath() + public function testSetBasePathWithPath(): void { $httpDispatcher = new HttpDispatcher( ['127.0.0.1:8080'], @@ -236,7 +225,7 @@ public function testSetBasePathWithPath() $this->assertEquals('http://127.0.0.1:8080/my/path/append', (string) $requests[0]->getUri()); } - public function testSetServersDefaultSchemeIsAdded() + public function testSetServersDefaultSchemeIsAdded(): void { $httpDispatcher = new HttpDispatcher(['127.0.0.1'], 'fos.lo', $this->httpClient); $request = $this->messageFactory->createRequest('PURGE', '/some/path'); @@ -247,7 +236,7 @@ public function testSetServersDefaultSchemeIsAdded() $this->assertEquals('http://127.0.0.1/some/path', $requests[0]->getUri()); } - public function testSchemeIsAdded() + public function testSchemeIsAdded(): void { $httpDispatcher = new HttpDispatcher(['127.0.0.1'], 'fos.lo', $this->httpClient); $uri = $this->uriFactory->createUri('/some/path')->withHost('goo.bar'); @@ -259,7 +248,7 @@ public function testSchemeIsAdded() $this->assertEquals('http://127.0.0.1/some/path', $requests[0]->getUri()); } - public function testPortIsAdded() + public function testPortIsAdded(): void { $httpDispatcher = new HttpDispatcher(['127.0.0.1:8080'], 'fos.lo', $this->httpClient); $request = $this->messageFactory->createRequest('PURGE', '/some/path'); @@ -270,7 +259,7 @@ public function testPortIsAdded() $this->assertEquals('http://127.0.0.1:8080/some/path', $requests[0]->getUri()); } - public function testSetServersThrowsInvalidUrlException() + public function testSetServersThrowsInvalidUrlException(): void { $this->expectException(InvalidUrlException::class); $this->expectExceptionMessage('URL "http:///this is no url" is invalid.'); @@ -278,7 +267,7 @@ public function testSetServersThrowsInvalidUrlException() new HttpDispatcher(['http:///this is no url']); } - public function testSetServersThrowsWeirdInvalidUrlException() + public function testSetServersThrowsWeirdInvalidUrlException(): void { $this->expectException(InvalidUrlException::class); $this->expectExceptionMessage('"this ://is no url" is invalid.'); @@ -286,7 +275,7 @@ public function testSetServersThrowsWeirdInvalidUrlException() new HttpDispatcher(['this ://is no url']); } - public function testSetServersThrowsInvalidServerException() + public function testSetServersThrowsInvalidServerException(): void { $this->expectException(InvalidUrlException::class); $this->expectExceptionMessage('Server "http://127.0.0.1:80/some/path" is invalid. Only scheme, user, pass, host, port URL parts are allowed'); @@ -294,7 +283,7 @@ public function testSetServersThrowsInvalidServerException() new HttpDispatcher(['http://127.0.0.1:80/some/path']); } - public function testFlushEmpty() + public function testFlushEmpty(): void { $httpDispatcher = new HttpDispatcher( ['127.0.0.1', '127.0.0.2'], @@ -306,7 +295,7 @@ public function testFlushEmpty() $this->assertCount(0, $this->httpClient->getRequests()); } - public function testFlushCountSuccess() + public function testFlushCountSuccess(): void { $httpClient = \Mockery::mock(HttpAsyncClient::class) ->shouldReceive('sendAsyncRequest') @@ -346,7 +335,7 @@ function (RequestInterface $request) { ); } - public function testEliminateDuplicates() + public function testEliminateDuplicates(): void { $httpClient = \Mockery::mock(HttpAsyncClient::class) ->shouldReceive('sendAsyncRequest') @@ -389,7 +378,7 @@ function (RequestInterface $request) { /** * @return RequestInterface[] */ - protected function getRequests() + protected function getRequests(): array { return $this->httpClient->getRequests(); } diff --git a/tests/Unit/ProxyClient/HttpProxyClientTest.php b/tests/Unit/ProxyClient/HttpProxyClientTest.php index b99762e31..d75998519 100644 --- a/tests/Unit/ProxyClient/HttpProxyClientTest.php +++ b/tests/Unit/ProxyClient/HttpProxyClientTest.php @@ -24,9 +24,9 @@ class HttpProxyClientTest extends TestCase { use MockeryPHPUnitIntegration; - public function testFlush() + public function testFlush(): void { - /** @var HttpDispatcher|MockInterface $httpDispatcher */ + /** @var HttpDispatcher&MockInterface $httpDispatcher */ $httpDispatcher = \Mockery::mock(HttpDispatcher::class) ->shouldReceive('flush') ->once() diff --git a/tests/Unit/ProxyClient/MultiplexerClientTest.php b/tests/Unit/ProxyClient/MultiplexerClientTest.php index 8911bb7c3..ca42ca92c 100644 --- a/tests/Unit/ProxyClient/MultiplexerClientTest.php +++ b/tests/Unit/ProxyClient/MultiplexerClientTest.php @@ -26,7 +26,7 @@ class MultiplexerClientTest extends TestCase { use MockeryPHPUnitIntegration; - public function testBan() + public function testBan(): void { $headers = ['Header1' => 'Header1-Value']; @@ -47,7 +47,7 @@ public function testBan() $this->assertSame($multiplexer, $multiplexer->ban($headers)); } - public function testBanPath() + public function testBanPath(): void { $path = 'path/to/ban'; $contentType = 'text/css'; @@ -69,7 +69,7 @@ public function testBanPath() $this->assertSame($multiplexer, $multiplexer->banPath($path, $contentType, $hosts)); } - public function testFlush() + public function testFlush(): void { $mockClient1 = \Mockery::mock(ProxyClient::class) ->shouldReceive('flush') @@ -87,7 +87,7 @@ public function testFlush() $this->assertEquals(10, $multiplexer->flush()); } - public function testInvalidateTags() + public function testInvalidateTags(): void { $tags = ['tag-1', 'tag-2']; @@ -102,7 +102,7 @@ public function testInvalidateTags() $this->assertSame($multiplexer, $multiplexer->invalidateTags($tags)); } - public function testRefresh() + public function testRefresh(): void { $url = 'example.com'; $headers = ['Header1' => 'Header1-Value']; @@ -124,7 +124,7 @@ public function testRefresh() $this->assertSame($multiplexer, $multiplexer->refresh($url, $headers)); } - public function testPurge() + public function testPurge(): void { $url = 'example.com'; $headers = ['Header1' => 'Header1-Value']; @@ -146,7 +146,7 @@ public function testPurge() $this->assertSame($multiplexer, $multiplexer->purge($url, $headers)); } - public function testClear() + public function testClear(): void { $mockClient1 = \Mockery::mock(ClearCapable::class) ->shouldReceive('clear') @@ -163,7 +163,7 @@ public function testClear() $this->assertSame($multiplexer, $multiplexer->clear()); } - public function provideInvalidClient() + public function provideInvalidClient(): array { return [ [['this-is-not-an-object']], @@ -176,7 +176,7 @@ public function provideInvalidClient() * * @dataProvider provideInvalidClient */ - public function testInvalidClientTest(array $clients) + public function testInvalidClientTest(array $clients): void { $this->expectException(InvalidArgumentException::class); new MultiplexerClient($clients); diff --git a/tests/Unit/ProxyClient/NoopTest.php b/tests/Unit/ProxyClient/NoopTest.php index eb1ca7606..878c2fdc8 100644 --- a/tests/Unit/ProxyClient/NoopTest.php +++ b/tests/Unit/ProxyClient/NoopTest.php @@ -16,42 +16,39 @@ class NoopTest extends TestCase { - /** - * @var Noop - */ - private $noop; + private Noop $noop; protected function setUp(): void { $this->noop = new Noop(); } - public function testBan() + public function testBan(): void { $this->assertSame($this->noop, $this->noop->ban(['header-123'])); } - public function testInvalidateTags() + public function testInvalidateTags(): void { $this->assertSame($this->noop, $this->noop->invalidateTags(['tag123'])); } - public function testBanPath() + public function testBanPath(): void { $this->assertSame($this->noop, $this->noop->banPath('/123')); } - public function testFlush() + public function testFlush(): void { - $this->assertTrue(is_int($this->noop->flush())); + $this->assertSame(0, $this->noop->flush()); } - public function testPurge() + public function testPurge(): void { $this->assertSame($this->noop, $this->noop->purge('/123', ['x-123' => 'yes'])); } - public function testRefresh() + public function testRefresh(): void { $this->assertSame($this->noop, $this->noop->refresh('/123')); } diff --git a/tests/Unit/ProxyClient/SymfonyTest.php b/tests/Unit/ProxyClient/SymfonyTest.php index 3e66c2b69..2b6fe0970 100644 --- a/tests/Unit/ProxyClient/SymfonyTest.php +++ b/tests/Unit/ProxyClient/SymfonyTest.php @@ -15,6 +15,7 @@ use FOS\HttpCache\ProxyClient\Symfony; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Mockery\MockInterface; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; @@ -22,17 +23,14 @@ class SymfonyTest extends TestCase { use MockeryPHPUnitIntegration; - /** - * @var HttpDispatcher|MockInterface - */ - private $httpDispatcher; + private HttpDispatcher&MockInterface $httpDispatcher; protected function setUp(): void { $this->httpDispatcher = \Mockery::mock(HttpDispatcher::class); } - public function testPurge() + public function testPurge(): void { $symfony = new Symfony($this->httpDispatcher); @@ -53,7 +51,7 @@ function (RequestInterface $request) { $symfony->purge('/url', ['X-Foo' => 'bar']); } - public function testInvalidateTags() + public function testInvalidateTags(): void { $symfony = new Symfony($this->httpDispatcher); @@ -74,9 +72,9 @@ function (RequestInterface $request) { $symfony->invalidateTags(['foobar', 'other tag']); } - public function testInvalidateTagsWithALotOfTags() + public function testInvalidateTagsWithALotOfTags(): void { - /** @var HttpDispatcher|\PHPUnit_Framework_MockObject_MockObject $dispatcher */ + /** @var HttpDispatcher&MockObject $dispatcher */ $dispatcher = $this->createMock(HttpDispatcher::class); $dispatcher ->expects($this->exactly(3)) @@ -143,7 +141,7 @@ public function testInvalidateTagsWithALotOfTags() ]); } - public function testClear() + public function testClear(): void { $symfony = new Symfony($this->httpDispatcher); @@ -164,7 +162,7 @@ function (RequestInterface $request) { $symfony->clear(); } - public function testRefresh() + public function testRefresh(): void { $symfony = new Symfony($this->httpDispatcher); diff --git a/tests/Unit/ProxyClient/VarnishTest.php b/tests/Unit/ProxyClient/VarnishTest.php index 0eacbdcef..a4784654d 100644 --- a/tests/Unit/ProxyClient/VarnishTest.php +++ b/tests/Unit/ProxyClient/VarnishTest.php @@ -23,17 +23,14 @@ class VarnishTest extends TestCase { use MockeryPHPUnitIntegration; - /** - * @var HttpDispatcher|MockInterface - */ - private $httpDispatcher; + private HttpDispatcher&MockInterface $httpDispatcher; protected function setUp(): void { $this->httpDispatcher = \Mockery::mock(HttpDispatcher::class); } - public function testBanHeaders() + public function testBanHeaders(): void { $options = [ 'default_ban_headers' => [ @@ -67,7 +64,7 @@ function (RequestInterface $request) { ]); } - public function testBanPath() + public function testBanPath(): void { $varnish = new Varnish($this->httpDispatcher); $this->httpDispatcher->shouldReceive('invalidate')->once()->with( @@ -87,7 +84,7 @@ function (RequestInterface $request) { $varnish->banPath('/articles/.*', 'text/html', $hosts); } - public function testPurgekeys() + public function testPurgekeys(): void { $options = [ 'tag_mode' => 'purgekeys', @@ -109,7 +106,7 @@ function (RequestInterface $request) { $varnish->invalidateTags(['post-1', 'post,type-3']); } - public function testHardPurgekeys() + public function testHardPurgekeys(): void { $options = [ 'tag_mode' => 'purgekeys', @@ -132,7 +129,7 @@ function (RequestInterface $request) { $varnish->invalidateTags(['post-1', 'post,type-3']); } - public function testBanPathEmptyHost() + public function testBanPathEmptyHost(): void { $varnish = new Varnish($this->httpDispatcher); @@ -141,7 +138,7 @@ public function testBanPathEmptyHost() $varnish->banPath('/articles/.*', 'text/html', $hosts); } - public function testTagsHeaders() + public function testTagsHeaders(): void { $options = [ 'default_ban_headers' => [ @@ -170,7 +167,7 @@ function (RequestInterface $request) { $varnish->invalidateTags(['mytag', 'othertag']); } - public function testTagsHeadersEscapingAndCustomHeader() + public function testTagsHeadersEscapingAndCustomHeader(): void { $options = [ 'tags_header' => 'X-Tags-TRex', @@ -192,7 +189,7 @@ function (RequestInterface $request) { $varnish->invalidateTags(['post-1', 'post,type-3']); } - public function testTagsHeadersSplit() + public function testTagsHeadersSplit(): void { $varnish = new Varnish($this->httpDispatcher, ['header_length' => 7]); $this->httpDispatcher->shouldReceive('invalidate')->twice(); @@ -200,7 +197,7 @@ public function testTagsHeadersSplit() $varnish->invalidateTags(['post-1', 'post-2']); } - public function testPurge() + public function testPurge(): void { $varnish = new Varnish($this->httpDispatcher); @@ -221,7 +218,7 @@ function (RequestInterface $request) { $varnish->purge('/url', ['X-Foo' => 'bar']); } - public function testRefresh() + public function testRefresh(): void { $varnish = new Varnish($this->httpDispatcher); $this->httpDispatcher->shouldReceive('invalidate')->once()->with( diff --git a/tests/Unit/SymfonyCache/CacheEventTest.php b/tests/Unit/SymfonyCache/CacheEventTest.php index a44fcbc30..8d10901f2 100644 --- a/tests/Unit/SymfonyCache/CacheEventTest.php +++ b/tests/Unit/SymfonyCache/CacheEventTest.php @@ -21,17 +21,14 @@ class CacheEventTest extends TestCase { - /** - * @var CacheInvalidation&MockObject - */ - private $kernel; + private CacheInvalidation&MockObject $kernel; public function setUp(): void { $this->kernel = $this->createMock(CacheInvalidation::class); } - public function testEventGetters() + public function testEventGetters(): void { $request = Request::create('/'); diff --git a/tests/Unit/SymfonyCache/CleanupCacheTagsListenerTest.php b/tests/Unit/SymfonyCache/CleanupCacheTagsListenerTest.php index d5106d451..9e8eb8623 100644 --- a/tests/Unit/SymfonyCache/CleanupCacheTagsListenerTest.php +++ b/tests/Unit/SymfonyCache/CleanupCacheTagsListenerTest.php @@ -24,21 +24,21 @@ */ class CleanupCacheTagsListenerTest extends TestCase { - public function testSubscribedEvents() + public function testSubscribedEvents(): void { $this->assertEquals([ Events::POST_HANDLE => 'removeTagsHeader', ], CleanupCacheTagsListener::getSubscribedEvents()); } - public function testNoResponse() + public function testNoResponse(): void { $listener = new CleanupCacheTagsListener(); $listener->removeTagsHeader($this->createEvent()); $this->addToAssertionCount(1); // Nothing should happen, just asserting the "response is null" case } - public function testResponseHeaderIsCleanedUp() + public function testResponseHeaderIsCleanedUp(): void { // Default cache tags header $response = new Response(); @@ -59,7 +59,7 @@ public function testResponseHeaderIsCleanedUp() $this->assertFalse($response->headers->has('Foobar')); } - private function createEvent(?Response $response = null) + private function createEvent(?Response $response = null): CacheEvent { return new CacheEvent( $this->createMock(CacheInvalidation::class), diff --git a/tests/Unit/SymfonyCache/CustomTtlListenerTest.php b/tests/Unit/SymfonyCache/CustomTtlListenerTest.php index 7c86198db..e86b077e7 100644 --- a/tests/Unit/SymfonyCache/CustomTtlListenerTest.php +++ b/tests/Unit/SymfonyCache/CustomTtlListenerTest.php @@ -24,17 +24,14 @@ class CustomTtlListenerTest extends TestCase { use MockeryPHPUnitIntegration; - /** - * @var CacheInvalidation&MockInterface - */ - private $kernel; + private CacheInvalidation&MockInterface $kernel; public function setUp(): void { $this->kernel = \Mockery::mock(CacheInvalidation::class); } - public function testCustomTtl() + public function testCustomTtl(): void { $ttlListener = new CustomTtlListener(); $request = Request::create('http://example.com/foo', 'GET'); @@ -52,7 +49,7 @@ public function testCustomTtl() $this->assertSame('60', $response->headers->get(CustomTtlListener::SMAXAGE_BACKUP)); } - public function testCustomTtlNoSmaxage() + public function testCustomTtlNoSmaxage(): void { $ttlListener = new CustomTtlListener(); $request = Request::create('http://example.com/foo', 'GET'); @@ -70,7 +67,7 @@ public function testCustomTtlNoSmaxage() $this->assertSame('false', $response->headers->get(CustomTtlListener::SMAXAGE_BACKUP)); } - public function testNoCustomTtl() + public function testNoCustomTtl(): void { $ttlListener = new CustomTtlListener(); $request = Request::create('http://example.com/foo', 'GET'); @@ -87,7 +84,7 @@ public function testNoCustomTtl() $this->assertFalse($response->headers->has(CustomTtlListener::SMAXAGE_BACKUP)); } - public function testCleanup() + public function testCleanup(): void { $ttlListener = new CustomTtlListener(); $request = Request::create('http://example.com/foo', 'GET'); @@ -108,7 +105,7 @@ public function testCleanup() $this->assertFalse($response->headers->has(CustomTtlListener::SMAXAGE_BACKUP)); } - public function testCleanupNoSmaxage() + public function testCleanupNoSmaxage(): void { $ttlListener = new CustomTtlListener(); $request = Request::create('http://example.com/foo', 'GET'); @@ -128,7 +125,7 @@ public function testCleanupNoSmaxage() $this->assertFalse($response->headers->has(CustomTtlListener::SMAXAGE_BACKUP)); } - public function testCleanupNoCustomTtl() + public function testCleanupNoCustomTtl(): void { $ttlListener = new CustomTtlListener(); $request = Request::create('http://example.com/foo', 'GET'); diff --git a/tests/Unit/SymfonyCache/DebugListenerTest.php b/tests/Unit/SymfonyCache/DebugListenerTest.php index 7ae21debc..33c85e24e 100644 --- a/tests/Unit/SymfonyCache/DebugListenerTest.php +++ b/tests/Unit/SymfonyCache/DebugListenerTest.php @@ -15,6 +15,7 @@ use FOS\HttpCache\SymfonyCache\CacheInvalidation; use FOS\HttpCache\SymfonyCache\DebugListener; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; +use Mockery\MockInterface; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; @@ -24,17 +25,14 @@ class DebugListenerTest extends TestCase { use MockeryPHPUnitIntegration; - /** - * @var CacheInvalidation&MockObject - */ - private $kernel; + private CacheInvalidation&MockInterface $kernel; public function setUp(): void { $this->kernel = \Mockery::mock(CacheInvalidation::class); } - public function testDebugHit() + public function testDebugHit(): void { $debugListener = new DebugListener(); $request = Request::create('http://example.com/foo', 'GET'); @@ -50,7 +48,7 @@ public function testDebugHit() $this->assertSame('HIT', $response->headers->get('X-Cache')); } - public function testDebugMiss() + public function testDebugMiss(): void { $debugListener = new DebugListener(); $request = Request::create('http://example.com/foo', 'GET'); @@ -66,7 +64,7 @@ public function testDebugMiss() $this->assertSame('MISS', $response->headers->get('X-Cache')); } - public function testDebugUndefined() + public function testDebugUndefined(): void { $debugListener = new DebugListener(); $request = Request::create('http://example.com/foo', 'GET'); @@ -82,7 +80,7 @@ public function testDebugUndefined() $this->assertSame('UNDETERMINED', $response->headers->get('X-Cache')); } - public function testNoHeader() + public function testNoHeader(): void { $debugListener = new DebugListener(); $request = Request::create('http://example.com/foo', 'GET'); diff --git a/tests/Unit/SymfonyCache/EventDispatchingHttpCacheTest.php b/tests/Unit/SymfonyCache/EventDispatchingHttpCacheTest.php index fcaea7f7e..cd778bc82 100644 --- a/tests/Unit/SymfonyCache/EventDispatchingHttpCacheTest.php +++ b/tests/Unit/SymfonyCache/EventDispatchingHttpCacheTest.php @@ -23,7 +23,7 @@ */ class EventDispatchingHttpCacheTest extends EventDispatchingHttpCacheTestCase { - protected function getCacheClass() + protected function getCacheClass(): string { return AppCache::class; } diff --git a/tests/Unit/SymfonyCache/KernelDispatcherTest.php b/tests/Unit/SymfonyCache/KernelDispatcherTest.php index 6047228d8..60ef27dba 100644 --- a/tests/Unit/SymfonyCache/KernelDispatcherTest.php +++ b/tests/Unit/SymfonyCache/KernelDispatcherTest.php @@ -25,7 +25,7 @@ */ class KernelDispatcherTest extends TestCase { - public function testFlush() + public function testFlush(): void { $httpCache = $this->createMock(HttpCache::class); $httpCache->expects($this->once()) @@ -37,10 +37,10 @@ public function testFlush() $valid = $valid && 'PURGETAGS' === $request->getMethod(); $valid = $valid && 'foobar' === $request->headers->get('content-type'); $valid = $valid && 'foo,bar,stuff' === $request->headers->get('x-cache-tags'); - $valid = $valid && ['query' => 'string', 'more' => 'stuff'] == $request->query->all(); - $valid = $valid && 'awesome' == $request->cookies->get('foscacheis'); - $valid = $valid && 'bar' == $request->cookies->get('foo'); - $valid = $valid && 'super content' == $request->getContent(); + $valid = $valid && ['query' => 'string', 'more' => 'stuff'] === $request->query->all(); + $valid = $valid && 'awesome' === $request->cookies->get('foscacheis'); + $valid = $valid && 'bar' === $request->cookies->get('foo'); + $valid = $valid && 'super content' === $request->getContent(); return $valid; })) @@ -57,7 +57,7 @@ public function testFlush() $dispatcher->flush(); } - public function testFlushWithoutHttpCache() + public function testFlushWithoutHttpCache(): void { $this->expectException(ProxyUnreachableException::class); $this->expectExceptionMessage('Kernel did not return a HttpCache instance. Did you forget $kernel->setHttpCache($cacheKernel) in your front controller?'); @@ -76,7 +76,7 @@ public function testFlushWithoutHttpCache() /** * @return Psr7Request */ - private function getRequest() + private function getRequest(): Psr7Request { $headers = [ 'Content-Type' => 'foobar', diff --git a/tests/Unit/SymfonyCache/PurgeListenerTest.php b/tests/Unit/SymfonyCache/PurgeListenerTest.php index f22cd5837..a8396bd39 100644 --- a/tests/Unit/SymfonyCache/PurgeListenerTest.php +++ b/tests/Unit/SymfonyCache/PurgeListenerTest.php @@ -31,7 +31,7 @@ class PurgeListenerTest extends TestCase /** * This tests a sanity check in the AbstractControlledListener. */ - public function testConstructorOverspecified() + public function testConstructorOverspecified(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('You may not set both a request matcher and an IP'); @@ -41,7 +41,7 @@ public function testConstructorOverspecified() ]); } - public function testPurgeAllowed() + public function testPurgeAllowed(): void { /** @var StoreInterface $store */ $store = \Mockery::mock(StoreInterface::class) @@ -63,7 +63,7 @@ public function testPurgeAllowed() $this->assertSame(200, $response->getStatusCode()); } - public function testClearCache() + public function testClearCache(): void { if (!class_exists(Psr6Store::class)) { $this->markTestSkipped('Needs PSR-6 store to be installed.'); @@ -88,7 +88,7 @@ public function testClearCache() $this->assertSame(200, $response->getStatusCode()); } - public function testClearCacheWithoutPsr6Store() + public function testClearCacheWithoutPsr6Store(): void { /** @var StoreInterface $store */ $store = \Mockery::mock(StoreInterface::class); @@ -104,7 +104,7 @@ public function testClearCacheWithoutPsr6Store() $this->assertSame('Store must be an instance of Toflar\Psr6HttpCacheStore\ClearableInterface. Please check your proxy configuration.', $response->getContent()); } - public function testPurgeAllowedMiss() + public function testPurgeAllowedMiss(): void { /** @var StoreInterface $store */ $store = \Mockery::mock(StoreInterface::class) @@ -126,7 +126,7 @@ public function testPurgeAllowedMiss() $this->assertSame(200, $response->getStatusCode()); } - public function testPurgeForbiddenMatcher() + public function testPurgeForbiddenMatcher(): void { $kernel = $this->getUnusedKernelMock(); @@ -142,7 +142,7 @@ public function testPurgeForbiddenMatcher() $this->assertSame(400, $response->getStatusCode()); } - public function testPurgeForbiddenIp() + public function testPurgeForbiddenIp(): void { $kernel = $this->getUnusedKernelMock(); @@ -160,7 +160,7 @@ public function testPurgeForbiddenIp() /** * Configuring the method to something else should make this listener skip the request. */ - public function testOtherMethod() + public function testOtherMethod(): void { $kernel = $this->getUnusedKernelMock(); $matcher = \Mockery::mock(RequestMatcherInterface::class) @@ -178,17 +178,14 @@ public function testOtherMethod() $this->assertNull($event->getResponse()); } - public function testInvalidConfiguration() + public function testInvalidConfiguration(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('does not exist'); new PurgeListener(['stuff' => '1.2.3.4']); } - /** - * @return CacheInvalidation|MockInterface - */ - private function getKernelMock(StoreInterface $store) + private function getKernelMock(StoreInterface $store): MockInterface&CacheInvalidation { return \Mockery::mock(CacheInvalidation::class) ->shouldReceive('getStore') @@ -197,10 +194,7 @@ private function getKernelMock(StoreInterface $store) ->getMock(); } - /** - * @return CacheInvalidation|MockInterface - */ - private function getUnusedKernelMock() + private function getUnusedKernelMock(): CacheInvalidation&MockInterface { return \Mockery::mock(CacheInvalidation::class) ->shouldNotReceive('getStore') diff --git a/tests/Unit/SymfonyCache/PurgeTagsListenerTest.php b/tests/Unit/SymfonyCache/PurgeTagsListenerTest.php index d89984551..8e06ff302 100644 --- a/tests/Unit/SymfonyCache/PurgeTagsListenerTest.php +++ b/tests/Unit/SymfonyCache/PurgeTagsListenerTest.php @@ -39,7 +39,7 @@ public function setUp(): void /** * This tests a sanity check in the AbstractControlledListener. */ - public function testConstructorOverspecified() + public function testConstructorOverspecified(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('You may not set both a request matcher and an IP'); @@ -49,7 +49,7 @@ public function testConstructorOverspecified() ]); } - public function testBadRequestIfWrongStore() + public function testBadRequestIfWrongStore(): void { /** @var StoreInterface $store */ $store = \Mockery::mock(StoreInterface::class) @@ -73,7 +73,7 @@ public function testBadRequestIfWrongStore() $this->assertSame('Store must be an instance of '.Psr6StoreInterface::class.'. Please check your proxy configuration.', $response->getContent()); } - public function testPurgeAllowed() + public function testPurgeAllowed(): void { /** @var StoreInterface $store */ $store = \Mockery::mock(Psr6StoreInterface::class) @@ -96,7 +96,7 @@ public function testPurgeAllowed() $this->assertSame(200, $response->getStatusCode()); } - public function testPurgeAllowedMiss() + public function testPurgeAllowedMiss(): void { /** @var StoreInterface $store */ $store = \Mockery::mock(Psr6StoreInterface::class) @@ -119,7 +119,7 @@ public function testPurgeAllowedMiss() $this->assertSame(200, $response->getStatusCode()); } - public function testPurgeForbiddenMatcher() + public function testPurgeForbiddenMatcher(): void { $kernel = $this->getUnusedKernelMock(); @@ -135,7 +135,7 @@ public function testPurgeForbiddenMatcher() $this->assertSame(400, $response->getStatusCode()); } - public function testPurgeForbiddenIp() + public function testPurgeForbiddenIp(): void { $kernel = $this->getUnusedKernelMock(); @@ -153,7 +153,7 @@ public function testPurgeForbiddenIp() /** * Configuring the method to something else should make this listener skip the request. */ - public function testOtherMethod() + public function testOtherMethod(): void { $kernel = $this->getUnusedKernelMock(); $matcher = \Mockery::mock(RequestMatcherInterface::class) @@ -171,17 +171,14 @@ public function testOtherMethod() $this->assertNull($event->getResponse()); } - public function testInvalidConfiguration() + public function testInvalidConfiguration(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('does not exist'); new PurgeTagsListener(['stuff' => '1.2.3.4']); } - /** - * @return CacheInvalidation|MockInterface - */ - private function getKernelMock(StoreInterface $store) + private function getKernelMock(StoreInterface $store): CacheInvalidation&MockInterface { return \Mockery::mock(CacheInvalidation::class) ->shouldReceive('getStore') @@ -190,10 +187,7 @@ private function getKernelMock(StoreInterface $store) ->getMock(); } - /** - * @return CacheInvalidation|MockInterface - */ - private function getUnusedKernelMock() + private function getUnusedKernelMock(): CacheInvalidation&MockInterface { return \Mockery::mock(CacheInvalidation::class) ->shouldNotReceive('getStore') diff --git a/tests/Unit/SymfonyCache/RefreshListenerTest.php b/tests/Unit/SymfonyCache/RefreshListenerTest.php index 57d4070cb..4af5330ac 100644 --- a/tests/Unit/SymfonyCache/RefreshListenerTest.php +++ b/tests/Unit/SymfonyCache/RefreshListenerTest.php @@ -22,17 +22,14 @@ class RefreshListenerTest extends TestCase { - /** - * @var CacheInvalidation&MockObject - */ - private $kernel; + private CacheInvalidation&MockObject $kernel; public function setUp(): void { $this->kernel = $this->createMock(CacheInvalidation::class); } - public function testRefreshAllowed() + public function testRefreshAllowed(): void { $request = Request::create('http://example.com/foo'); $request->headers->addCacheControlDirective('no-cache'); @@ -51,7 +48,7 @@ public function testRefreshAllowed() $this->assertSame($response, $event->getResponse()); } - public function testRefreshForbiddenMatcher() + public function testRefreshForbiddenMatcher(): void { $this->kernel->expects($this->never()) ->method('fetch') @@ -68,7 +65,7 @@ public function testRefreshForbiddenMatcher() $this->assertNull($event->getResponse()); } - public function testRefreshForbiddenIp() + public function testRefreshForbiddenIp(): void { $this->kernel->expects($this->never()) ->method('fetch') @@ -86,7 +83,7 @@ public function testRefreshForbiddenIp() /** * Configuring the method to something else should make this listener skip the request. */ - public function testUnsafe() + public function testUnsafe(): void { $this->kernel->expects($this->never()) ->method('fetch') @@ -105,7 +102,7 @@ public function testUnsafe() /** * Refresh only happens if no-cache is sent. */ - public function testNoRefresh() + public function testNoRefresh(): void { $this->kernel->expects($this->never()) ->method('fetch') @@ -120,7 +117,7 @@ public function testNoRefresh() $this->assertNull($event->getResponse()); } - public function testInvalidConfiguration() + public function testInvalidConfiguration(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('does not exist'); diff --git a/tests/Unit/SymfonyCache/UserContextListenerTest.php b/tests/Unit/SymfonyCache/UserContextListenerTest.php index d2966e6bb..8658c9abc 100644 --- a/tests/Unit/SymfonyCache/UserContextListenerTest.php +++ b/tests/Unit/SymfonyCache/UserContextListenerTest.php @@ -16,6 +16,7 @@ use FOS\HttpCache\SymfonyCache\UserContextListener; use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Mockery\MockInterface; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -24,10 +25,7 @@ class UserContextListenerTest extends TestCase { use MockeryPHPUnitIntegration; - /** - * @var CacheInvalidation|MockInterface - */ - private $kernel; + private CacheInvalidation&MockInterface $kernel; public function setUp(): void { @@ -39,12 +37,11 @@ public function setUp(): void * * @return array */ - public function provideConfigOptions() + public function provideConfigOptions(): array { $userContextListener = new UserContextListener(); $ref = new \ReflectionObject($userContextListener); $prop = $ref->getProperty('options'); - $prop->setAccessible(true); $options = $prop->getValue($userContextListener); $custom = [ @@ -63,7 +60,7 @@ public function provideConfigOptions() /** * @dataProvider provideConfigOptions */ - public function testGenerateUserHashNotAllowed($arg, $options) + public function testGenerateUserHashNotAllowed(array $arg, array $options): void { $userContextListener = new UserContextListener($arg); @@ -82,7 +79,7 @@ public function testGenerateUserHashNotAllowed($arg, $options) /** * @dataProvider provideConfigOptions */ - public function testPassingUserHashNotAllowed($arg, $options) + public function testPassingUserHashNotAllowed(array $arg, array $options): void { $userContextListener = new UserContextListener($arg); @@ -101,7 +98,7 @@ public function testPassingUserHashNotAllowed($arg, $options) /** * @dataProvider provideConfigOptions */ - public function testUserHashAnonymous($arg, $options) + public function testUserHashAnonymous(array $arg, array $options): void { $userContextListener = new UserContextListener($arg); $request = new Request(); @@ -122,7 +119,7 @@ public function testUserHashAnonymous($arg, $options) $expectedContextHash = 'my_generated_hash'; // Just avoid the response to modify the request object, otherwise it's impossible to test objects equality. - /** @var Response|\PHPUnit_Framework_MockObject_MockObject $hashResponse */ + /** @var Response&MockObject $hashResponse */ $hashResponse = $this->getMockBuilder(Response::class) ->setMethods(['prepare']) ->getMock(); @@ -134,7 +131,7 @@ public function testUserHashAnonymous($arg, $options) ->once() ->with( \Mockery::on( - function (Request $request) use ($that, $hashRequest) { + static function (Request $request) use ($that, $hashRequest) { // we need to call some methods to get the internal fields initialized $request->getMethod(); $request->getPathInfo(); @@ -167,7 +164,7 @@ function (Request $request) use ($that, $hashRequest) { /** * @dataProvider provideConfigOptions */ - public function testUserHashUserWithSession($arg, $options) + public function testUserHashUserWithSession(array $arg, array $options): void { $userContextListener = new UserContextListener($arg); @@ -193,7 +190,7 @@ public function testUserHashUserWithSession($arg, $options) $expectedContextHash = 'my_generated_hash'; // Just avoid the response to modify the request object, otherwise it's impossible to test objects equality. - /** @var Response|\PHPUnit_Framework_MockObject_MockObject $hashResponse */ + /** @var Response&MockObject $hashResponse */ $hashResponse = $this->getMockBuilder(Response::class) ->setMethods(['prepare']) ->getMock(); @@ -205,7 +202,7 @@ public function testUserHashUserWithSession($arg, $options) ->once() ->with( \Mockery::on( - function (Request $request) use ($that, $hashRequest) { + static function (Request $request) use ($that, $hashRequest) { // we need to call some methods to get the internal fields initialized $request->getMethod(); $request->getPathInfo(); @@ -234,7 +231,7 @@ function (Request $request) use ($that, $hashRequest) { * * This test does not have authentication headers and thus considers the request anonymous. */ - public function testUserHashUserIgnoreCookies() + public function testUserHashUserIgnoreCookies(): void { $userContextListener = new UserContextListener([ 'session_name_prefix' => false, @@ -262,7 +259,7 @@ public function testUserHashUserIgnoreCookies() /** * @dataProvider provideConfigOptions */ - public function testUserHashUserWithAuthorizationHeader($arg, $options) + public function testUserHashUserWithAuthorizationHeader(array $arg, array $options): void { $userContextListener = new UserContextListener($arg); @@ -311,14 +308,14 @@ function (Request $request) use ($that, $hashRequest) { $this->assertSame($expectedContextHash, $request->headers->get($options['user_hash_header'])); } - public function testInvalidConfiguration() + public function testInvalidConfiguration(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('does not exist'); new UserContextListener(['foo' => 'bar']); } - public function testHttpMethodParameterOverride() + public function testHttpMethodParameterOverride(): void { $userContextListener = new UserContextListener(); $request = Request::create('/foo', 'POST', ['_method' => 'PUT']); diff --git a/tests/Unit/TagHeaderFormatter/CommaSeparatedTagHeaderFormatterTest.php b/tests/Unit/TagHeaderFormatter/CommaSeparatedTagHeaderFormatterTest.php index 5e4eab1de..4698ade32 100644 --- a/tests/Unit/TagHeaderFormatter/CommaSeparatedTagHeaderFormatterTest.php +++ b/tests/Unit/TagHeaderFormatter/CommaSeparatedTagHeaderFormatterTest.php @@ -17,19 +17,19 @@ class CommaSeparatedTagHeaderFormatterTest extends TestCase { - public function testGetDefaultTagsHeaderName() + public function testGetDefaultTagsHeaderName(): void { $formatter = new CommaSeparatedTagHeaderFormatter(); $this->assertSame('X-Cache-Tags', $formatter->getTagsHeaderName()); } - public function testGetCustomTagsHeaderName() + public function testGetCustomTagsHeaderName(): void { $formatter = new CommaSeparatedTagHeaderFormatter('Foobar'); $this->assertSame('Foobar', $formatter->getTagsHeaderName()); } - public function testGetTagsHeaderValue() + public function testGetTagsHeaderValue(): void { $formatter = new CommaSeparatedTagHeaderFormatter(); @@ -38,7 +38,7 @@ public function testGetTagsHeaderValue() $this->assertSame('tag1,tag2,tag3', $formatter->getTagsHeaderValue(['tag1', 'tag2', 'tag3'])); } - public function testGetCustomGlueTagsHeaderValue() + public function testGetCustomGlueTagsHeaderValue(): void { $formatter = new CommaSeparatedTagHeaderFormatter(TagHeaderFormatter::DEFAULT_HEADER_NAME, ' '); @@ -47,7 +47,7 @@ public function testGetCustomGlueTagsHeaderValue() $this->assertSame('tag1 tag2 tag3', $formatter->getTagsHeaderValue(['tag1', 'tag2', 'tag3'])); } - public function testParseTagsHeaderValue() + public function testParseTagsHeaderValue(): void { $parser = new CommaSeparatedTagHeaderFormatter(); @@ -55,7 +55,7 @@ public function testParseTagsHeaderValue() $this->assertSame(['a', 'b', 'c'], $parser->parseTagsHeaderValue(['a', 'b,c'])); } - public function testParseCustomGlueTagsHeaderValue() + public function testParseCustomGlueTagsHeaderValue(): void { $parser = new CommaSeparatedTagHeaderFormatter(TagHeaderFormatter::DEFAULT_HEADER_NAME, ' '); diff --git a/tests/Unit/TagHeaderFormatter/MaxHeaderValueLengthFormatterTest.php b/tests/Unit/TagHeaderFormatter/MaxHeaderValueLengthFormatterTest.php index 9d7e26979..64b77313c 100644 --- a/tests/Unit/TagHeaderFormatter/MaxHeaderValueLengthFormatterTest.php +++ b/tests/Unit/TagHeaderFormatter/MaxHeaderValueLengthFormatterTest.php @@ -18,13 +18,13 @@ class MaxHeaderValueLengthFormatterTest extends TestCase { - public function testGetTagsHeaderName() + public function testGetTagsHeaderName(): void { $formatter = $this->getFormatter(50); $this->assertSame('X-Cache-Tags', $formatter->getTagsHeaderName()); } - public function testNotTooLong() + public function testNotTooLong(): void { $formatter = $this->getFormatter(50); $tags = ['foo', 'bar', 'baz']; @@ -38,13 +38,13 @@ public function testNotTooLong() * @param int $maxLength * @param string[] $tags */ - public function testTooLong($maxLength, $tags, $expectedHeaderValue) + public function testTooLong(int $maxLength, array $tags, string|array $expectedHeaderValue): void { $formatter = $this->getFormatter($maxLength); $this->assertSame($expectedHeaderValue, $formatter->getTagsHeaderValue($tags)); } - public function testOneTagExceedsMaximum() + public function testOneTagExceedsMaximum(): void { $this->expectException(InvalidTagException::class); $this->expectExceptionMessage('You configured a maximum header length of 3 but the tag "way-too-long-tag" is too long.'); @@ -53,12 +53,7 @@ public function testOneTagExceedsMaximum() $formatter->getTagsHeaderValue(['way-too-long-tag']); } - /** - * @param int $maxLength - * - * @return MaxHeaderValueLengthFormatter - */ - private function getFormatter($maxLength) + private function getFormatter(int $maxLength): MaxHeaderValueLengthFormatter { return new MaxHeaderValueLengthFormatter( new CommaSeparatedTagHeaderFormatter(), @@ -66,7 +61,7 @@ private function getFormatter($maxLength) ); } - public function tooLongProvider() + public function tooLongProvider(): array { return [ [3, ['foo', 'bar', 'baz'], ['foo', 'bar', 'baz']], diff --git a/tests/Unit/Test/PHPUnit/IsCacheHitConstraintTest.php b/tests/Unit/Test/PHPUnit/IsCacheHitConstraintTest.php index f01ce617f..a2087f860 100644 --- a/tests/Unit/Test/PHPUnit/IsCacheHitConstraintTest.php +++ b/tests/Unit/Test/PHPUnit/IsCacheHitConstraintTest.php @@ -15,11 +15,6 @@ use GuzzleHttp\Psr7\Stream; use PHPUnit\Framework\ExpectationFailedException; -// phpunit 5 has forward compatibility classes but missed this one -if (!class_exists('\PHPUnit\Framework\ExpectationFailedException')) { - class_alias('\PHPUnit_Framework_ExpectationFailedException', '\PHPUnit\Framework\ExpectationFailedException'); -} - class IsCacheHitConstraintTest extends AbstractCacheConstraintTest { /** diff --git a/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php b/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php index 6ab501691..55553482f 100644 --- a/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php +++ b/tests/Unit/Test/PHPUnit/IsCacheMissConstraintTest.php @@ -14,11 +14,6 @@ use FOS\HttpCache\Test\PHPUnit\IsCacheMissConstraint; use PHPUnit\Framework\ExpectationFailedException; -// phpunit 5 has forward compatibility classes but missed this one -if (!class_exists('\PHPUnit\Framework\ExpectationFailedException')) { - class_alias('\PHPUnit_Framework_ExpectationFailedException', '\PHPUnit\Framework\ExpectationFailedException'); -} - class IsCacheMissConstraintTest extends AbstractCacheConstraintTest { /**