Skip to content

Commit

Permalink
Merge pull request #619 from FriendsOfSymfony/phpunit-10
Browse files Browse the repository at this point in the history
upgrade to phpunit 10 and adjust to FOSHttpCache strict typing
  • Loading branch information
dbu committed Apr 11, 2024
2 parents 49c8832 + 5b10edf commit 1dbe069
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 194 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Expand Up @@ -67,10 +67,10 @@ jobs:
if: ${{ matrix.stability != 'dev' }}

- name: Run tests
run: php vendor/bin/phpunit -v
run: php vendor/bin/phpunit
if: ${{ matrix.stability != 'dev' }}

- name: Run tests allow to fail
run: php vendor/bin/phpunit -v || true
run: php vendor/bin/phpunit || true
continue-on-error: true
if: ${{ matrix.stability == 'dev' }}
2 changes: 1 addition & 1 deletion composer.json
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
38 changes: 17 additions & 21 deletions phpunit.xml.dist
@@ -1,10 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./tests/bootstrap.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true">
cacheDirectory=".phpunit.cache"
>
<source>
<include>
<directory>./src</directory>
</include>
<exclude>
<directory>./src/Resources</directory>
</exclude>
</source>

<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">./tests/Unit</directory>
Expand All @@ -14,22 +23,9 @@
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
<exclude>
<directory>./src/Resources</directory>
</exclude>
</whitelist>
</filter>

<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener" />
</listeners>

<php>
<env name="KERNEL_DIR" value="./tests/Functional/Fixtures/app" />
<env name="KERNEL_CLASS" value="AppKernel" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
<env name="KERNEL_DIR" value="./tests/Functional/Fixtures/app"/>
<env name="KERNEL_CLASS" value="AppKernel"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
</php>
</phpunit>
31 changes: 8 additions & 23 deletions src/CacheManager.php
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
6 changes: 4 additions & 2 deletions src/DependencyInjection/FOSHttpCacheExtension.php
Expand Up @@ -382,6 +382,7 @@ private function createHttpDispatcherDefinition(ContainerBuilder $container, arr
}
$config['servers'] = $config['servers_from_jsonenv'];
}

if (!empty($config['base_url'])) {
$baseUrl = $config['base_url'];
$usedEnvs = [];
Expand All @@ -391,8 +392,9 @@ private function createHttpDispatcherDefinition(ContainerBuilder $container, arr
$this->validateUrl($baseUrl, 'Not a valid base path: "%s"');
}
} else {
$baseUrl = null;
$baseUrl = '';
}

$httpClient = null;
if ($config['http_client']) {
$httpClient = new Reference($config['http_client']);
Expand Down Expand Up @@ -629,7 +631,7 @@ private function validateUrl($url, $msg): void
{
$prefixed = $this->prefixSchema($url);

if (!$parts = parse_url($prefixed)) {
if (!parse_url($prefixed)) {
throw new InvalidConfigurationException(sprintf($msg, $url));
}
}
Expand Down
7 changes: 3 additions & 4 deletions tests/Functional/EventListener/InvalidationListenerTest.php
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
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
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
2 changes: 1 addition & 1 deletion tests/Unit/CacheManagerTest.php
Expand Up @@ -38,7 +38,7 @@ public function testInvalidateRoute()
->shouldReceive('purge')->once()->with('/my/route', [])
->shouldReceive('purge')->once()->with('/route/with/params/id/123', [])
->shouldReceive('purge')->once()->with('/route/with/params/id/123', ['X-Foo' => 'bar'])
->shouldReceive('flush')->once()
->shouldReceive('flush')->once()->andReturn(2)
->getMock();

$router = \Mockery::mock(UrlGeneratorInterface::class)
Expand Down
11 changes: 5 additions & 6 deletions tests/Unit/Command/PathSanityCheckTest.php
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

0 comments on commit 1dbe069

Please sign in to comment.