Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[!!!][TASK] Avoid setMethods() on mocks #449

Merged
merged 1 commit into from Apr 6, 2023
Merged

[!!!][TASK] Avoid setMethods() on mocks #449

merged 1 commit into from Apr 6, 2023

Conversation

lolli42
Copy link
Member

@lolli42 lolli42 commented Apr 6, 2023

setMethods() has been removed with phpunit 10.

$this->getAccessibleMock() used this to only
partially mock a test subject. The patch switches
to onlyMethods() now.

There is one difference: onlyMethods() fails if
a methods that should be mocked does not exist,
while setMethods() did not fail on this.

So this is more strict now. It was frequently used in core tests mocking ['dummy'] to say "mock no method". Those have to be switched to use 'null' as argument to $this->getAccessibleMock() now. Also, when a class is refactored, and a method that has been previously mocked is removed with the refactoring, the mock has to be adapted to remove the method-mock request from the test as well now, otherwise the test will fail.

Rules for the second argument of getAccessibleMock():

  • "keep all methods, mock nothing": Hand over 'null'
  • "mock all methods": Hand over empty array [] (default)
  • "mock single methods": Hand over an array with method names

Releases: main, 7
Resolves: #444

Changes proposed in this pull request

Steps to test the solution

Results

Fixes: #

setMethods() has been removed with phpunit 10.

$this->getAccessibleMock() used this to only
partially mock a test subject. The patch switches
to onlyMethods() now.

There is one difference: onlyMethods() fails if
a methods that should be mocked does not exist,
while setMethods() did not fail on this.

So this is more strict now. It was frequently used in
core tests mocking ['dummy'] to say "mock no method".
Those have to be switched to use 'null' as argument
to $this->getAccessibleMock() now. Also, when a class
is refactored, and a method that has been previously
mocked is removed with the refactoring, the mock has
to be adapted to remove the method-mock request from
the test as well now, otherwise the test will fail.

Rules for the second argument of getAccessibleMock():

* "keep all methods, mock nothing": Hand over 'null'
* "mock all methods": Hand over empty array [] (default)
* "mock single methods": Hand over an array with method names

Releases: main, 7
Resolves: #444
@lolli42 lolli42 merged commit f9baa96 into 7 Apr 6, 2023
8 checks passed
@lolli42 lolli42 deleted the lolli-1 branch April 6, 2023 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant