Problem/Motivation
PHPUnit 12.5.0+ is changing the way that mock objects work:
- Mock objects that do not configure expectations should be converted to stubs. Otherwise, they throw a notice:
No expectations were configured for the mock object for \Drupal\comment\CommentManagerInterface. You should refactor your test code and use a test stub instead. $this->any()is deprecated. It was equivalent to having no expectation, in which case the object should be a stub (see the previous bullet) or a more exact number of expectations should be added.with()is deprecated for stubs because it was deemed to be equally pointless.
Steps to reproduce
- Require PHPUnit 12 with Composer.
- Fix compatibility issues with PHPUnit 12 by applying this patch.
- Run the Unit tests for a library with
--display-phpunit-notices --display-phpunit-deprecations. - Observe the PHPUnit notices that occur.
Proposed resolution
Fix notices in the tests for the following core libraries, which currently only represents 6 affected classes.
core/tests/Drupal/Tests/Core/Image/core/tests/Drupal/Tests/Core/ParamConverter/core/tests/Drupal/Tests/Core/Test/
Note that TestDiscoveryTest::testTestInfoParser() will fail 5 times if running the tests on PHPUnit 12. This is due to a PHPUnit function that we're using in our test discovery being removed from version 12. If it were an easy fix, then I'd fix it here. But after a little investigation I couldn't figure out how it should be fixed or if the discovery needs a more drastic change because this is related to test annotations. So I'm declaring this to be out-of-scope.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Issue fork drupal-3579905
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3579905-mockobject-fixes
changes, plain diff MR !15131
Comments
Comment #3
dcam commentedHopefully this will be the last batch from the primary conversion issues! I expect there will be at least one more for clean-up because I know that we had instances of
any()added to the code base since we started this.Once again,
TestDiscoveryTest::testTestInfoParser()will fail on PHPUnit 12! See the issue summary for more info.Comment #4
dcam commentedComment #5
dcam commentedFYI: the recently-committed
UnitTestCasechanges have been merged into this MR, so they do not need to be applied separately for testing.Comment #6
smustgrave commentedImage = OK (26 tests, 77 assertions)
ParamConverter = OK (34 tests, 71 assertions)
Test = I get a fatal error
PHP Fatal error: Cannot override final method PHPUnit\Framework\TestCase::__construct() in /var/www/html/core/tests/Drupal/Tests/BrowserTestBase.php on line 181
Fatal error: Cannot override final method PHPUnit\Framework\TestCase::__construct() in /var/www/html/core/tests/Drupal/Tests/BrowserTestBase.php on line 181
Stack trace:
Mind taking a look at that?
Comment #7
dcam commentedThat's supposed to be taken care of by the PHPUnit 12 patch linked in the IS. If you check it, then you'll see that
__construct()is deleted fromBrowserTestBase. Maybe this change got reverted on your local environment.Comment #8
smustgrave commentedWeird the folder was there but the file was gone
Comment #10
catchCommitted/pushed to main, thanks!