Skip to content

Commit 06750a6

Browse files
authored
Merge pull request #14 from W0rma/fix-removed-assertions
Reimplement assertions removed in PHPUnit 10/12
2 parents 85e7600 + a0dfe3b commit 06750a6

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

src/Codeception/Util/Shared/InheritedAsserts.php

+55-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Codeception\PHPUnit\TestCase;
88
use PHPUnit\Framework\Assert;
99
use PHPUnit\Framework\Constraint\Constraint as PHPUnitConstraint;
10+
use PHPUnit\Framework\Constraint\LogicalNot;
11+
use PHPUnit\Framework\Constraint\StringMatchesFormatDescription;
1012

1113
trait InheritedAsserts
1214
{
@@ -51,7 +53,13 @@ protected function assertClassHasAttribute(string $attributeName, string $classN
5153
*/
5254
protected function assertClassHasStaticAttribute(string $attributeName, string $className, string $message = '')
5355
{
54-
Assert::assertClassHasStaticAttribute($attributeName, $className, $message);
56+
trigger_error(__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 10', E_USER_DEPRECATED);
57+
58+
if (method_exists(Assert::class, 'assertClassHasStaticAttribute')) {
59+
Assert::assertClassHasStaticAttribute($attributeName, $className, $message);
60+
} else {
61+
Assert::assertTrue(self::hasStaticAttribute($attributeName, $className), $message);
62+
}
5563
}
5664

5765
/**
@@ -75,7 +83,11 @@ protected function assertClassNotHasStaticAttribute(string $attributeName, strin
7583
{
7684
trigger_error(__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 10', E_USER_DEPRECATED);
7785

78-
Assert::assertClassNotHasStaticAttribute($attributeName, $className, $message);
86+
if (method_exists(Assert::class, 'assertClassNotHasStaticAttribute')) {
87+
Assert::assertClassNotHasStaticAttribute($attributeName, $className, $message);
88+
} else {
89+
Assert::assertFalse(self::hasStaticAttribute($attributeName, $className), $message);
90+
}
7991
}
8092

8193
/**
@@ -1150,15 +1162,37 @@ protected function assertStringNotEqualsFileIgnoringCase(string $expectedFile, s
11501162
*/
11511163
protected function assertStringNotMatchesFormat(string $format, string $string, string $message = '')
11521164
{
1153-
Assert::assertStringNotMatchesFormat($format, $string, $message);
1165+
trigger_error(__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 12', E_USER_DEPRECATED);
1166+
1167+
if (method_exists(Assert::class, 'assertStringNotMatchesFormat')) {
1168+
Assert::assertStringNotMatchesFormat($format, $string, $message);
1169+
} else {
1170+
$constraint = new LogicalNot(new StringMatchesFormatDescription($format));
1171+
1172+
Assert::assertThat($string, $constraint, $message);
1173+
}
11541174
}
11551175

11561176
/**
11571177
* Asserts that a string does not match a given format string.
11581178
*/
11591179
protected function assertStringNotMatchesFormatFile(string $formatFile, string $string, string $message = '')
11601180
{
1161-
Assert::assertStringNotMatchesFormatFile($formatFile, $string, $message);
1181+
trigger_error(__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 12', E_USER_DEPRECATED);
1182+
1183+
if (method_exists(Assert::class, 'assertStringNotMatchesFormatFile')) {
1184+
Assert::assertStringNotMatchesFormatFile($formatFile, $string, $message);
1185+
} else {
1186+
Assert::assertFileExists($formatFile);
1187+
1188+
$constraint = new LogicalNot(
1189+
new StringMatchesFormatDescription(
1190+
file_get_contents($formatFile)
1191+
)
1192+
);
1193+
1194+
Assert::assertThat($string, $constraint, $message);
1195+
}
11621196
}
11631197

11641198
/**
@@ -1280,4 +1314,21 @@ protected function markTestSkipped(string $message = '')
12801314
{
12811315
Assert::markTestSkipped($message);
12821316
}
1317+
1318+
/**
1319+
* @see https://github.com/sebastianbergmann/phpunit/blob/9.6/src/Framework/Constraint/Object/ClassHasStaticAttribute.php
1320+
*/
1321+
private static function hasStaticAttribute(string $attributeName, string $className)
1322+
{
1323+
try {
1324+
$class = new \ReflectionClass($className);
1325+
1326+
if ($class->hasProperty($attributeName)) {
1327+
return $class->getProperty($attributeName)->isStatic();
1328+
}
1329+
} catch (ReflectionException $e) {
1330+
}
1331+
1332+
return false;
1333+
}
12831334
}

0 commit comments

Comments
 (0)