From 25f9d574c79841de9abc6776f78c7e00c99eefb8 Mon Sep 17 00:00:00 2001 From: catch <6915-catch@users.noreply.drupalcode.org> Date: Thu, 17 Oct 2024 17:11:38 +0100 Subject: [PATCH] Issue #3480919 by mondrake: Method getMockForAbstractClass() is deprecated - replace in Drupal\Tests\views\Unit\Plugin\argument_validator\EntityTest --- core/.phpstan-baseline.php | 18 ------- .../Plugin/argument_validator/EntityTest.php | 11 +++- .../Tests/Core/Entity/EntityUnitTest.php | 11 +--- .../Tests/Core/Entity/EntityUrlTest.php | 50 +++++++++++-------- .../Tests/Core/Entity/StubEntityBase.php | 19 +++++++ 5 files changed, 58 insertions(+), 51 deletions(-) create mode 100644 core/tests/Drupal/Tests/Core/Entity/StubEntityBase.php diff --git a/core/.phpstan-baseline.php b/core/.phpstan-baseline.php index 84edbecf8203..b38db3554694 100644 --- a/core/.phpstan-baseline.php +++ b/core/.phpstan-baseline.php @@ -63165,12 +63165,6 @@ 'count' => 1, 'path' => __DIR__ . '/modules/views/tests/src/Unit/Plugin/area/ResultTest.php', ]; -$ignoreErrors[] = [ - // identifier: method.deprecated - 'message' => '#^Call to deprecated method getMockForAbstractClass\\(\\) of class PHPUnit\\\\Framework\\\\TestCase\\.$#', - 'count' => 2, - 'path' => __DIR__ . '/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php', -]; $ignoreErrors[] = [ // identifier: method.deprecated 'message' => '#^Call to deprecated method getMockForAbstractClass\\(\\) of class PHPUnit\\\\Framework\\\\MockObject\\\\MockBuilder\\.$#', @@ -74217,18 +74211,6 @@ 'count' => 1, 'path' => __DIR__ . '/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php', ]; -$ignoreErrors[] = [ - // identifier: phpunit.mockMethod - 'message' => '#^Trying to mock an undefined method getRevisionId\\(\\) on class Drupal\\\\Tests\\\\Core\\\\Entity\\\\UrlTestEntity\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php', -]; -$ignoreErrors[] = [ - // identifier: phpunit.mockMethod - 'message' => '#^Trying to mock an undefined method isDefaultRevision\\(\\) on class Drupal\\\\Tests\\\\Core\\\\Entity\\\\UrlTestEntity\\.$#', - 'count' => 1, - 'path' => __DIR__ . '/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php', -]; $ignoreErrors[] = [ // identifier: missingType.return 'message' => '#^Method Drupal\\\\Tests\\\\Core\\\\Entity\\\\FieldDefinitionTest\\:\\:factoryTypeProvider\\(\\) has no return type specified\\.$#', diff --git a/core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php b/core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php index 533e579e720a..d2791fabad1f 100644 --- a/core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/argument_validator/EntityTest.php @@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Tests\Core\Entity\StubEntityBase; use Drupal\Tests\UnitTestCase; use Drupal\views\Plugin\views\argument_validator\Entity; @@ -59,7 +60,10 @@ protected function setUp(): void { $this->entityTypeManager = $this->createMock(EntityTypeManagerInterface::class); $this->entityTypeBundleInfo = $this->createMock(EntityTypeBundleInfoInterface::class); - $mock_entity = $this->getMockForAbstractClass('Drupal\Core\Entity\EntityBase', [], '', FALSE, TRUE, TRUE, ['bundle', 'access']); + $mock_entity = $this->getMockBuilder(StubEntityBase::class) + ->disableOriginalConstructor() + ->onlyMethods(['bundle', 'access']) + ->getMock(); $mock_entity->expects($this->any()) ->method('bundle') ->willReturn('test_bundle'); @@ -71,7 +75,10 @@ protected function setUp(): void { ['test_op_3', NULL, FALSE, TRUE], ]); - $mock_entity_bundle_2 = $this->getMockForAbstractClass('Drupal\Core\Entity\EntityBase', [], '', FALSE, TRUE, TRUE, ['bundle', 'access']); + $mock_entity_bundle_2 = $this->getMockBuilder(StubEntityBase::class) + ->disableOriginalConstructor() + ->onlyMethods(['bundle', 'access']) + ->getMock(); $mock_entity_bundle_2->expects($this->any()) ->method('bundle') ->willReturn('test_bundle_2'); diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php index 858efd232275..199c79e8f697 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php @@ -7,7 +7,6 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Cache\Cache; use Drupal\Core\DependencyInjection\ContainerBuilder; -use Drupal\Core\Entity\EntityBase; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeRepositoryInterface; @@ -127,7 +126,7 @@ protected function setUp(): void { $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator->reveal()); \Drupal::setContainer($container); - $this->entity = new EntityBaseTest($this->values, $this->entityTypeId); + $this->entity = new StubEntityBase($this->values, $this->entityTypeId); } /** @@ -605,11 +604,3 @@ public function testCacheMaxAge(): void { } } - -class EntityBaseTest extends EntityBase { - public $id; - public $langcode; - public $uuid; - public $label; - -} diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php index 378efcbb5937..022928797ae0 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php @@ -5,7 +5,6 @@ namespace Drupal\Tests\Core\Entity; use Drupal\Core\DependencyInjection\ContainerBuilder; -use Drupal\Core\Entity\EntityBase; use Drupal\Core\Entity\EntityMalformedException; use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeInterface; @@ -96,7 +95,7 @@ class EntityUrlTest extends UnitTestCase { * @covers ::toUrl */ public function testToUrlNoId(): void { - $entity = $this->getEntity(UrlTestEntity::class, []); + $entity = $this->getEntity(StubEntityBase::class, []); $this->expectException(EntityMalformedException::class); $this->expectExceptionMessage('The "' . static::ENTITY_TYPE_ID . '" entity cannot have a URI as it does not have an ID'); @@ -113,7 +112,7 @@ public function testToUrlNoId(): void { */ public function testToUrlDefaultException(): void { $values = ['id' => static::ENTITY_ID]; - $entity = $this->getEntity(UrlTestEntity::class, $values); + $entity = $this->getEntity(StubEntityBase::class, $values); $this->entityType->getUriCallback()->willReturn(NULL); $this->expectException(UndefinedLinkTemplateException::class); @@ -131,7 +130,7 @@ public function testToUrlDefaultException(): void { */ public function testToUrlDefaultFallback(): void { $values = ['id' => static::ENTITY_ID, 'langcode' => $this->langcode]; - $entity = $this->getEntity(UrlTestEntity::class, $values); + $entity = $this->getEntity(StubEntityBase::class, $values); $this->registerLinkTemplate('edit-form'); /** @var \Drupal\Core\Url $url */ $url = $entity->toUrl(); @@ -168,7 +167,7 @@ public function testToUrlDefaultFallback(): void { */ public function testToUrlLinkTemplates($link_template, $expected_route_name): void { $values = ['id' => static::ENTITY_ID, 'langcode' => $this->langcode]; - $entity = $this->getEntity(UrlTestEntity::class, $values); + $entity = $this->getEntity(StubEntityBase::class, $values); $this->registerLinkTemplate($link_template); /** @var \Drupal\Core\Url $url */ @@ -216,7 +215,8 @@ public static function providerTestToUrlLinkTemplates() { */ public function testToUrlLinkTemplateRevision(bool $is_default_revision, string $link_template, string $expected_route_name, array $expected_route_parameters): void { $values = ['id' => static::ENTITY_ID, 'langcode' => $this->langcode]; - $entity = $this->getEntity(RevisionableEntity::class, $values); + $entity = $this->getEntity(RevisionableEntity::class, $values, ['getRevisionId', 'isDefaultRevision']); + assert($entity instanceof RevisionableEntity); $entity->method('getRevisionId')->willReturn(static::REVISION_ID); $entity->method('isDefaultRevision')->willReturn($is_default_revision); $this->registerLinkTemplate($link_template); @@ -265,7 +265,7 @@ public static function providerTestToUrlLinkTemplateRevision(): array { * @covers ::urlRouteParameters */ public function testToUrlLinkTemplateNoId($link_template, $expected_route_name): void { - $entity = $this->getEntity(UrlTestEntity::class, ['id' => static::ENTITY_ID]); + $entity = $this->getEntity(StubEntityBase::class, ['id' => static::ENTITY_ID]); $this->registerLinkTemplate($link_template); /** @var \Drupal\Core\Url $url */ @@ -310,7 +310,7 @@ public static function providerTestToUrlLinkTemplateNoId() { */ public function testToUrlLinkTemplateAddForm(bool $has_bundle_key, ?string $bundle_entity_type, string|false $bundle_key, array $expected_route_parameters): void { $values = ['id' => static::ENTITY_ID, 'langcode' => $this->langcode]; - $entity = $this->getEntity(UrlTestEntity::class, $values); + $entity = $this->getEntity(StubEntityBase::class, $values); $this->entityType->hasKey('bundle')->willReturn($has_bundle_key); $this->entityType->getBundleEntityType()->willReturn($bundle_entity_type); $this->entityType->getKey('bundle')->willReturn($bundle_key); @@ -355,7 +355,7 @@ public static function providerTestToUrlLinkTemplateAddForm(): array { * @covers ::linkTemplates */ public function testToUrlUriCallbackUndefined(array $bundle_info, $uri_callback): void { - $entity = $this->getEntity(UrlTestEntity::class, ['id' => static::ENTITY_ID]); + $entity = $this->getEntity(StubEntityBase::class, ['id' => static::ENTITY_ID]); $this->registerBundleInfo($bundle_info); $this->entityType->getUriCallback()->willReturn($uri_callback); @@ -396,7 +396,7 @@ public static function providerTestToUrlUriCallbackUndefined() { * @dataProvider providerTestToUrlUriCallback */ public function testToUrlUriCallback(array $bundle_info, ?\Closure $uri_callback): void { - $entity = $this->getEntity(UrlTestEntity::class, ['id' => static::ENTITY_ID, 'langcode' => $this->langcode]); + $entity = $this->getEntity(StubEntityBase::class, ['id' => static::ENTITY_ID, 'langcode' => $this->langcode]); $this->registerBundleInfo($bundle_info); $this->entityType->getUriCallback()->willReturn($uri_callback); @@ -433,7 +433,7 @@ public static function providerTestToUrlUriCallback(): array { * @covers ::uriRelationships */ public function testUriRelationships(): void { - $entity = $this->getEntity(UrlTestEntity::class, ['id' => static::ENTITY_ID]); + $entity = $this->getEntity(StubEntityBase::class, ['id' => static::ENTITY_ID]); $container_builder = new ContainerBuilder(); $url_generator = $this->createMock(UrlGeneratorInterface::class); @@ -463,7 +463,7 @@ public function testUriRelationships(): void { * Returns a mock entity for testing. * * @param string $class - * The class name to mock. Should be \Drupal\Tests\Core\Entity\UrlTestEntity + * The class name to mock. Should be \Drupal\Tests\Core\Entity\StubEntityBase * or a subclass. * @param array $values * An array of entity values to construct the mock entity with. @@ -471,7 +471,7 @@ public function testUriRelationships(): void { * (optional) An array of additional methods to mock on the entity object. * The getEntityType() and entityTypeBundleInfo() methods are always mocked. * - * @return \Drupal\Tests\Core\Entity\UrlTestEntity|\PHPUnit\Framework\MockObject\MockObject + * @return \Drupal\Tests\Core\Entity\StubEntityBase|\PHPUnit\Framework\MockObject\MockObject */ protected function getEntity($class, array $values, array $methods = []) { $methods = array_merge($methods, ['getEntityType', 'entityTypeBundleInfo']); @@ -502,7 +502,7 @@ protected function getEntity($class, array $values, array $methods = []) { * The expected route name of the generated URL. * @param array $expected_route_parameters * The expected route parameters of the generated URL. - * @param \Drupal\Tests\Core\Entity\UrlTestEntity|\PHPUnit\Framework\MockObject\MockObject $entity + * @param \Drupal\Tests\Core\Entity\StubEntityBase|\PHPUnit\Framework\MockObject\MockObject $entity * The entity that is expected to be set as a URL option. * @param bool $has_language * Whether or not the URL is expected to have a language option. @@ -553,12 +553,20 @@ protected function registerBundleInfo($bundle_info) { } -class UrlTestEntity extends EntityBase { - public $id; - public $langcode; - public $uuid; - public $label; +abstract class RevisionableEntity extends StubEntityBase implements RevisionableInterface { -} + /** + * {@inheritdoc} + */ + public function getRevisionId(): int|string|NULL { + return NULL; + } -abstract class RevisionableEntity extends UrlTestEntity implements RevisionableInterface {} + /** + * {@inheritdoc} + */ + public function isDefaultRevision($new_value = NULL): bool { + return FALSE; + } + +} diff --git a/core/tests/Drupal/Tests/Core/Entity/StubEntityBase.php b/core/tests/Drupal/Tests/Core/Entity/StubEntityBase.php new file mode 100644 index 000000000000..4b7e547920f5 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Entity/StubEntityBase.php @@ -0,0 +1,19 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\Core\Entity; + +use Drupal\Core\Entity\EntityBase; + +/** + * A stub base entity for testing purposes. + */ +class StubEntityBase extends EntityBase { + + public $id; + public $langcode; + public $uuid; + public $label; + +} -- GitLab