From c4c8528f4fe71a86b5305aabfdc57cf5c49adbd2 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Sat, 13 Apr 2024 12:39:44 +0200 Subject: [PATCH] Add support for PHPUnit 11 --- composer.json | 10 ++--- phpstan.neon | 1 + src/DomainTest.php | 4 +- src/ResolvedDomainTest.php | 8 ++-- .../PublicSuffixListPsr16CacheTest.php | 35 ++++++++++++++++++ src/Storage/TimeToLiveTest.php | 4 +- .../TopLevelDomainListPsr16CacheTest.php | 37 +++++++++++++++++++ 7 files changed, 85 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index c2be172..65f30e6 100644 --- a/composer.json +++ b/composer.json @@ -46,16 +46,16 @@ "ext-json": "*" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.52.1", + "friendsofphp/php-cs-fixer": "^3.53.0", "guzzlehttp/guzzle": "^7.8.1", "guzzlehttp/psr7": "^1.6 || ^2.6.2", - "phpstan/phpstan": "^1.10.64", + "phpstan/phpstan": "^1.10.66", "phpstan/phpstan-phpunit": "^1.3.16", - "phpstan/phpstan-strict-rules": "^1.5.2", - "phpunit/phpunit": "^10.5.15", + "phpstan/phpstan-strict-rules": "^1.5.3", + "phpunit/phpunit": "^10.5.15 || ^11.1.1", "psr/http-factory": "^1.0.2", "psr/simple-cache": "^1.0.1", - "symfony/cache": "^v5.0.0 || ^6.4.4" + "symfony/cache": "^v5.0.0 || ^6.4.6" }, "suggest": { "psr/http-client-implementation": "To use the storage functionnality which depends on PSR-18", diff --git a/phpstan.neon b/phpstan.neon index caa678b..0332b88 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -12,4 +12,5 @@ parameters: path: src/Rules.php - message: '#Variable \$line on left side of \?\? always exists and is not nullable.#' path: src/Rules.php + - '#^Parameter \#1 \$callback of function set_error_handler expects#' reportUnmatchedIgnoredErrors: true diff --git a/src/DomainTest.php b/src/DomainTest.php index 3ea5315..e3ea7c0 100644 --- a/src/DomainTest.php +++ b/src/DomainTest.php @@ -168,14 +168,14 @@ public static function toUnicodeProvider(): iterable public function testToAscii( ?string $domain, ?string $expectedDomain, - ?string $expectedAsciiDomain + ?string $expectedIDNDomain ): void { $domain = Domain::fromIDNA2008($domain); self::assertSame($expectedDomain, $domain->value()); /** @var Domain $domainIDN */ $domainIDN = $domain->toAscii(); - self::assertSame($expectedAsciiDomain, $domainIDN->value()); + self::assertSame($expectedIDNDomain, $domainIDN->value()); } /** diff --git a/src/ResolvedDomainTest.php b/src/ResolvedDomainTest.php index e0b1466..12c777b 100644 --- a/src/ResolvedDomainTest.php +++ b/src/ResolvedDomainTest.php @@ -173,8 +173,8 @@ public function testItCanBeConvertedToAscii( ?string $publicSuffix, ?string $expectedDomain, ?string $expectedSuffix, - ?string $expectedAsciiDomain, - ?string $expectedAsciiSuffix + ?string $expectedIDNDomain, + ?string $expectedIDNSuffix ): void { $domain = ResolvedDomain::fromUnknown(Domain::fromIDNA2003($domain), count(Domain::fromIDNA2003($publicSuffix))); self::assertSame($expectedDomain, $domain->value()); @@ -182,8 +182,8 @@ public function testItCanBeConvertedToAscii( /** @var ResolvedDomain $domainIDN */ $domainIDN = $domain->toAscii(); - self::assertSame($expectedAsciiDomain, $domainIDN->value()); - self::assertSame($expectedAsciiSuffix, $domainIDN->suffix()->value()); + self::assertSame($expectedIDNDomain, $domainIDN->value()); + self::assertSame($expectedIDNSuffix, $domainIDN->suffix()->value()); } /** diff --git a/src/Storage/PublicSuffixListPsr16CacheTest.php b/src/Storage/PublicSuffixListPsr16CacheTest.php index 18b0cf3..f41cfb5 100644 --- a/src/Storage/PublicSuffixListPsr16CacheTest.php +++ b/src/Storage/PublicSuffixListPsr16CacheTest.php @@ -9,6 +9,7 @@ use InvalidArgumentException; use Pdp\Rules; use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\ErrorHandler; use Psr\SimpleCache\CacheException; use Psr\SimpleCache\CacheInterface; use RuntimeException; @@ -128,4 +129,38 @@ public function testItWillThrowIfTheTTLIsNotParsable(): void $cache = self::createStub(CacheInterface::class); new PublicSuffixListPsr16Cache($cache, 'pdp_', 'foobar'); } + + protected function restoreExceptionHandler(): void + { + while (true) { + $previousHandler = set_exception_handler(static fn () => null); + restore_exception_handler(); + if (null === $previousHandler) { + break; + } + + restore_exception_handler(); + } + } + + protected function restoreErrorHandler(): void + { + while (true) { + $previousHandler = set_error_handler(static fn (int $errno, string $errstr, ?string $errfile = null, ?int $errline = null) => null); + restore_error_handler(); + $isPhpUnitErrorHandler = ($previousHandler instanceof ErrorHandler); + if (null === $previousHandler || $isPhpUnitErrorHandler) { + break; + } + restore_error_handler(); + } + } + + protected function tearDown(): void + { + parent::tearDown(); + + $this->restoreErrorHandler(); + $this->restoreExceptionHandler(); + } } diff --git a/src/Storage/TimeToLiveTest.php b/src/Storage/TimeToLiveTest.php index 1604d3d..9ca00b7 100644 --- a/src/Storage/TimeToLiveTest.php +++ b/src/Storage/TimeToLiveTest.php @@ -22,9 +22,7 @@ public function testItDoesNotReturnTheAbsoluteInterval(): void self::assertSame(0, TimeToLive::until($tomorrow)->invert); } - /** - * @dataProvider validDurationString - */ + #[DataProvider('validDurationString')] public function testItCanBeInstantiatedFromDurationInput(string $input, DateInterval $expected): void { $now = new DateTimeImmutable(); diff --git a/src/Storage/TopLevelDomainListPsr16CacheTest.php b/src/Storage/TopLevelDomainListPsr16CacheTest.php index 9d4275b..2eb8cb4 100644 --- a/src/Storage/TopLevelDomainListPsr16CacheTest.php +++ b/src/Storage/TopLevelDomainListPsr16CacheTest.php @@ -9,6 +9,7 @@ use InvalidArgumentException; use Pdp\TopLevelDomains; use PHPUnit\Framework\TestCase; +use PHPUnit\Runner\ErrorHandler; use Psr\SimpleCache\CacheException; use Psr\SimpleCache\CacheInterface; use RuntimeException; @@ -125,4 +126,40 @@ public function testItWillThrowIfTheTTLIsNotParsable(): void $cache = self::createStub(CacheInterface::class); new TopLevelDomainListPsr16Cache($cache, 'pdp_', 'foobar'); } + + + + protected function restoreExceptionHandler(): void + { + while (true) { + $previousHandler = set_exception_handler(static fn () => null); + restore_exception_handler(); + if (null === $previousHandler) { + break; + } + + restore_exception_handler(); + } + } + + protected function restoreErrorHandler(): void + { + while (true) { + $previousHandler = set_error_handler(static fn (int $errno, string $errstr, ?string $errfile = null, ?int $errline = null) => null); + restore_error_handler(); + $isPhpUnitErrorHandler = ($previousHandler instanceof ErrorHandler); + if (null === $previousHandler || $isPhpUnitErrorHandler) { + break; + } + restore_error_handler(); + } + } + + protected function tearDown(): void + { + parent::tearDown(); + + $this->restoreErrorHandler(); + $this->restoreExceptionHandler(); + } }