Closed (fixed)
Project:
Drupal core
Version:
8.8.x-dev
Component:
phpunit
Priority:
Major
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
7 Dec 2017 at 11:45 UTC
Updated:
26 Oct 2019 at 04:09 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
ioana apetri commentedI am going to work on this issue.
Comment #3
mile23Shouldn't this issue be postponed on the outcome of #2795317: Allow PHPUnit 6+ support for object mocking ?
Comment #4
ioana apetri commentedI will continue to finish once the #2795317 is fixed.
Comment #5
mondrake#2795317: Allow PHPUnit 6+ support for object mocking has been committed.
Comment #6
martin107 commentedcd core
find . -name '*Test.php' | xargs grep '\->getMock' | wc -l
shows 2280 lines where this needs changing.
To break the task into bytes sized pieces perhaps we need some structure
A issue for all the units tests,
then a issue for all the kernel tests etc?
Comment #7
alexpottPatch attached replaces all calls to
\Drupal\Tests\UnitTestCase::getMock()apart from in\Drupal\Tests\PhpunitCompatibilityTraitTest(as expected).Most of the conversion was done using a replace using regex.
\$this->getMock\(([^\),]+)\)using the replacement$this->getMockBuilder($1)->getMock()and then manually converting the getMock()'s which have more than one argument. If the mock was using NULL for the methods argument this has been converted into just instantiating the class. There's no point in mocking at this point because nothing is actually mocked - we're just adding a layer of complexity for no reason.Comment #8
alexpottIf we want to break this up into 2 patches I guess we could do the one that is creatable via regex replace and then the one that gets the rest.
The regex just messes up:
They are simple to fix manually. Here's the 2 possible patches. Not the with args patch does not apply because of context... i.e. it needs the regex patch applied first.
The plus point for this approach is the regex patch can be reviewed with git diff --color-words
Comment #10
borisson_The patch no longer applies, but I agree with the approach to do as much as possible with the regex and do the rest by hand.
Comment #13
lendudeLooks like great progress already, lets get this back on the map
Comment #14
gábor hojtsygetMock() is the largest portion of deprecated uses in Drupal core. Drupal 8.7 currently has around 4k deprecations and over 1k is single-handedly for getMock(). Looking forward to have this in :) Upping to major for its impact.
Comment #15
lendudeHere is a reroll of the 'with-args' patch, the other one we should be able to reconstruct using the regex from @alexpott
Comment #16
lendudeJust ran the regex and with #15 applied on top of that I added a trigger error to
\Drupal\Tests\PhpunitCompatibilityTrait::getMockand ran the unit testsuite:17x in SessionConfigurationTest::testEnforcedSessionNameViaCookieDomain from Drupal\Tests\Core\Session
17x in SessionConfigurationTest::testGeneratedSessionName from Drupal\Tests\Core\Session
16x in FieldPluginBaseTest::testRenderAsLinkWithPathAndOptions from Drupal\Tests\views\Unit\Plugin\field
15x in FieldPluginBaseTest::testRenderAsLinkWithUrlAndOptions from Drupal\Tests\views\Unit\Plugin\field
12x in CounterTest::setUp from Drupal\Tests\views\Unit\Plugin\field
11x in SessionConfigurationTest::testEnforcedCookieDomain from Drupal\Tests\Core\Session
11x in SessionConfigurationTest::testGeneratedCookieDomain from Drupal\Tests\Core\Session
9x in FieldPluginBaseTest::testRenderAsLinkWithPathAndTokens from Drupal\Tests\views\Unit\Plugin\field
9x in CommandLineOrUnsafeMethodTest::setUp from Drupal\Tests\Core\PageCache
8x in FieldPluginBaseTest::testRenderTrimmedWithMoreLinkAndPath from Drupal\Tests\views\Unit\Plugin\field
7x in SpecialAttributesRouteSubscriberTest::testOnRouteBuildingInvalidVariables from Drupal\Tests\Core\EventSubscriber
6x in RouteNormalizerRequestSubscriberTest::setUp from Drupal\Tests\redirect\Unit
6x in SessionConfigurationTest::testCookieSecureNotOverridable from Drupal\Tests\Core\Session
6x in SessionConfigurationTest::testCookieSecure from Drupal\Tests\Core\Session
6x in DomainRedirectRequestSubscriberTest::testDomainRedirect from Drupal\Tests\redirect_domain\Unit
4x in SpecialAttributesRouteSubscriberTest::testOnRouteBuildingValidVariables from Drupal\Tests\Core\EventSubscriber
4x in PhpunitCompatibilityTraitTest::testGetMock from Drupal\Tests
4x in FieldPluginBaseTest::testRenderAsExternalLinkWithPathAndTokens from Drupal\Tests\views\Unit\Plugin\field
3x in RedirectRequestSubscriberTest::testRedirectLogicWithoutQueryRetaining from Drupal\Tests\redirect\Unit
3x in RedirectRequestSubscriberTest::testRedirectLogicWithQueryRetaining from Drupal\Tests\redirect\Unit
3x in CollectRoutesTest::setUp from Drupal\Tests\rest\Unit
3x in FieldPluginBaseTest::testElementClassesWithTokens from Drupal\Tests\views\Unit\Plugin\field
3x in ViewListBuilderTest::testBuildRowEntityList from Drupal\Tests\views_ui\Unit
3x in UnitTestSuite::suite from Drupal\Tests\TestSuites
3x in MigrateSourceTest::testCount from Drupal\Tests\migrate\Unit
2x in PsrResponseSubscriberTest::testDoesNotConvertControllerResult from Drupal\Tests\Core\EventSubscriber
2x in PsrResponseSubscriberTest::setUp from Drupal\Tests\Core\EventSubscriber
2x in MigrateSourceTest::testNextNeedsUpdate from Drupal\Tests\migrate\Unit
2x in PhpunitCompatibilityTraitTest::testCreateMock from Drupal\Tests
1x in EntityDisplayModeBaseUnitTest::testSetTargetType from Drupal\Tests\Core\Config\Entity
1x in FieldPluginBaseTest::testRenderNoResult from Drupal\Tests\views\Unit\Plugin\field
1x in FieldPluginBaseTest::testGetRenderTokensWithoutArguments from Drupal\Tests\views\Unit\Plugin\field
1x in FormValidatorTest::testElementValidate from Drupal\Tests\Core\Form
1x in FormValidatorTest::testExecuteValidateHandlers from Drupal\Tests\Core\Form
1x in FieldPluginBaseTest::testGetRenderTokensWithoutFieldsAndArguments from Drupal\Tests\views\Unit\Plugin\field
1x in FieldPluginBaseTest::testRenderAsLinkWithoutPath from Drupal\Tests\views\Unit\Plugin\field
1x in FieldPluginBaseTest::testGetRenderTokensWithArguments from Drupal\Tests\views\Unit\Plugin\field
1x in ConnectionTest::testDestroy from Drupal\Tests\Core\Database
1x in EntityDisplayModeBaseUnitTest::testGetTargetType from Drupal\Tests\Core\Config\Entity
1x in MigrateSourceTest::testPrepareRowFalse from Drupal\Tests\migrate\Unit
1x in ReverseProxyMiddlewareTest::testNoProxy from Drupal\Tests\Core\StackMiddleware
1x in ViewUIObjectTest::testEntityDecoration from Drupal\Tests\views_ui\Unit
1x in PsrResponseSubscriberTest::testConvertsControllerResult from Drupal\Tests\Core\EventSubscriber
1x in ImageTest::testChmodFails from Drupal\Tests\Core\Image
1x in ImageTest::testSave from Drupal\Tests\Core\Image
1x in FieldItemListTest::testDefaultValuesFormSubmit from Drupal\Tests\Core\Field
1x in FieldItemListTest::testDefaultValuesFormValidate from Drupal\Tests\Core\Field
1x in FieldItemListTest::testDefaultValuesForm from Drupal\Tests\Core\Field
1x in AggregatorPluginSettingsBaseTest::testSettingsForm from Drupal\Tests\aggregator\Unit\Plugin
1x in MigrateSourceTest::testNewHighwater from Drupal\Tests\migrate\Unit
1x in ForumManagerTest::testGetIndex from Drupal\Tests\forum\Unit
1x in ForumListingBreadcrumbBuilderTest::testBuild from Drupal\Tests\forum\Unit\Breadcrumb
1x in ForumNodeBreadcrumbBuilderTest::testBuild from Drupal\Tests\forum\Unit\Breadcrumb
1x in MigrateSourceTest::testHighwaterTrackChangesIncompatible from Drupal\Tests\migrate\Unit
1x in MigrateSourceTest::testCountCacheKey from Drupal\Tests\migrate\Unit
1x in MigrateSourceTest::testOutdatedHighwater from Drupal\Tests\migrate\Unit
1x in ViewUIObjectTest::testIsLocked from Drupal\Tests\views_ui\Unit
So the patch in #15 needs to be extended to address those too.
Comment #17
martin107 commentedComment #18
gábor hojtsyI wrote this above in March:
The latest phpstan report from today shows 2793 errors, of those 1208 are still getmock. So close to half of all the deprecated uses. This would be quite a big step forward.
Comment #19
volegerRerolled #15 (core/tests/Drupal/Tests/Core/Config/Entity/EntityDisplayModeBaseUnitTest.php - 2 hunks)
First iteration:
- replace generic
\$this->getMock\(('[\w\\]+')\)with\$this->createMock\($1\)withincore/testsfolder (used replace tool of PHPStorm)- replace generic
\$this->getMock\(([\w]+::class)\)with\$this->createMock\($1\)withincore/testsfolder (used replace tool of PHPStorm)Comment #20
volegerSecond iteration:
- replace generic
\$this->getMock\(('[\w\\]+')\) with\$this->createMock\($1\)withincore/modulesfolder (used replace tool of PHPStorm)- replace generic
\$this->getMock\(([\w]+::class)\)with\$this->createMock\($1\)withincore/modulesfolder (used replace tool of PHPStorm)Comment #21
volegerThird iteration:
- few manual replacements to
createMock()- few manual replacements to
getMockBuilder()Looks like this should be ready for review. Hope we didn't miss any other calls.
Comment #22
volegerActually, one call left in
core/tests/Drupal/Tests/PhpunitCompatibilityTrait.php:110but this is the overriding of the
createMockmethod. Looks like we need to remove that method in favor of the parentcreateMock.Comment #23
volegerAdded the deprecation error call for the getMock() method. Fixed CS issue for the @deprecated tag.
Comment #24
volegerWeird error in
\Drupal\Tests\serialization\Unit\Normalizer\ContentEntityNormalizerTest::testSupportsNormalization()test.There was tried to mock
Drupal\Core\Entity\ConfigEntityInterfacewhich not exists in the codebase. But with the new method call, the error was appearing. To fix this test we need to mockDrupal\Core\Config\Entity\ConfigEntityInterfacewhich exists in the codebase.Comment #25
volegerReupload the interdiff
Comment #27
volegerAdded @expectedDeprecation message and @group legacy to the
\Drupal\Tests\PhpunitCompatibilityTraitTest::testGetMocktest.Comment #30
volegerAlso, split up the
\Drupal\Tests\PhpunitCompatibilityTraitTest::testCreateMock()testComment #32
volegerThis patch contains simple deprecation calls replacement and only 1 test fix.
Wondering, is it ok to fix that test in that way? See #24 and interdiff #25
Comment #33
lendudeLets not mess with the test in this issue, it would make this much harder to review (manual change in 500k of scripted stuff), I think we are addressing this in #3053417: Clean up PhpunitCompatibilityTrait so probably best wait for that to land
Comment #34
lendude@alexpott pointed out that there was no need to wait, that this is fine with the test change in, so went through this with
git diff --color-words, and all looks good.Apparently we need both, so this is good too.
Comment #35
alexpottCommitted 91fe1d2 and pushed to 8.8.x. Thanks!
Let's move on :) this will be a little disruptive but we've already remove PHPUnit 4 from Drupal 8.8.x so this feels like the right moment. If we don't then this patch will enter re-roll-itis. And mostly this patch is a find-and-replace.
Comment #38
xjmWe should mention the end of PHPUnit 4 in the release notes (along with its context of having dropped PHP 5 support entirely). Can we get a release note for that? Thanks!
Comment #39
xjmActually, the release note is already covered by #3008870: Drop support for PHPUnit 4.8 once PHP 5 is no longer supported (8.8.x). That release note could use some work, though, so reopening it.