Skip to content

Commit 7a52e6e

Browse files
committed
Build/Test Tools: Parse expectation annotations without PHPUnit.
Read deprecation and incorrect-usage annotations through reflection instead of PHPUnit's internal parser, which is unavailable in PHPUnit 11. Preserve existing expectation methods, annotation names, and callers. Developed in: #13848 Props sukhendu2002, hellofromtonya, jrf, sergeybiryukov, dmsnell. See #54029. git-svn-id: https://develop.svn.wordpress.org/trunk@65465 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 496e882 commit 7a52e6e

1 file changed

Lines changed: 28 additions & 9 deletions

File tree

‎tests/phpunit/includes/abstract-testcase.php‎

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -838,15 +838,34 @@ public function wp_die_handler( $message, $title, $args ) {
838838
* @since 3.7.0
839839
*/
840840
public function expectDeprecated() {
841-
if ( method_exists( $this, 'getAnnotations' ) ) {
842-
// PHPUnit < 9.5.0.
843-
$annotations = $this->getAnnotations();
844-
} else {
845-
// PHPUnit >= 9.5.0.
846-
$annotations = \PHPUnit\Util\Test::parseTestMethodAnnotations(
847-
static::class,
848-
$this->getName( false )
849-
);
841+
$method_name = method_exists( $this, 'name' ) ? $this->name() : $this->getName( false );
842+
$class = new ReflectionClass( $this );
843+
$reflectors = array(
844+
'class' => array_merge( array_values( $class->getTraits() ), array( $class ) ),
845+
'method' => array( $class->getMethod( $method_name ) ),
846+
);
847+
$annotations = array(
848+
'class' => array(),
849+
'method' => array(),
850+
);
851+
852+
foreach ( $reflectors as $depth => $depth_reflectors ) {
853+
foreach ( $depth_reflectors as $reflector ) {
854+
$docblock = substr( (string) $reflector->getDocComment(), 3, -2 );
855+
$doc_annotations = array();
856+
857+
// Parse all tags before filtering so annotation values remain intact.
858+
if ( preg_match_all( '/@([A-Za-z_-]+)(?:[ \t]+(.*?))?[ \t]*\r?$/m', $docblock, $matches, PREG_SET_ORDER ) ) {
859+
foreach ( $matches as $match ) {
860+
if ( in_array( $match[1], array( 'expectedDeprecated', 'expectedIncorrectUsage' ), true ) ) {
861+
$doc_annotations[ $match[1] ][] = $match[2] ?? '';
862+
}
863+
}
864+
}
865+
866+
// Later traits and the class replace earlier values for the same annotation.
867+
$annotations[ $depth ] = array_merge( $annotations[ $depth ], $doc_annotations );
868+
}
850869
}
851870

852871
foreach ( array( 'class', 'method' ) as $depth ) {

0 commit comments

Comments
 (0)