Skip to content

Commit 5b10edf

Browse files
committed
upgrade to phpunit 10 and adjust to FOSHttpCache strict typing
1 parent 49c8832 commit 5b10edf

13 files changed

+155
-194
lines changed

.github/workflows/php.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ jobs:
6767
if: ${{ matrix.stability != 'dev' }}
6868

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

7373
- name: Run tests allow to fail
74-
run: php vendor/bin/phpunit -v || true
74+
run: php vendor/bin/phpunit || true
7575
continue-on-error: true
7676
if: ${{ matrix.stability == 'dev' }}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"symfony/browser-kit": "^6.4 || ^7.0",
3939
"symfony/console": "^6.4 || ^7.0",
4040
"symfony/finder": "^6.4 || ^7.0",
41-
"phpunit/phpunit": "^9.6.15",
41+
"phpunit/phpunit": "^10.5",
4242
"symfony/security-bundle": "^6.4 || ^7.0",
4343
"symfony/twig-bundle": "^6.4 || ^7.0",
4444
"twig/twig": "^v3.8",

phpunit.xml.dist

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
3-
<phpunit bootstrap="./tests/bootstrap.php"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
bootstrap="./tests/bootstrap.php"
45
colors="true"
5-
convertErrorsToExceptions="true"
6-
convertNoticesToExceptions="true"
7-
convertWarningsToExceptions="true">
6+
cacheDirectory=".phpunit.cache"
7+
>
8+
<source>
9+
<include>
10+
<directory>./src</directory>
11+
</include>
12+
<exclude>
13+
<directory>./src/Resources</directory>
14+
</exclude>
15+
</source>
16+
817
<testsuites>
918
<testsuite name="unit">
1019
<directory suffix="Test.php">./tests/Unit</directory>
@@ -14,22 +23,9 @@
1423
</testsuite>
1524
</testsuites>
1625

17-
<filter>
18-
<whitelist>
19-
<directory>./src</directory>
20-
<exclude>
21-
<directory>./src/Resources</directory>
22-
</exclude>
23-
</whitelist>
24-
</filter>
25-
26-
<listeners>
27-
<listener class="\Mockery\Adapter\Phpunit\TestListener" />
28-
</listeners>
29-
3026
<php>
31-
<env name="KERNEL_DIR" value="./tests/Functional/Fixtures/app" />
32-
<env name="KERNEL_CLASS" value="AppKernel" />
33-
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
27+
<env name="KERNEL_DIR" value="./tests/Functional/Fixtures/app"/>
28+
<env name="KERNEL_CLASS" value="AppKernel"/>
29+
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
3430
</php>
3531
</phpunit>

src/CacheManager.php

