Skip to content

Commit

Permalink
[!!!][TASK] Avoid setMethods() on mocks
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lolli42 committed Apr 6, 2023
1 parent 54cc600 commit 6641616
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
5 changes: 0 additions & 5 deletions Build/phpstan/phpstan-baseline.neon
@@ -1,10 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method PHPUnit\\\\Framework\\\\MockObject\\\\MockBuilder\\<T of object&TYPO3\\\\TestingFramework\\\\Core\\\\AccessibleObjectInterface\\>\\:\\:setMethods\\(\\)\\.$#"
count: 1
path: ../../Classes/Core/BaseTestCase.php

-
message: "#^Instantiated class PHPUnit\\\\Framework\\\\RiskyTestError not found\\.$#"
count: 3
Expand Down
7 changes: 6 additions & 1 deletion Classes/Core/BaseTestCase.php
Expand Up @@ -124,10 +124,15 @@ protected function getAccessibleMock(
}

$mockBuilder = $this->getMockBuilder($this->buildAccessibleProxy($originalClassName))
->setMethods($methods)
->setConstructorArgs($arguments)
->setMockClassName($mockClassName);

if ($methods === null) {
$mockBuilder->onlyMethods([]);
} elseif (!empty($methods)) {
$mockBuilder->onlyMethods($methods);
}

if (!$callOriginalConstructor) {
$mockBuilder->disableOriginalConstructor();
}
Expand Down

0 comments on commit 6641616

Please sign in to comment.