Skip to content

Commit

Permalink
upgrade to phpunit 10 and adjust to FOSHttpCache strict typing
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Mar 28, 2024
1 parent 49c8832 commit 57a44f7
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 165 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"symfony/browser-kit": "^6.4 || ^7.0",
"symfony/console": "^6.4 || ^7.0",
"symfony/finder": "^6.4 || ^7.0",
"phpunit/phpunit": "^9.6.15",
"phpunit/phpunit": "^10.5",
"symfony/security-bundle": "^6.4 || ^7.0",
"symfony/twig-bundle": "^6.4 || ^7.0",
"twig/twig": "^v3.8",
Expand Down
31 changes: 8 additions & 23 deletions src/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,14 @@
*/
class CacheManager extends CacheInvalidator
{
/**
* @var ProxyClient
*/
private $cache;
private ProxyClient $cache;

/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;
private UrlGeneratorInterface $urlGenerator;

/**
* What type of urls to generate.
*
* @var bool|string
*/
private $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH;
private int $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH;

/**
* Constructor.
Expand All @@ -57,9 +49,9 @@ public function __construct(ProxyClient $cache, UrlGeneratorInterface $urlGenera
/**
* Set what type of URLs to generate.
*
* @param bool|string $generateUrlType One of the constants in UrlGeneratorInterface
* @param int $generateUrlType One of the constants in UrlGeneratorInterface
*/
public function setGenerateUrlType($generateUrlType)
public function setGenerateUrlType(int $generateUrlType): void
{
$this->generateUrlType = $generateUrlType;
}
Expand All @@ -73,7 +65,7 @@ public function setGenerateUrlType($generateUrlType)
*
* @return $this
*/
public function invalidateRoute($name, array $parameters = [], array $headers = [])
public function invalidateRoute(string $name, array $parameters = [], array $headers = []): static
{
$this->invalidatePath($this->urlGenerator->generate($name, $parameters, $this->generateUrlType), $headers);

Expand All @@ -89,21 +81,14 @@ public function invalidateRoute($name, array $parameters = [], array $headers =
*
* @return $this
*/
public function refreshRoute($route, array $parameters = [], array $headers = [])
public function refreshRoute(string $route, array $parameters = [], array $headers = []): static
{
$this->refreshPath($this->urlGenerator->generate($route, $parameters, $this->generateUrlType), $headers);

return $this;
}

/**
* Send all pending invalidation requests.
*
* @return int the number of cache invalidations performed per caching server
*
* @throws \FOS\HttpCache\Exception\ExceptionCollection
*/
public function flush()
public function flush(): int
{
if (!$this->cache instanceof LazyObjectInterface || $this->cache->isLazyObjectInitialized()) {
return parent::flush();
Expand Down
7 changes: 3 additions & 4 deletions tests/Functional/EventListener/InvalidationListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use FOS\HttpCacheBundle\CacheManager;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\Attributes as PHPUnit;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class InvalidationListenerTest extends WebTestCase
Expand Down Expand Up @@ -49,9 +50,7 @@ public function testInvalidateRoute(): void
$client->request('POST', '/invalidate/route/42');
}

/**
* @dataProvider getStatusCodesThatTriggerInvalidation
*/
#[PHPUnit\DataProvider('getStatusCodesThatTriggerInvalidation')]
public function testInvalidatePath($statusCode): void
{
$client = static::createClient();
Expand Down Expand Up @@ -96,7 +95,7 @@ public function testErrorIsNotInvalidated(): void
$client->request('POST', '/invalidate/error');
}

public function getStatusCodesThatTriggerInvalidation(): array
public static function getStatusCodesThatTriggerInvalidation(): array
{
return [[200], [204], [302]];
}
Expand Down
19 changes: 7 additions & 12 deletions tests/Functional/EventListener/TagListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use FOS\HttpCacheBundle\Configuration\Tag;
use FOS\HttpCacheBundle\EventListener\TagListener;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\Attributes as PHPUnit;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -143,9 +144,7 @@ public function testTwigExtension(): void
$this->assertEquals('tag-from-twig', $response->headers->get('X-Cache-Tags'));
}

/**
* @dataProvider cacheableRequestResponseCombinations
*/
#[PHPUnit\DataProvider('cacheableRequestResponseCombinations')]
public function testTagsAreSetWhenCacheable(Request $request, Response $response): void
{
self::$overrideService = true;
Expand Down Expand Up @@ -175,9 +174,7 @@ public function testTagsAreSetWhenCacheable(Request $request, Response $response
$this->assertEquals('cacheable-is-tagged', $headers->get('X-Cache-Tags'));
}

/**
* @dataProvider mustInvalidateRequestResponseCombinations
*/
#[PHPUnit\DataProvider('mustInvalidateRequestResponseCombinations')]
public function testTagsAreInvalidated(Request $request, Response $response): void
{
self::$overrideService = true;
Expand Down Expand Up @@ -211,9 +208,7 @@ public function testTagsAreInvalidated(Request $request, Response $response): vo
$this->assertFalse($headers->has('X-Cache-Tags'));
}

/**
* @dataProvider mustNotInvalidateRequestResponseCombinations
*/
#[PHPUnit\DataProvider('mustNotInvalidateRequestResponseCombinations')]
public function testTagsAreNotInvalidated(Request $request, Response $response): void
{
self::$overrideService = true;
Expand Down Expand Up @@ -244,7 +239,7 @@ public function testTagsAreNotInvalidated(Request $request, Response $response):
$this->assertFalse($headers->has('X-Cache-Tags'));
}

public function cacheableRequestResponseCombinations(): array
public static function cacheableRequestResponseCombinations(): array
{
return [
[Request::create('', 'GET'), new Response('', 200)],
Expand All @@ -254,15 +249,15 @@ public function cacheableRequestResponseCombinations(): array
];
}

public function mustInvalidateRequestResponseCombinations(): array
public static function mustInvalidateRequestResponseCombinations(): array
{
return [
// https://github.com/FriendsOfSymfony/FOSHttpCacheBundle/issues/241
[Request::create('', 'POST'), new Response('', 201)],
];
}

public function mustNotInvalidateRequestResponseCombinations(): array
public static function mustNotInvalidateRequestResponseCombinations(): array
{
return [
// https://github.com/FriendsOfSymfony/FOSHttpCacheBundle/issues/279
Expand Down
13 changes: 5 additions & 8 deletions tests/Functional/EventListener/UserContextListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
namespace FOS\HttpCacheBundle\Tests\Functional\EventListener;

use FOS\HttpCacheBundle\Tests\Functional\SessionHelperTrait;
use PHPUnit\Framework\Attributes as PHPUnit;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpKernel\Event\RequestEvent;

class UserContextListenerTest extends WebTestCase
{
use SessionHelperTrait;

/**
* @dataProvider userHashDataProvider
*/
#[PHPUnit\DataProvider('userHashDataProvider')]
public function testHashLookup(string $username, string $hash)
{
// as we tamper with the session id, make sure no previous session is around
Expand All @@ -48,10 +47,8 @@ public function testHashLookup(string $username, string $hash)
$this->assertSame('max-age=60, public', $response->headers->get('Cache-Control'));
}

/**
* @dataProvider userHashDataProvider
*/
public function testSessionCanBeCached(string $username, string $hash)
#[PHPUnit\DataProvider('userHashDataProvider')]
public function testSessionCanBeCached(string $username, string $hash): void
{
$client = static::createClient([], $username ? [
'PHP_AUTH_USER' => $username,
Expand All @@ -66,7 +63,7 @@ public function testSessionCanBeCached(string $username, string $hash)
$this->assertEquals('max-age=60, public', $response->headers->get('Cache-Control'));
}

public function userHashDataProvider()
public static function userHashDataProvider(): \Generator
{
yield 'anonymous' => ['', '5224d8f5b85429624e2160e538a3376a479ec87b89251b295c44ecbf7498ea3c'];
yield 'user' => ['user', '14cea38921d7f2284a52ac67eafb9ed5d30bed84684711591747d9110cae8be9'];
Expand Down
11 changes: 5 additions & 6 deletions tests/Unit/Command/PathSanityCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@

use FOS\HttpCacheBundle\Command\PathSanityCheck;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;

class PathSanityCheckTest extends TestCase
{
use MockeryPHPUnitIntegration;

public function pathProvider()
public static function pathProvider(): array
{
return [
[false, '/foo'],
Expand All @@ -32,10 +33,8 @@ public function pathProvider()
];
}

/**
* @dataProvider pathProvider
*/
public function testLooksLikeRegularExpression($expected, $path)
#[PHPUnit\DataProvider('pathProvider')]
public function testLooksLikeRegularExpression(bool $expected, string $path): void
{
$sanityChecking = new SanityChecking();
$this->assertEquals($expected, $sanityChecking->looksLikeRegularExpression($path));
Expand All @@ -48,7 +47,7 @@ class SanityChecking
looksLikeRegularExpression as traitFunction;
}

public function looksLikeRegularExpression($path)
public function looksLikeRegularExpression(string $path): bool
{
return $this->traitFunction($path);
}
Expand Down
Loading

0 comments on commit 57a44f7

Please sign in to comment.