+8-23
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,14 @@
2424
*/
2525
class CacheManager extends CacheInvalidator
2626
{
27-
/**
28-
* @var ProxyClient
29-
*/
30-
private $cache;
27+
private ProxyClient $cache;
3128

32-
/**
33-
* @var UrlGeneratorInterface
34-
*/
35-
private $urlGenerator;
29+
private UrlGeneratorInterface $urlGenerator;
3630

3731
/**
3832
* What type of urls to generate.
39-
*
40-
* @var bool|string
4133
*/
42-
private $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH;
34+
private int $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH;
4335

4436
/**
4537
* Constructor.
@@ -57,9 +49,9 @@ public function __construct(ProxyClient $cache, UrlGeneratorInterface $urlGenera
5749
/**
5850
* Set what type of URLs to generate.
5951
*
60-
* @param bool|string $generateUrlType One of the constants in UrlGeneratorInterface
52+
* @param int $generateUrlType One of the constants in UrlGeneratorInterface
6153
*/
62-
public function setGenerateUrlType($generateUrlType)
54+
public function setGenerateUrlType(int $generateUrlType): void
6355
{
6456
$this->generateUrlType = $generateUrlType;
6557
}
@@ -73,7 +65,7 @@ public function setGenerateUrlType($generateUrlType)
7365
*
7466
* @return $this
7567
*/
76-
public function invalidateRoute($name, array $parameters = [], array $headers = [])
68+
public function invalidateRoute(string $name, array $parameters = [], array $headers = []): static
7769
{
7870
$this->invalidatePath($this->urlGenerator->generate($name, $parameters, $this->generateUrlType), $headers);
7971

@@ -89,21 +81,14 @@ public function invalidateRoute($name, array $parameters = [], array $headers =
8981
*
9082
* @return $this
9183
*/
92-
public function refreshRoute($route, array $parameters = [], array $headers = [])
84+
public function refreshRoute(string $route, array $parameters = [], array $headers = []): static
9385
{
9486
$this->refreshPath($this->urlGenerator->generate($route, $parameters, $this->generateUrlType), $headers);
9587

9688
return $this;
9789
}
9890

99-
/**
100-
* Send all pending invalidation requests.
101-
*
102-
* @return int the number of cache invalidations performed per caching server
103-
*
104-
* @throws \FOS\HttpCache\Exception\ExceptionCollection
105-
*/
106-
public function flush()
91+
public function flush(): int
10792
{
10893
if (!$this->cache instanceof LazyObjectInterface || $this->cache->isLazyObjectInitialized()) {
10994
return parent::flush();

src/DependencyInjection/FOSHttpCacheExtension.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ private function createHttpDispatcherDefinition(ContainerBuilder $container, arr
382382
}
383383
$config['servers'] = $config['servers_from_jsonenv'];
384384
}
385+
385386
if (!empty($config['base_url'])) {
386387
$baseUrl = $config['base_url'];
387388
$usedEnvs = [];
@@ -391,8 +392,9 @@ private function createHttpDispatcherDefinition(ContainerBuilder $container, arr
391392
$this->validateUrl($baseUrl, 'Not a valid base path: "%s"');
392393
}
393394
} else {
394-
$baseUrl = null;
395+
$baseUrl = '';
395396
}
397+
396398
$httpClient = null;
397399
if ($config['http_client']) {
398400
$httpClient = new Reference($config['http_client']);
@@ -629,7 +631,7 @@ private function validateUrl($url, $msg): void
629631
{
630632
$prefixed = $this->prefixSchema($url);
631633

632-
if (!$parts = parse_url($prefixed)) {
634+
if (!parse_url($prefixed)) {
633635
throw new InvalidConfigurationException(sprintf($msg, $url));
634636
}
635637
}

tests/Functional/EventListener/InvalidationListenerTest.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use FOS\HttpCacheBundle\CacheManager;
1515
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
16+
use PHPUnit\Framework\Attributes as PHPUnit;
1617
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1718

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

52-
/**
53-
* @dataProvider getStatusCodesThatTriggerInvalidation
54-
*/
53+
#[PHPUnit\DataProvider('getStatusCodesThatTriggerInvalidation')]
5554
public function testInvalidatePath($statusCode): void
5655
{
5756
$client = static::createClient();
@@ -96,7 +95,7 @@ public function testErrorIsNotInvalidated(): void
9695
$client->request('POST', '/invalidate/error');
9796
}
9897

99-
public function getStatusCodesThatTriggerInvalidation(): array
98+
public static function getStatusCodesThatTriggerInvalidation(): array
10099
{
101100
return [[200], [204], [302]];
102101
}

tests/Functional/EventListener/TagListenerTest.php

+7-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use FOS\HttpCacheBundle\Configuration\Tag;
1616
use FOS\HttpCacheBundle\EventListener\TagListener;
1717
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
18+
use PHPUnit\Framework\Attributes as PHPUnit;
1819
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1920
use Symfony\Component\HttpFoundation\Request;
2021
use Symfony\Component\HttpFoundation\Response;
@@ -143,9 +144,7 @@ public function testTwigExtension(): void
143144
$this->assertEquals('tag-from-twig', $response->headers->get('X-Cache-Tags'));
144145
}
145146

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

178-
/**
179-
* @dataProvider mustInvalidateRequestResponseCombinations
180-
*/
177+
#[PHPUnit\DataProvider('mustInvalidateRequestResponseCombinations')]
181178
public function testTagsAreInvalidated(Request $request, Response $response): void
182179
{
183180
self::$overrideService = true;
@@ -211,9 +208,7 @@ public function testTagsAreInvalidated(Request $request, Response $response): vo
211208
$this->assertFalse($headers->has('X-Cache-Tags'));
212209
}
213210

214-
/**
215-
* @dataProvider mustNotInvalidateRequestResponseCombinations
216-
*/
211+
#[PHPUnit\DataProvider('mustNotInvalidateRequestResponseCombinations')]
217212
public function testTagsAreNotInvalidated(Request $request, Response $response): void
218213
{
219214
self::$overrideService = true;
@@ -244,7 +239,7 @@ public function testTagsAreNotInvalidated(Request $request, Response $response):
244239
$this->assertFalse($headers->has('X-Cache-Tags'));
245240
}
246241

247-
public function cacheableRequestResponseCombinations(): array
242+
public static function cacheableRequestResponseCombinations(): array
248243
{
249244
return [
250245
[Request::create('', 'GET'), new Response('', 200)],
@@ -254,15 +249,15 @@ public function cacheableRequestResponseCombinations(): array
254249
];
255250
}
256251

257-
public function mustInvalidateRequestResponseCombinations(): array
252+
public static function mustInvalidateRequestResponseCombinations(): array
258253
{
259254
return [
260255
// https://github.com/FriendsOfSymfony/FOSHttpCacheBundle/issues/241
261256
[Request::create('', 'POST'), new Response('', 201)],
262257
];
263258
}
264259

265-
public function mustNotInvalidateRequestResponseCombinations(): array
260+
public static function mustNotInvalidateRequestResponseCombinations(): array
266261
{
267262
return [
268263
// https://github.com/FriendsOfSymfony/FOSHttpCacheBundle/issues/279

tests/Functional/EventListener/UserContextListenerTest.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,15 @@
1212
namespace FOS\HttpCacheBundle\Tests\Functional\EventListener;
1313

1414
use FOS\HttpCacheBundle\Tests\Functional\SessionHelperTrait;
15+
use PHPUnit\Framework\Attributes as PHPUnit;
1516
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1617
use Symfony\Component\HttpKernel\Event\RequestEvent;
1718

1819
class UserContextListenerTest extends WebTestCase
1920
{
2021
use SessionHelperTrait;
2122

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

51-
/**
52-
* @dataProvider userHashDataProvider
53-
*/
54-
public function testSessionCanBeCached(string $username, string $hash)
50+
#[PHPUnit\DataProvider('userHashDataProvider')]
51+
public function testSessionCanBeCached(string $username, string $hash): void
5552
{
5653
$client = static::createClient([], $username ? [
5754
'PHP_AUTH_USER' => $username,
@@ -66,7 +63,7 @@ public function testSessionCanBeCached(string $username, string $hash)
6663
$this->assertEquals('max-age=60, public', $response->headers->get('Cache-Control'));
6764
}
6865

69-
public function userHashDataProvider()
66+
public static function userHashDataProvider(): \Generator
7067
{
7168
yield 'anonymous' => ['', '5224d8f5b85429624e2160e538a3376a479ec87b89251b295c44ecbf7498ea3c'];
7269
yield 'user' => ['user', '14cea38921d7f2284a52ac67eafb9ed5d30bed84684711591747d9110cae8be9'];

tests/Unit/CacheManagerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function testInvalidateRoute()
3838
->shouldReceive('purge')->once()->with('/my/route', [])
3939
->shouldReceive('purge')->once()->with('/route/with/params/id/123', [])
4040
->shouldReceive('purge')->once()->with('/route/with/params/id/123', ['X-Foo' => 'bar'])
41-
->shouldReceive('flush')->once()
41+
->shouldReceive('flush')->once()->andReturn(2)
4242
->getMock();
4343

4444
$router = \Mockery::mock(UrlGeneratorInterface::class)

tests/Unit/Command/PathSanityCheckTest.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
use FOS\HttpCacheBundle\Command\PathSanityCheck;
1515
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
16+
use PHPUnit\Framework\Attributes as PHPUnit;
1617
use PHPUnit\Framework\TestCase;
1718

1819
class PathSanityCheckTest extends TestCase
1920
{
2021
use MockeryPHPUnitIntegration;
2122

22-
public function pathProvider()
23+
public static function pathProvider(): array
2324
{
2425
return [
2526
[false, '/foo'],
@@ -32,10 +33,8 @@ public function pathProvider()
3233
];
3334
}
3435

35-
/**
36-
* @dataProvider pathProvider
37-
*/
38-
public function testLooksLikeRegularExpression($expected, $path)
36+
#[PHPUnit\DataProvider('pathProvider')]
37+
public function testLooksLikeRegularExpression(bool $expected, string $path): void
3938
{
4039
$sanityChecking = new SanityChecking();
4140
$this->assertEquals($expected, $sanityChecking->looksLikeRegularExpression($path));
@@ -48,7 +47,7 @@ class SanityChecking
4847
looksLikeRegularExpression as traitFunction;
4948
}
5049

51-
public function looksLikeRegularExpression($path)
50+
public function looksLikeRegularExpression(string $path): bool
5251
{
5352
return $this->traitFunction($path);
5453
}

0 commit comments

Comments
 (0)