From bdcf4705fa688c45c59de29c8c6655db42f0e588 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 26 Mar 2024 17:04:37 +0000 Subject: [PATCH] Issue #3433086 by mondrake: Method addMethods() of class PHPUnit\Framework\MockObject\MockBuilder is deprecated in PHPUnit 10 (cherry picked from commit b73c68de45491739c0752d859277c42835ccc54a) --- .../DisplayVariant/BlockPageVariantTest.php | 10 ++-- .../rest/tests/src/Unit/CollectRoutesTest.php | 6 +-- .../Unit/Plugin/field/FieldPluginBaseTest.php | 13 ++--- .../StaticDiscoveryDecoratorTest.php | 48 ++++++++++++------- .../Entity/EntityDisplayModeBaseUnitTest.php | 13 +++-- .../Tests/Core/Form/FormValidatorTest.php | 5 +- 6 files changed, 51 insertions(+), 44 deletions(-) diff --git a/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php b/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php index 5842e957d6d2..2d56c3d24b13 100644 --- a/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php +++ b/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Drupal\Tests\block\Unit\Plugin\DisplayVariant; +use Drupal\block\Plugin\DisplayVariant\BlockPageVariant; use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\DependencyInjection\Container; use Drupal\Tests\UnitTestCase; @@ -43,8 +44,8 @@ class BlockPageVariantTest extends UnitTestCase { * @param array $definition * The plugin definition array. * - * @return \Drupal\block\Plugin\DisplayVariant\BlockPageVariant|\PHPUnit\Framework\MockObject\MockObject - * A mocked display variant plugin. + * @return \Drupal\block\Plugin\DisplayVariant\BlockPageVariant + * A test display variant plugin. */ public function setUpDisplayVariant($configuration = [], $definition = []) { @@ -62,10 +63,7 @@ class BlockPageVariantTest extends UnitTestCase { $this->blockRepository = $this->createMock('Drupal\block\BlockRepositoryInterface'); $this->blockViewBuilder = $this->createMock('Drupal\Core\Entity\EntityViewBuilderInterface'); - return $this->getMockBuilder('Drupal\block\Plugin\DisplayVariant\BlockPageVariant') - ->setConstructorArgs([$configuration, 'test', $definition, $this->blockRepository, $this->blockViewBuilder, ['config:block_list']]) - ->addMethods(['getRegionNames']) - ->getMock(); + return new BlockPageVariant($configuration, 'test', $definition, $this->blockRepository, $this->blockViewBuilder, ['config:block_list']); } public function providerBuild() { diff --git a/core/modules/rest/tests/src/Unit/CollectRoutesTest.php b/core/modules/rest/tests/src/Unit/CollectRoutesTest.php index b70073e23815..7ef03be11164 100644 --- a/core/modules/rest/tests/src/Unit/CollectRoutesTest.php +++ b/core/modules/rest/tests/src/Unit/CollectRoutesTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\rest\Unit; use Drupal\Tests\UnitTestCase; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\rest\Plugin\views\display\RestExport; +use Drupal\views\Entity\View; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; @@ -41,10 +42,7 @@ class CollectRoutesTest extends UnitTestCase { ->disableOriginalConstructor() ->getMock(); - $view = $this->getMockBuilder('\Drupal\views\Entity\View') - ->addMethods(['initHandlers']) - ->setConstructorArgs([['id' => 'test_view'], 'view']) - ->getMock(); + $view = new View(['id' => 'test_view'], 'view'); $view_executable = $this->getMockBuilder('\Drupal\views\ViewExecutable') ->onlyMethods(['initHandlers', 'getTitle']) diff --git a/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php index a7899a91431e..df4c653103aa 100644 --- a/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace Drupal\Tests\views\Unit\Plugin\field; -use Drupal\Core\GeneratedUrl; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\GeneratedUrl; use Drupal\Core\Language\Language; use Drupal\Core\Render\Markup; use Drupal\Core\Url; @@ -15,11 +15,11 @@ use Drupal\Core\Utility\UnroutedUrlAssembler; use Drupal\Tests\UnitTestCase; use Drupal\views\Plugin\views\field\FieldPluginBase; use Drupal\views\ResultRow; +use Prophecy\Prophet; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Routing\Route; -use Prophecy\Prophet; /** * @coversDefaultClass \Drupal\views\Plugin\views\field\FieldPluginBase @@ -640,18 +640,13 @@ class FieldPluginBaseTest extends UnitTestCase { /** * Sets up a test field. * - * @return \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTestField|\PHPUnit\Framework\MockObject\MockObject + * @return \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTestField * The test field. */ protected function setupTestField(array $options = []) { - /** @var \Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTestField $field */ - $field = $this->getMockBuilder('Drupal\Tests\views\Unit\Plugin\field\FieldPluginBaseTestField') - ->addMethods(['l']) - ->setConstructorArgs([$this->configuration, $this->pluginId, $this->pluginDefinition]) - ->getMock(); + $field = new FieldPluginBaseTestField($this->configuration, $this->pluginId, $this->pluginDefinition); $field->init($this->executable, $this->display, $options); $field->setLinkGenerator($this->linkGenerator); - return $field; } diff --git a/core/tests/Drupal/Tests/Component/Plugin/Discovery/StaticDiscoveryDecoratorTest.php b/core/tests/Drupal/Tests/Component/Plugin/Discovery/StaticDiscoveryDecoratorTest.php index b05a7be50281..51e5b83202b2 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/Discovery/StaticDiscoveryDecoratorTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/Discovery/StaticDiscoveryDecoratorTest.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Drupal\Tests\Component\Plugin\Discovery; +use Drupal\Component\Plugin\Discovery\StaticDiscovery; +use Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator; use PHPUnit\Framework\TestCase; /** @@ -62,9 +64,9 @@ class StaticDiscoveryDecoratorTest extends TestCase { */ public function testGetDefinition($expected, $has_register_definitions, $exception_on_invalid, $definitions, $base_plugin_id) { // Mock our StaticDiscoveryDecorator. - $mock_decorator = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator') + $mock_decorator = $this->getMockBuilder(StaticDiscoveryDecorator::class) ->disableOriginalConstructor() - ->addMethods(['registeredDefinitionCallback']) + ->onlyMethods([]) ->getMock(); // Set up the ::$registerDefinitions property. @@ -129,9 +131,9 @@ class StaticDiscoveryDecoratorTest extends TestCase { */ public function testGetDefinitions($has_register_definitions, $definitions) { // Mock our StaticDiscoveryDecorator. - $mock_decorator = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator') + $mock_decorator = $this->getMockBuilder(StaticDiscoveryDecorator::class) ->disableOriginalConstructor() - ->addMethods(['registeredDefinitionCallback']) + ->onlyMethods([]) ->getMock(); // Set up the ::$registerDefinitions property. @@ -194,9 +196,9 @@ class StaticDiscoveryDecoratorTest extends TestCase { */ public function testCall($method, $args) { // Mock a decorated object. - $mock_decorated = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\DiscoveryInterface') - ->addMethods([$method]) - ->getMockForAbstractClass(); + $mock_decorated = $this->getMockBuilder(StaticDiscoveryTestDecoratedClass::class) + ->onlyMethods([$method]) + ->getMock(); // Our mocked method will return any arguments sent to it. $mock_decorated->expects($this->once()) ->method($method) @@ -206,18 +208,13 @@ class StaticDiscoveryDecoratorTest extends TestCase { } ); - // Create a mock decorator. - $mock_decorator = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\StaticDiscoveryDecorator') - ->disableOriginalConstructor() - ->getMock(); - // Poke the decorated object into our decorator. - $ref_decorated = new \ReflectionProperty($mock_decorator, 'decorated'); - $ref_decorated->setValue($mock_decorator, $mock_decorated); + // Create the decorator. + $decorator = new StaticDiscoveryDecorator($mock_decorated); - // Exercise __call. + // Exercise __call on the decorator. $this->assertEquals( $args, - \call_user_func_array([$mock_decorated, $method], $args) + \call_user_func_array([$decorator, $method], $args) ); } @@ -234,3 +231,22 @@ interface StaticDiscoveryDecoratorTestMockInterface { public function registerDefinitionsCallback(); } + +/** + * A class extending StaticDiscovery for testing purposes. + */ +class StaticDiscoveryTestDecoratedClass extends StaticDiscovery { + + public function getDefinitions(): array { + return []; + } + + public function complexArguments(mixed ...$args): array { + return $args; + } + + public function noArguments(): array { + return []; + } + +} diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php index bc44483ed202..c71a84836c3b 100644 --- a/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Drupal\Tests\Core\Config\Entity; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Entity\EntityDisplayModeBase; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Tests\UnitTestCase; @@ -92,11 +93,7 @@ class EntityDisplayModeBaseUnitTest extends UnitTestCase { [$this->entityType, TRUE, $this->entityInfo], ]); - $this->entity = $this->getMockBuilder('\Drupal\Core\Entity\EntityDisplayModeBase') - ->setConstructorArgs([$values, $this->entityType]) - ->addMethods(['getFilterFormat']) - ->getMock(); - + $this->entity = new EntityDisplayModeBaseTestableClass($values, $this->entityType); $dependencies = $this->entity->calculateDependencies()->getDependencies(); $this->assertContains('test_module', $dependencies['module']); } @@ -154,3 +151,9 @@ class EntityDisplayModeBaseUnitTest extends UnitTestCase { } } + +/** + * A class extending EntityDisplayModeBase for testing purposes. + */ +class EntityDisplayModeBaseTestableClass extends EntityDisplayModeBase { +} diff --git a/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php b/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php index db334a0feef6..4be62209529f 100644 --- a/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php +++ b/core/tests/Drupal/Tests/Core/Form/FormValidatorTest.php @@ -373,10 +373,7 @@ class FormValidatorTest extends UnitTestCase { * @dataProvider providerTestPerformRequiredValidation */ public function testPerformRequiredValidation($element, $expected_message, $call_watchdog) { - $form_validator = $this->getMockBuilder('Drupal\Core\Form\FormValidator') - ->setConstructorArgs([new RequestStack(), $this->getStringTranslationStub(), $this->csrfToken, $this->logger, $this->formErrorHandler]) - ->addMethods(['setError']) - ->getMock(); + $form_validator = new FormValidator(new RequestStack(), $this->getStringTranslationStub(), $this->csrfToken, $this->logger, $this->formErrorHandler); if ($call_watchdog) { $this->logger->expects($this->once())