From 49720dc8727655533c4098ff7be17bc2d555cff7 Mon Sep 17 00:00:00 2001 From: catch <6915-catch@users.noreply.drupalcode.org> Date: Fri, 9 Aug 2024 17:08:50 +0900 Subject: [PATCH] Issue #3464213 by mondrake: Method getMockForAbstractClass() of class PHPUnit\Framework\TestCase is deprecated in PHPUnit 10 - replace in Plugin component tests --- core/.phpstan-baseline.php | 12 ------------ .../Tests/Component/Plugin/PluginBaseTest.php | 18 ++++++++---------- .../Component/Plugin/PluginManagerBaseTest.php | 8 ++------ .../Tests/Component/Plugin/StubPluginBase.php | 13 +++++++++++++ .../Component/Plugin/StubPluginManagerBase.php | 13 +++++++++++++ 5 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 core/tests/Drupal/Tests/Component/Plugin/StubPluginBase.php create mode 100644 core/tests/Drupal/Tests/Component/Plugin/StubPluginManagerBase.php diff --git a/core/.phpstan-baseline.php b/core/.phpstan-baseline.php index d9b3b03d9e1d..6b0f8073d600 100644 --- a/core/.phpstan-baseline.php +++ b/core/.phpstan-baseline.php @@ -2521,18 +2521,6 @@ 'count' => 1, 'path' => __DIR__ . '/tests/Drupal/Tests/Component/Plugin/Factory/ReflectionFactoryTest.php', ]; -$ignoreErrors[] = [ - // identifier: method.deprecated - 'message' => '#^Call to deprecated method getMockForAbstractClass\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\.$#', - 'count' => 4, - 'path' => __DIR__ . '/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php', -]; -$ignoreErrors[] = [ - // identifier: method.deprecated - 'message' => '#^Call to deprecated method getMockForAbstractClass\\(\\) of class PHPUnit\\\\Framework\\\\MockObject\\\\MockBuilder\\.$#', - 'count' => 2, - 'path' => __DIR__ . '/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php', -]; $ignoreErrors[] = [ 'message' => '#^Missing cache backend declaration for performance\\.$#', 'count' => 1, diff --git a/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php b/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php index 488361d6de9e..51ff3575f969 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/PluginBaseTest.php @@ -17,11 +17,11 @@ class PluginBaseTest extends TestCase { * @covers ::getPluginId */ public function testGetPluginId($plugin_id, $expected): void { - $plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', [ + $plugin_base = new StubPluginBase( [], $plugin_id, [], - ]); + ); $this->assertEquals($expected, $plugin_base->getPluginId()); } @@ -43,12 +43,11 @@ public static function providerTestGetPluginId() { * @coves ::getBaseId */ public function testGetBaseId($plugin_id, $expected): void { - /** @var \Drupal\Component\Plugin\PluginBase|\PHPUnit\Framework\MockObject\MockObject $plugin_base */ - $plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', [ + $plugin_base = new StubPluginBase( [], $plugin_id, [], - ]); + ); $this->assertEquals($expected, $plugin_base->getBaseId()); } @@ -70,12 +69,11 @@ public static function providerTestGetBaseId() { * @covers ::getDerivativeId */ public function testGetDerivativeId($plugin_id = NULL, $expected = NULL): void { - /** @var \Drupal\Component\Plugin\PluginBase|\PHPUnit\Framework\MockObject\MockObject $plugin_base */ - $plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', [ + $plugin_base = new StubPluginBase( [], $plugin_id, [], - ]); + ); $this->assertEquals($expected, $plugin_base->getDerivativeId()); } @@ -96,11 +94,11 @@ public static function providerTestGetDerivativeId() { * @covers ::getPluginDefinition */ public function testGetPluginDefinition(): void { - $plugin_base = $this->getMockForAbstractClass('Drupal\Component\Plugin\PluginBase', [ + $plugin_base = new StubPluginBase( [], 'plugin_id', ['value', ['key' => 'value']], - ]); + ); $this->assertEquals(['value', ['key' => 'value']], $plugin_base->getPluginDefinition()); } diff --git a/core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php b/core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php index 39fdeddc08dd..9842f252a839 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/PluginManagerBaseTest.php @@ -6,7 +6,6 @@ use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Component\Plugin\Mapper\MapperInterface; -use Drupal\Component\Plugin\PluginManagerBase; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; @@ -51,8 +50,7 @@ public function getMockFactoryInterface($expects_count) { * @covers ::createInstance */ public function testCreateInstance(): void { - $manager = $this->getMockBuilder('Drupal\Component\Plugin\PluginManagerBase') - ->getMockForAbstractClass(); + $manager = new StubPluginManagerBase(); // PluginManagerBase::createInstance() looks for a factory object and then // calls createInstance() on it. So we have to mock a factory object. $factory_ref = new \ReflectionProperty($manager, 'factory'); @@ -118,9 +116,7 @@ public function testGetInstanceWithoutMapperShouldThrowException(): void { 'foo' => 'F00', 'bar' => 'bAr', ]; - /** @var \Drupal\Component\Plugin\PluginManagerBase $manager */ - $manager = $this->getMockBuilder(PluginManagerBase::class) - ->getMockForAbstractClass(); + $manager = new StubPluginManagerBase(); // Set the expected exception thrown by ::getInstance. $this->expectException(\BadMethodCallException::class); $this->expectExceptionMessage(sprintf('%s does not support this method unless %s::$mapper is set.', get_class($manager), get_class($manager))); diff --git a/core/tests/Drupal/Tests/Component/Plugin/StubPluginBase.php b/core/tests/Drupal/Tests/Component/Plugin/StubPluginBase.php new file mode 100644 index 000000000000..1c9e87cea416 --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Plugin/StubPluginBase.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\Component\Plugin; + +use Drupal\Component\Plugin\PluginBase; + +/** + * A class extending PluginBase for testing purposes. + */ +class StubPluginBase extends PluginBase { +} diff --git a/core/tests/Drupal/Tests/Component/Plugin/StubPluginManagerBase.php b/core/tests/Drupal/Tests/Component/Plugin/StubPluginManagerBase.php new file mode 100644 index 000000000000..a4ae95b3e529 --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Plugin/StubPluginManagerBase.php @@ -0,0 +1,13 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\Component\Plugin; + +use Drupal\Component\Plugin\PluginManagerBase; + +/** + * A class extending PluginManagerBase for testing purposes. + */ +class StubPluginManagerBase extends PluginManagerBase { +} -- GitLab