From 754423e5e6f9329b9b5c621fbc5909b6caa6f473 Mon Sep 17 00:00:00 2001 From: W0rma Date: Thu, 20 Feb 2025 08:58:02 +0100 Subject: [PATCH 1/2] Reimplement static property assertions which were removed in PHPUnit 10 --- .../Util/Shared/InheritedAsserts.php | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Codeception/Util/Shared/InheritedAsserts.php b/src/Codeception/Util/Shared/InheritedAsserts.php index 1b412c8..5d9f804 100644 --- a/src/Codeception/Util/Shared/InheritedAsserts.php +++ b/src/Codeception/Util/Shared/InheritedAsserts.php @@ -51,7 +51,13 @@ protected function assertClassHasAttribute(string $attributeName, string $classN */ protected function assertClassHasStaticAttribute(string $attributeName, string $className, string $message = '') { - Assert::assertClassHasStaticAttribute($attributeName, $className, $message); + trigger_error(__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 10', E_USER_DEPRECATED); + + if (method_exists(Assert::class, 'assertClassHasStaticAttribute')) { + Assert::assertClassHasStaticAttribute($attributeName, $className, $message); + } else { + Assert::assertTrue(self::hasStaticAttribute($attributeName, $className), $message); + } } /** @@ -75,7 +81,11 @@ protected function assertClassNotHasStaticAttribute(string $attributeName, strin { trigger_error(__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 10', E_USER_DEPRECATED); - Assert::assertClassNotHasStaticAttribute($attributeName, $className, $message); + if (method_exists(Assert::class, 'assertClassNotHasStaticAttribute')) { + Assert::assertClassNotHasStaticAttribute($attributeName, $className, $message); + } else { + Assert::assertFalse(self::hasStaticAttribute($attributeName, $className), $message); + } } /** @@ -1200,4 +1210,21 @@ protected function markTestSkipped(string $message = '') { Assert::markTestSkipped($message); } + + /** + * @see https://github.com/sebastianbergmann/phpunit/blob/9.6/src/Framework/Constraint/Object/ClassHasStaticAttribute.php + */ + private static function hasStaticAttribute(string $attributeName, string $className) + { + try { + $class = new \ReflectionClass($className); + + if ($class->hasProperty($attributeName)) { + return $class->getProperty($attributeName)->isStatic(); + } + } catch (ReflectionException $e) { + } + + return false; + } } From a0dfe3b956d90ddedd43346a01196d2f5c932dcd Mon Sep 17 00:00:00 2001 From: W0rma Date: Thu, 20 Feb 2025 11:52:41 +0100 Subject: [PATCH 2/2] Reimplement negated string matches format assertions which were removed in PHPUnit 12 --- .../Util/Shared/InheritedAsserts.php | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Codeception/Util/Shared/InheritedAsserts.php b/src/Codeception/Util/Shared/InheritedAsserts.php index 5d9f804..993b127 100644 --- a/src/Codeception/Util/Shared/InheritedAsserts.php +++ b/src/Codeception/Util/Shared/InheritedAsserts.php @@ -7,6 +7,8 @@ use Codeception\PHPUnit\TestCase; use PHPUnit\Framework\Assert; use PHPUnit\Framework\Constraint\Constraint as PHPUnitConstraint; +use PHPUnit\Framework\Constraint\LogicalNot; +use PHPUnit\Framework\Constraint\StringMatchesFormatDescription; trait InheritedAsserts { @@ -1082,7 +1084,15 @@ protected function assertStringNotEqualsFileIgnoringCase(string $expectedFile, s */ protected function assertStringNotMatchesFormat(string $format, string $string, string $message = '') { - Assert::assertStringNotMatchesFormat($format, $string, $message); + trigger_error(__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 12', E_USER_DEPRECATED); + + if (method_exists(Assert::class, 'assertStringNotMatchesFormat')) { + Assert::assertStringNotMatchesFormat($format, $string, $message); + } else { + $constraint = new LogicalNot(new StringMatchesFormatDescription($format)); + + Assert::assertThat($string, $constraint, $message); + } } /** @@ -1090,7 +1100,21 @@ protected function assertStringNotMatchesFormat(string $format, string $string, */ protected function assertStringNotMatchesFormatFile(string $formatFile, string $string, string $message = '') { - Assert::assertStringNotMatchesFormatFile($formatFile, $string, $message); + trigger_error(__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 12', E_USER_DEPRECATED); + + if (method_exists(Assert::class, 'assertStringNotMatchesFormatFile')) { + Assert::assertStringNotMatchesFormatFile($formatFile, $string, $message); + } else { + Assert::assertFileExists($formatFile); + + $constraint = new LogicalNot( + new StringMatchesFormatDescription( + file_get_contents($formatFile) + ) + ); + + Assert::assertThat($string, $constraint, $message); + } } /**