Closed (fixed)
Project:
Drupal core
Version:
9.3.x-dev
Component:
phpunit
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
13 Sep 2021 at 17:06 UTC
Updated:
2 Jun 2022 at 08:54 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
mondrakeHere's a patch.
Comment #3
longwaveThere 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.Comment #4
longwaveUnsure 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.Comment #5
daffie commentedAll the changed code looks good to me.
Do we want to do more changes?
Comment #6
mondrakeLooks good to me, reflected the enlarged scope in the IS and title.
Comment #7
larowlanshould we switch this to use the class constant whilst we're touching these lines?
Comment #8
mondrakeGood idea
Comment #9
vsujeetkumar commentedAddressed #7.
@larowlan this is what you expected, Please have a look and advise.
Comment #10
mondrakeLooks good.
Comment #11
mondrakeWhy 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?
Comment #12
vsujeetkumar commentedAccording to me these lines are untouched as per the above patch, I think so there is no need to convert these(#11) lines.
Comment #13
paulocsComment #14
paulocsI made changes suggested in #11 in the classes
ViewAjaxControllerTestandContentEntityBaseUnitTest.Its not possible to alter
PathPluginBaseTestbecausePathPluginBaseis an abstract class, so we need to mock it. The same thing forEntityDisplayModeBaseUnitTestwhich mocks the abstract classEntityDisplayModeBase.Comment #15
paulocsComment #16
longwaveI bet we can do something like
and then
to test the abstract classes?
Comment #17
longwaveEntityDisplayModeBaseUnitTest::testSet/GetTargetType() don't look like very good tests, they explicitly test the internals of the object via reflection.
Comment #18
paulocsI 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.
Comment #19
longwaveYou 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.
Comment #20
paulocsAh I got it.
I still prefer using the mock because its easier to read.
Comment #22
beatrizrodriguesComment #23
beatrizrodriguesI 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.
Comment #29
quietone commentedUpdating version and re-uploading the patch from #14.
Comment #30
alexpottLess 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!