Skip to content

Commit

Permalink
adjust to phpunit 8 BC breaks
Browse files Browse the repository at this point in the history
* have no risky tests
* fix deprecations
  • Loading branch information
dbu committed Dec 21, 2021
1 parent 769fe99 commit 845d4eb
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 75 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ env:

jobs:
latest:
name: PHP ${{ matrix.php }} Varnish ${{ VARNISH_VERSION }}
name: PHP ${{ matrix.php }} Varnish 6
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
include:
- php: '7.2'
Expand Down
4 changes: 0 additions & 4 deletions phpstan.tests.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@ parameters:
level: 1
paths:
- tests
ignoreErrors:
# we can add the return type declaration when dropping support for PHP < 7.4
- '#Method FOS\\HttpCache\\Tests\\Unit\\SymfonyCache\\AppCache::fetch\(\) should return Symfony\\Component\\HttpFoundation\\Response but return statement is missing.#'
- '#Method FOS\\HttpCache\\Tests\\Functional\\Symfony\\AppCache::fetch\(\) should return Symfony\\Component\\HttpFoundation\\Response but return statement is missing.#'
2 changes: 1 addition & 1 deletion src/EventListener/LogListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(LoggerInterface $logger)
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
Events::PROXY_UNREACHABLE_ERROR => 'onProxyUnreachableError',
Expand Down
5 changes: 3 additions & 2 deletions src/ResponseTagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@ public function hasTags()
*
* This must be called before any HTTP response is sent to the client.
*
* @param array $tags List of tags to add
* @param string[] $tags List of tags to add
*
* @throws InvalidTagException
*
* @return $this
*/
public function addTags(array $tags)
{
$filtered = array_filter($tags, 'strlen');
$filtered = array_filter($tags, 'is_string');
$filtered = array_filter($filtered, 'strlen');

if ($this->options['strict'] && array_diff($tags, $filtered)) {
throw new InvalidTagException('Empty tags are not allowed');
Expand Down
2 changes: 1 addition & 1 deletion src/SymfonyCache/CleanupCacheTagsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function removeTagsHeader(CacheEvent $e)
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
Events::POST_HANDLE => 'removeTagsHeader',
Expand Down
2 changes: 1 addition & 1 deletion src/SymfonyCache/CustomTtlListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function cleanResponse(CacheEvent $e)
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
Events::PRE_STORE => 'useCustomTtl',
Expand Down
2 changes: 1 addition & 1 deletion src/SymfonyCache/DebugListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DebugListener implements EventSubscriberInterface
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
Events::POST_HANDLE => 'handleDebug',
Expand Down
8 changes: 4 additions & 4 deletions src/SymfonyCache/EventDispatchingHttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,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)
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response
{
// trigger loading the CacheEvent to avoid fatal error when HttpKernel::loadClassCache is used.
class_exists(CacheEvent::class);
Expand Down Expand Up @@ -122,7 +122,7 @@ protected function store(Request $request, Response $response)
*
* Adding the Events::PRE_INVALIDATE event.
*/
protected function invalidate(Request $request, $catch = false)
protected function invalidate(Request $request, $catch = false): Response
{
if ($response = $this->dispatch(Events::PRE_INVALIDATE, $request)) {
return $response;
Expand All @@ -138,9 +138,9 @@ protected function invalidate(Request $request, $catch = false)
* @param Response|null $response If already available
* @param int $requestType The request type (default HttpKernelInterface::MASTER_REQUEST)
*
* @return Response The response to return, which might be provided/altered by a listener
* @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)
protected function dispatch($name, Request $request, Response $response = null, $requestType = HttpKernelInterface::MASTER_REQUEST): ?Response
{
if ($this->getEventDispatcher()->hasListeners($name)) {
$event = new CacheEvent($this, $request, $response, $requestType);
Expand Down
2 changes: 1 addition & 1 deletion src/SymfonyCache/PurgeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(array $options = [])
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
Events::PRE_INVALIDATE => 'handlePurge',
Expand Down
2 changes: 1 addition & 1 deletion src/SymfonyCache/PurgeTagsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct(array $options = [])
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
Events::PRE_INVALIDATE => 'handlePurgeTags',
Expand Down
2 changes: 1 addition & 1 deletion src/SymfonyCache/RefreshListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RefreshListener extends AccessControlledListener
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
Events::PRE_HANDLE => 'handleRefresh',
Expand Down
24 changes: 8 additions & 16 deletions src/SymfonyCache/UserContextListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function __construct(array $options = [])
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
Events::PRE_HANDLE => 'preHandle',
Expand All @@ -103,7 +103,7 @@ public static function getSubscribedEvents()
* Checks if an external request tries tampering with the use context hash mechanism
* to prevent attacks.
*/
public function preHandle(CacheEvent $event)
public function preHandle(CacheEvent $event): void
{
$request = $event->getRequest();
if (!$this->isInternalRequest($request)) {
Expand Down Expand Up @@ -132,7 +132,7 @@ public function preHandle(CacheEvent $event)
* Cleans cookies header to only keep the session identifier cookie, so the hash lookup request
* can be cached per session.
*/
protected function cleanupHashLookupRequest(Request $hashLookupRequest, Request $originalRequest)
protected function cleanupHashLookupRequest(Request $hashLookupRequest, Request $originalRequest): void
{
$sessionIds = [];
if (!$this->options['session_name_prefix']) {
Expand All @@ -155,20 +155,16 @@ protected function cleanupHashLookupRequest(Request $hashLookupRequest, Request

/**
* Checks if passed request object is to be considered internal (e.g. for user hash lookup).
*
* @return bool
*/
private function isInternalRequest(Request $request)
private function isInternalRequest(Request $request): bool
{
return true === $request->attributes->get('internalRequest', false);
}

/**
* Returns the user context hash for $request.
*
* @return string
*/
private function getUserHash(HttpKernelInterface $kernel, Request $request)
private function getUserHash(HttpKernelInterface $kernel, Request $request): ?string
{
if (isset($this->userHash)) {
return $this->userHash;
Expand All @@ -189,10 +185,8 @@ private function getUserHash(HttpKernelInterface $kernel, Request $request)

/**
* Checks if current request is considered anonymous.
*
* @return bool
*/
private function isAnonymous(Request $request)
private function isAnonymous(Request $request): bool
{
$anonymousRequestMatcher = new AnonymousRequestMatcher([
'user_identifier_headers' => $this->options['user_identifier_headers'],
Expand All @@ -206,10 +200,8 @@ private function isAnonymous(Request $request)
* Checks if passed string can be considered as a session name, such as would be used in cookies.
*
* @param string $name
*
* @return bool
*/
private function isSessionName($name)
private function isSessionName($name): bool
{
return 0 === strpos($name, $this->options['session_name_prefix']);
}
Expand All @@ -219,7 +211,7 @@ private function isSessionName($name)
*
* @return Request the request that will return the user context hash value
*/
private function generateHashLookupRequest(Request $request)
private function generateHashLookupRequest(Request $request): Request
{
$hashLookupRequest = Request::create($this->options['user_hash_uri'], $this->options['user_hash_method'], [], [], [], $request->server->all());
$hashLookupRequest->attributes->set('internalRequest', true);
Expand Down
2 changes: 1 addition & 1 deletion src/Test/EventDispatchingHttpCacheTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public function __construct(
$this->request = $request;
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return [
Events::PRE_HANDLE => 'preHandle',
Expand Down
2 changes: 1 addition & 1 deletion src/Test/WebServerListenerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function startPhpWebServer()
{
$command = sprintf(
'php -S %s:%d -t %s >/dev/null 2>&1 & echo $!',
'127.0.0.1', // on travis, localhost is not 127.0.0.1 but IPv6 ::1
'127.0.0.1',
$this->getPort(),
$this->getDocRoot()
);
Expand Down
2 changes: 1 addition & 1 deletion src/UserContext/AnonymousRequestMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(array $options = [])
$this->options = $resolver->resolve($options);
}

public function matches(Request $request)
public function matches(Request $request): bool
{
// You might have to enable rewriting of the Authorization header in your server config or .htaccess:
// RewriteEngine On
Expand Down
2 changes: 1 addition & 1 deletion src/UserContext/UserContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getParameters()
return $this->parameters;
}

public function getIterator()
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->parameters);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Symfony/EventDispatchingHttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ class AppCache extends HttpCache implements CacheInvalidation
*/
public function fetch(Request $request, $catch = false): Response
{
parent::fetch($request, $catch);
return parent::fetch($request, $catch);
}
}
52 changes: 17 additions & 35 deletions tests/Unit/CacheInvalidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,46 +158,28 @@ public function testInvalidateRegex()
$cacheInvalidator->invalidateRegex('/a', 'b', ['example.com']);
}

public function testMethodException()
public function provideOperations(): iterable
{
yield from [
['invalidatePath', '/'],
['refreshPath', '/'],
['invalidate', []],
['invalidateRegex', '/'],
['invalidateTags', []],
];
}

/**
* @dataProvider provideOperations
*/
public function testMethodException(string $method, $arg): void
{
/** @var MockInterface&ProxyClient $proxyClient */
$proxyClient = \Mockery::mock(ProxyClient::class);
$cacheInvalidator = new CacheInvalidator($proxyClient);

try {
$cacheInvalidator->invalidatePath('/');
$this->fail('Expected exception');
} catch (UnsupportedProxyOperationException $e) {
// success
}

try {
$cacheInvalidator->refreshPath('/');
$this->fail('Expected exception');
} catch (UnsupportedProxyOperationException $e) {
// success
}

try {
$cacheInvalidator->invalidate([]);
$this->fail('Expected exception');
} catch (UnsupportedProxyOperationException $e) {
// success
}

try {
$cacheInvalidator->invalidateRegex('/');
$this->fail('Expected exception');
} catch (UnsupportedProxyOperationException $e) {
// success
}

try {
$cacheInvalidator->invalidateTags([]);
$this->fail('Expected exception');
} catch (UnsupportedProxyOperationException $e) {
// success
}
$this->expectException(UnsupportedProxyOperationException::class);
$cacheInvalidator->$method($arg);
}

public function testProxyClientExceptionsAreLogged()
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/ProxyClient/HttpDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ public function testBanWithoutBaseUri()

$request = $this->messageFactory->createRequest('BAN', '/', ['X-Url' => '/foo/.*']);
$httpDispatcher->invalidate($request, false);
$httpDispatcher->flush();

$requests = $this->getRequests();
$this->assertSame('127.0.0.1:123', $requests[0]->getHeaderLine('Host'));
}

public function testSetBasePathWithHost()
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/SymfonyCache/EventDispatchingHttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ class AppCache extends HttpCache implements CacheInvalidation
*/
public function fetch(Request $request, $catch = false): Response
{
parent::fetch($request, $catch);
return parent::fetch($request, $catch);
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Test/Proxy/SymfonyProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public function testStart()
{
$proxy = new SymfonyProxy();
$proxy->start();
$this->addToAssertionCount(1);
$proxy->stop();
$this->addToAssertionCount(1);
}

public function testInvalidDirectoryThrowsException()
Expand Down

0 comments on commit 845d4eb

Please sign in to comment.