77use ArrayAccess ;
88use PHPUnit \Framework \Assert ;
99use PHPUnit \Framework \Constraint \Constraint as PHPUnitConstraint ;
10+ use PHPUnit \Framework \Constraint \LogicalNot ;
11+ use PHPUnit \Framework \Constraint \StringMatchesFormatDescription ;
1012use ReflectionClass ;
1113
1214trait InheritedAsserts
@@ -45,8 +47,9 @@ protected function assertClassHasAttribute(string $attributeName, string $classN
4547 */
4648 protected function assertClassHasStaticAttribute (string $ attributeName , string $ className , string $ message = '' ): void
4749 {
48- $ rc = new ReflectionClass ($ className );
49- Assert::assertTrue ($ rc ->hasProperty ($ attributeName ) && $ rc ->getProperty ($ attributeName )->isStatic ());
50+ trigger_error (__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 10 ' , E_USER_DEPRECATED );
51+
52+ Assert::assertTrue ($ this ->hasStaticAttribute ($ attributeName , $ className ), $ message );
5053 }
5154
5255 /**
@@ -66,8 +69,7 @@ protected function assertClassNotHasAttribute(string $attributeName, string $cla
6669 protected function assertClassNotHasStaticAttribute (string $ attributeName , string $ className , string $ message = '' ): void
6770 {
6871 trigger_error (__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 10 ' , E_USER_DEPRECATED );
69- $ rc = new ReflectionClass ($ className );
70- Assert::assertFalse ($ rc ->hasProperty ($ attributeName ) && $ rc ->getProperty ($ attributeName )->isStatic ());
72+ Assert::assertFalse ($ this ->hasStaticAttribute ($ attributeName , $ className ), $ message );
7173 }
7274
7375 /**
@@ -1138,6 +1140,31 @@ protected function assertStringNotEqualsFileIgnoringCase(string $expectedFile, s
11381140 Assert::assertStringNotEqualsFileIgnoringCase ($ expectedFile , $ actualString , $ message );
11391141 }
11401142
1143+ /**
1144+ * Asserts that a string does not match a given format string.
1145+ */
1146+ protected function assertStringNotMatchesFormat (string $ format , string $ string , string $ message = '' )
1147+ {
1148+ trigger_error (__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 12 ' , E_USER_DEPRECATED );
1149+ $ constraint = new LogicalNot (new StringMatchesFormatDescription ($ format ));
1150+ Assert::assertThat ($ string , $ constraint , $ message );
1151+ }
1152+
1153+ /**
1154+ * Asserts that a string does not match a given format string.
1155+ */
1156+ protected function assertStringNotMatchesFormatFile (string $ formatFile , string $ string , string $ message = '' )
1157+ {
1158+ trigger_error (__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 12 ' , E_USER_DEPRECATED );
1159+ Assert::assertFileExists ($ formatFile );
1160+ $ constraint = new LogicalNot (
1161+ new StringMatchesFormatDescription (
1162+ file_get_contents ($ formatFile )
1163+ )
1164+ );
1165+ Assert::assertThat ($ string , $ constraint , $ message );
1166+ }
1167+
11411168 /**
11421169 * Asserts that a string starts not with a given prefix.
11431170 * @param non-empty-string $prefix
@@ -1290,4 +1317,21 @@ protected function markTestSkipped(string $message = ''): never
12901317 {
12911318 Assert::markTestSkipped ($ message );
12921319 }
1320+
1321+ /**
1322+ * @see https://github.com/sebastianbergmann/phpunit/blob/9.6/src/Framework/Constraint/Object/ClassHasStaticAttribute.php
1323+ */
1324+ private static function hasStaticAttribute (string $ attributeName , string $ className )
1325+ {
1326+ try {
1327+ $ class = new \ReflectionClass ($ className );
1328+
1329+ if ($ class ->hasProperty ($ attributeName )) {
1330+ return $ class ->getProperty ($ attributeName )->isStatic ();
1331+ }
1332+ } catch (ReflectionException $ e ) {
1333+ }
1334+
1335+ return false ;
1336+ }
12931337}
0 commit comments