diff --git a/src/Codeception/Util/Shared/InheritedAsserts.php b/src/Codeception/Util/Shared/InheritedAsserts.php index 1b412c8..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 { @@ -51,7 +53,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 +83,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); + } } /** @@ -1072,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); + } } /** @@ -1080,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); + } } /** @@ -1200,4 +1234,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; + } }