diff --git a/core/tests/Drupal/Tests/Phpunit5FCTrait.php b/core/tests/Drupal/Tests/Phpunit5FCTrait.php index ad69d3fac9..0c3b0ad238 100644 --- a/core/tests/Drupal/Tests/Phpunit5FCTrait.php +++ b/core/tests/Drupal/Tests/Phpunit5FCTrait.php @@ -42,11 +42,12 @@ * @return \PHPUnit_Framework_MockObject_MockObject * * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. - * Use \Drupal\Tests\Phpunit5FCTrait::createMock. + * Use \Drupal\Tests\Phpunit5FCTrait::createMock() instead. * * @see https://www.drupal.org/node/2907725 */ public function getMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = TRUE, $callOriginalClone = TRUE, $callAutoload = TRUE, $cloneArguments = FALSE, $callOriginalMethods = FALSE, $proxyTarget = NULL) { + @trigger_error('\Drupal\Tests\Phpunit5FCTrait::getMock() in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Tests\Phpunit5FCTrait::createMock() instead. See https://www.drupal.org/node/2907725.', E_USER_DEPRECATED); return $this->createMock($originalClassName, $methods, $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods, $proxyTarget); } @@ -98,16 +99,12 @@ public function createMock($originalClassName, $methods = array(), array $argume private function isPhpunit5() { // Get the parent class of the currently running test class. $parent = get_parent_class($this); - // Test classes can have an arbitrary amount of subclasses. We can not - // simply test for the existence of the PHPUnit 5 method because this trait - // is most likely used among the ancestor classes. So we get the parent of - // the parent until this method is not defined any more and we know that - // the method_exists check is performed on \PHPUnit\Framework\TestCase. - while (method_exists($parent, 'isPhpunit5')) { + // Ensure that we do the method exists check of the first parent of $this + // that does not use this trait. Most often the check will be made on + // \PHPUnit\Framework\TestCase.; + while (in_array(__TRAIT__, class_uses($parent), TRUE)) { $parent = get_parent_class($parent); } - // Finally we are not checking the method defined in this trait and we can - // determine if the running test class is extending from PHPUnit 5 or not. return method_exists($parent, 'createMock'); } diff --git a/core/tests/Drupal/Tests/Phpunit5FCTraitTest.php b/core/tests/Drupal/Tests/Phpunit5FCTraitTest.php index 7ae5e7e3db..cf02b4e0bb 100644 --- a/core/tests/Drupal/Tests/Phpunit5FCTraitTest.php +++ b/core/tests/Drupal/Tests/Phpunit5FCTraitTest.php @@ -16,6 +16,8 @@ class Phpunit5FCTraitTest extends UnitTestCase { * * @covers ::getMock * @dataProvider providerMockVersions + * @group legacy + * @expectedDeprecation \Drupal\Tests\Phpunit5FCTrait::getMock() in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Tests\Phpunit5FCTrait::createMock() instead. See https://www.drupal.org/node/2907725. */ public function testGetMock($className, $expected) { $class = new $className(); @@ -41,7 +43,9 @@ public function testCreateMock($className, $expected) { public function providerMockVersions() { return [ [UnitTestCasePhpunit4TestClass::class, 'PHPUnit 4'], + [UnitTestCasePhpunit4TestClassExtends::class, 'PHPUnit 4'], [UnitTestCasePhpunit5TestClass::class, 'PHPUnit 5'], + [UnitTestCasePhpunit5TestClassExtends::class, 'PHPUnit 5'], ]; } @@ -54,6 +58,7 @@ class Phpunit4TestClass { public function getMock($originalClassName) { return 'PHPUnit 4'; } + } /** @@ -63,6 +68,7 @@ class Phpunit5TestClass { public function createMock($originalClassName) { return 'PHPUnit 5'; } + } /** @@ -70,6 +76,13 @@ public function createMock($originalClassName) { */ class UnitTestCasePhpunit4TestClass extends Phpunit4TestClass { use Phpunit5FCTrait; + +} + +/** + * Test class for \Drupal\Tests\UnitTestCase with PHPUnit 4. + */ +class UnitTestCasePhpunit4TestClassExtends extends UnitTestCasePhpunit4TestClass { } /** @@ -77,4 +90,11 @@ class UnitTestCasePhpunit4TestClass extends Phpunit4TestClass { */ class UnitTestCasePhpunit5TestClass extends Phpunit5TestClass { use Phpunit5FCTrait; + +} + +/** + * Test class for \Drupal\Tests\UnitTestCase with PHPUnit 5. + */ +class UnitTestCasePhpunit5TestClassExtends extends UnitTestCasePhpunit5TestClass { }