Problem/Motivation

TestCase::getMockClass() is removed from PHPUnit 10.

In PHPUnit 9.6, using getMockClass() will yield a deprecation error,

Therefore the use of the method needs to be replaced.

See https://github.com/sebastianbergmann/phpunit/issues/5064

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

mondrake created an issue. See original summary.

mondrake’s picture

Issue tags: +PHPUnit 9.6
mondrake’s picture

Issue summary: View changes
mondrake’s picture

Status: Active » Needs review
StatusFileSize
new3.38 KB

Here's a patch replacing the deprecated method.

spokje’s picture

Status: Needs review » Needs work

Look great, but you might have missed one occurence in \Drupal\Tests\Core\Entity\EntityFieldManagerTest::setUpEntityTypeDefinitions()?

mondrake’s picture

Status: Needs work » Needs review
StatusFileSize
new4.23 KB

Woah. Thanks @Spokje.

Note to @mondrake: if you save the files after editing, your chances to get proper diffs increase...

longwave’s picture

StatusFileSize
new5.05 KB

Seems like a lot of this code isn't needed?

longwave’s picture

In fact it seems that in some of the tests setUpEntityTypeManager() doesn't need to be called at all!

spokje’s picture

Status: Needs review » Needs work

Could do with slightly less PHPCS errors, but seems like a nice clean-up :)

mondrake’s picture

Status: Needs work » Needs review
StatusFileSize
new6.11 KB

Status: Needs review » Needs work

The last submitted patch, 10: 3322784-10.patch, failed testing. View results

mondrake’s picture

Status: Needs work » Needs review
spokje’s picture

Status: Needs review » Reviewed & tested by the community

- Reason why we're doing this is clearly stated in the IS.
- All issues were addressed.
- No remaining occurrences of TestCase::getMockClass() found after applying the patch.
- Green TestBot.
- Added Bonus: Code clean-up.

RTBC for me.

  • xjm committed 7432d6d1 on 10.1.x
    Issue #3322784 by mondrake, longwave, Spokje: TestCase::getMockClass()...

  • xjm committed c670adb1 on 10.0.x
    Issue #3322784 by mondrake, longwave, Spokje: TestCase::getMockClass()...

  • xjm committed 5a94f228 on 9.5.x
    Issue #3322784 by mondrake, longwave, Spokje: TestCase::getMockClass()...
xjm’s picture

Version: 10.1.x-dev » 9.5.x-dev
Status: Reviewed & tested by the community » Fixed

When I see seemingly unnecessary test coverage like this I always wonder "why is it that way"? For all these tests, that's #2337191-9: Split up EntityManager into many services. The getMockClass() usage as it stands was added in the original prototype in that issue, and it appears to have been copied to all those tests from the original one:

+++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php
@@ -140,93 +151,63 @@ class EntityManagerTest extends UnitTestCase {
   protected function setUpEntityManager($definitions = array()) {
-    $class = $this->getMockClass('Drupal\Core\Entity\EntityInterface');
-    foreach ($definitions as $entity_type) {
-      $entity_type->expects($this->any())
-        ->method('getClass')
-        ->will($this->returnValue($class));
+    $class = $this->getMockClass(EntityInterface::class);
+    foreach ($definitions as $key => $entity_type) {
+      // \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called
+      // by \Drupal\Core\Entity\EntityManager::processDefinition() so it must
+      // always be mocked.
+      $entity_type->getLinkTemplates()->willReturn([]);
+
+      // Give the entity type a legitimate class to return.
+      $entity_type->getClass()->willReturn($class);

I reviewed each instance and confirmed that EntityTypeManagerTest does actually use it for something other than testing getMockClass() itself, but that the others don't.

Committed to 10.1.x, and cherry-picked to 10.0.x and 9.5.x as mostly a dead test code cleanup. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.