Skip to content

Reimplement assertions removed in PHPUnit 10/12 #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions src/Codeception/Util/Shared/InheritedAsserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -1072,15 +1084,37 @@ 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);
}
}

/**
* Asserts that a string does not match a given format 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);
}
}

/**
Expand Down Expand Up @@ -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;
}
}