services: Do not use deprecated ReflectionType::__toString() in tests
authorMáté Szabó <mszabo@wikia-inc.com>
Mon, 16 Sep 2019 19:14:07 +0000 (21:14 +0200)
committerMáté Szabó <mszabo@wikia-inc.com>
Mon, 16 Sep 2019 19:14:07 +0000 (21:14 +0200)
The \MediaWikiServicesTest::provideGetService() PHPUnit data provider
method uses the ReflectionType::__toString() method to obtain the
return type of service instantiator callables as a string.

In PHP 7.4, calling this method generates an E_DEPRECATED notice.
The solution is to use ReflectionType::getName() instead, available
since PHP 7.1.[1]

This patch updates the method to use ReflectionType::getName()
if it is available, and fall back to ReflectionType::__toString()
otherwise.

---
[1] https://github.com/php/php-src/blob/php-7.4.0RC1/UPGRADING#L395

Bug: T233012
Change-Id: I69b97f75795e6c4e005bf1a23fb798f7e52da2fd

tests/phpunit/includes/MediaWikiServicesTest.php

index 8fa0cd6..d34ba0a 100644 (file)
@@ -308,7 +308,16 @@ class MediaWikiServicesTest extends MediaWikiTestCase {
                                throw new MWException( 'All service callbacks must have a return type defined, ' .
                                        "none found for $name" );
                        }
-                       $ret[$name] = [ $name, $fun->getReturnType()->__toString() ];
+
+                       $returnType = $fun->getReturnType();
+
+                       // ReflectionType::__toString() generates deprecation notices in PHP 7.4 and above
+                       // TODO: T228342 - remove this check after MediaWiki only supports PHP 7.1+
+                       if ( is_callable( [ $returnType, 'getName' ] ) ) {
+                               $ret[$name] = [ $name, $returnType->getName() ];
+                       } else {
+                               $ret[$name] = [ $name, $fun->getReturnType()->__toString() ];
+                       }
                }
                return $ret;
        }