Issue #3433086 by mondrake: Method addMethods() of class PHPUnit\Framework\MockObject\MockBuilder is deprecated in PHPUnit 10

merge-requests/7196/head
Alex Pott 2024-03-26 17:04:37 +00:00
parent f6be8531a8
commit b73c68de45
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
6 changed files with 51 additions and 44 deletions

View File

@ -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 static function providerBuild() {

View File

@ -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'])

View File

@ -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;
}

View File

@ -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 [];
}
}

View File

@ -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 {
}

View File

@ -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())