7
7
use Codeception \PHPUnit \TestCase ;
8
8
use PHPUnit \Framework \Assert ;
9
9
use PHPUnit \Framework \Constraint \Constraint as PHPUnitConstraint ;
10
+ use PHPUnit \Framework \Constraint \LogicalNot ;
11
+ use PHPUnit \Framework \Constraint \StringMatchesFormatDescription ;
10
12
11
13
trait InheritedAsserts
12
14
{
@@ -51,7 +53,13 @@ protected function assertClassHasAttribute(string $attributeName, string $classN
51
53
*/
52
54
protected function assertClassHasStaticAttribute (string $ attributeName , string $ className , string $ message = '' )
53
55
{
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
+ }
55
63
}
56
64
57
65
/**
@@ -75,7 +83,11 @@ protected function assertClassNotHasStaticAttribute(string $attributeName, strin
75
83
{
76
84
trigger_error (__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 10 ' , E_USER_DEPRECATED );
77
85
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
+ }
79
91
}
80
92
81
93
/**
@@ -1150,15 +1162,37 @@ protected function assertStringNotEqualsFileIgnoringCase(string $expectedFile, s
1150
1162
*/
1151
1163
protected function assertStringNotMatchesFormat (string $ format , string $ string , string $ message = '' )
1152
1164
{
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
+ }
1154
1174
}
1155
1175
1156
1176
/**
1157
1177
* Asserts that a string does not match a given format string.
1158
1178
*/
1159
1179
protected function assertStringNotMatchesFormatFile (string $ formatFile , string $ string , string $ message = '' )
1160
1180
{
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
+ }
1162
1196
}
1163
1197
1164
1198
/**
@@ -1280,4 +1314,21 @@ protected function markTestSkipped(string $message = '')
1280
1314
{
1281
1315
Assert::markTestSkipped ($ message );
1282
1316
}
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
+ }
1283
1334
}
0 commit comments