Problem/Motivation

Noted in #3130606-34: MockBuilder::setMethods is deprecated in PHPUnit8 and removed from PHPUnit10, the usage of ::onlyMethods([]) smells like we do not need a mocks sometimes but rather use the acutal object.

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

Status: Active » Needs review
StatusFileSize
new1.19 KB

Here's a patch.

longwave’s picture

There are 24 uses of ->onlyMethods([]) following the parent issue, I think we should investigate them all here as most of them seem to be mocking the object under test, when we should probably just instantiate a real object.

longwave’s picture

StatusFileSize
new14.44 KB

Unsure if this is a step too far, but ContextualLinkManagerTest sets up a bunch of mocks and injects them into the main object with reflection and sets onlyMethods([]) - we can avoid all that simply by using the real constructor. The plugin factory doesn't even need mocking at all after this.

Similarly, when testing form validation we mock a FormValidator but then just pass in some arguments and don't stub out any methods, so we might as well use the real validator.

Two breadcrumb builders also can have similar treatment.

The other cases are where we mock abstract classes, or call disableOriginalConstructor() in unit tests, so these seemingly do need to stay as mocks.

daffie’s picture

All the changed code looks good to me.
Do we want to do more changes?

mondrake’s picture

Title: ImageTest uses a mock of Image with no purpose » Replace, in tests, mocks that do not configure doubles with their actual objects
Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

Looks good to me, reflected the enlarged scope in the IS and title.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkManagerTest.php
@@ -73,59 +60,34 @@ class ContextualLinkManagerTest extends UnitTestCase {
+    $language_manager = $this->createMock('Drupal\Core\Language\LanguageManagerInterface');
...
+    $this->moduleHandler = $this->createMock('\Drupal\Core\Extension\ModuleHandlerInterface');
...
+      $this->createMock('\Drupal\Core\Controller\ControllerResolverInterface'),

should we switch this to use the class constant whilst we're touching these lines?

mondrake’s picture

Status: Needs review » Needs work

Good idea

vsujeetkumar’s picture

Status: Needs work » Needs review
StatusFileSize
new14.6 KB
new1.75 KB

Addressed #7.
@larowlan this is what you expected, Please have a look and advise.

mondrake’s picture

Status: Needs review » Reviewed & tested by the community

Looks good.

mondrake’s picture

Status: Reviewed & tested by the community » Needs work

Why cannot be converted also Drupal\Tests\views\Unit\Controller\ViewAjaxControllerTest, Drupal\Tests\views\Unit\Plugin\display\PathPluginBaseTest, Drupal\Tests\Core\Config\Entity\EntityDisplayModeBaseUnitTest, Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest?

vsujeetkumar’s picture

According to me these lines are untouched as per the above patch, I think so there is no need to convert these(#11) lines.

paulocs’s picture

Assigned: Unassigned » paulocs
paulocs’s picture

I made changes suggested in #11 in the classes ViewAjaxControllerTest and ContentEntityBaseUnitTest.

Its not possible to alter PathPluginBaseTest because PathPluginBase is an abstract class, so we need to mock it. The same thing for EntityDisplayModeBaseUnitTest which mocks the abstract class EntityDisplayModeBase.

paulocs’s picture

StatusFileSize
new3.3 KB
new18.22 KB
longwave’s picture

I bet we can do something like

class TestPathPlugin extends PathPluginBase {}

and then

$this->pathPlugin = new TestPathPlugin([], 'path_base', [], $this->routeProvider, $this->state);

to test the abstract classes?

longwave’s picture

EntityDisplayModeBaseUnitTest::testSet/GetTargetType() don't look like very good tests, they explicitly test the internals of the object via reflection.

paulocs’s picture

I thought about #16 but don't you think it is better not to have one more class file and just mock the abstract class?
I think its easier to understand the test and we don't need a new file.

longwave’s picture

You don't need a new file, you can just declare it inside the same file as the test; we do this in a number of tests already. But I get your point, mocking is probably easier to read.

paulocs’s picture

Ah I got it.
I still prefer using the mock because its easier to read.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

beatrizrodrigues’s picture

Assigned: Unassigned » beatrizrodrigues
beatrizrodrigues’s picture

Assigned: beatrizrodrigues » Unassigned
Status: Needs review » Reviewed & tested by the community

I tested the patch and it applies normally on 9.4.x. Also, no sniff were found. I do think that mocking the abstract class is the easier to read option, and the patch at #15 addresses that. It also replaces all mocks (when possible and/or necessary) with its actual objects. I actually don't see any problems with the patch so, for me, is RTBC.

The last submitted patch, 3232714-14.patch, failed testing. View results

The last submitted patch, 3232714-14.patch, failed testing. View results

The last submitted patch, 3232714-14.patch, failed testing. View results

The last submitted patch, 3232714-14.patch, failed testing. View results

The last submitted patch, 3232714-14.patch, failed testing. View results

quietone’s picture

Version: 9.4.x-dev » 10.0.x-dev
StatusFileSize
new18.22 KB

Updating version and re-uploading the patch from #14.

alexpott’s picture

Version: 10.0.x-dev » 9.3.x-dev
Status: Reviewed & tested by the community » Fixed

Less mocks for the win. Nice work. Backported to 9.3.x to keep tests aligned.

Committed and pushed de4c1e82c2 to 10.0.x and da098e3256 to 9.5.x and 95a1d4db5c to 9.4.x and e6247d2632 to 9.3.x. Thanks!

  • alexpott committed de4c1e8 on 10.0.x
    Issue #3232714 by paulocs, vsujeetkumar, mondrake, longwave, quietone,...

  • alexpott committed da098e3 on 9.5.x
    Issue #3232714 by paulocs, vsujeetkumar, mondrake, longwave, quietone,...

  • alexpott committed 95a1d4d on 9.4.x
    Issue #3232714 by paulocs, vsujeetkumar, mondrake, longwave, quietone,...

  • alexpott committed e6247d2 on 9.3.x
    Issue #3232714 by paulocs, vsujeetkumar, mondrake, longwave, quietone,...

Status: Fixed » Closed (fixed)

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