-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Methods that return never
cannot be doubled
#5048
Comments
never
throws a PHP Fatal Errornever
cannot be doubled
Thank you for bringing this to my attention. Given this interface: interface I
{
public function m(): never;
} when class Mock_InterfaceWithMethodReturningNever_f206cac2 implements PHPUnit\Framework\MockObject\MockObject, PHPUnit\TestFixture\MockObject\InterfaceWithMethodReturningNever
{
use \PHPUnit\Framework\MockObject\Api;
use \PHPUnit\Framework\MockObject\Method;
use \PHPUnit\Framework\MockObject\MockedCloneMethodWithVoidReturnType;
public function neverReturns(): never
{
$__phpunit_arguments = [];
$__phpunit_count = func_num_args();
if ($__phpunit_count > 0) {
$__phpunit_arguments_tmp = func_get_args();
for ($__phpunit_i = 0; $__phpunit_i < $__phpunit_count; $__phpunit_i++) {
$__phpunit_arguments[] = $__phpunit_arguments_tmp[$__phpunit_i];
}
}
$this->__phpunit_getInvocationHandler()->invoke(
new \PHPUnit\Framework\MockObject\Invocation(
'PHPUnit\TestFixture\MockObject\InterfaceWithMethodReturningNever', 'neverReturns', $__phpunit_arguments, 'never', $this, false
)
);
}
} When the class never
{
}
class Mock_never_5c984be4 extends never implements PHPUnit\Framework\MockObject\MockObject
{
use \PHPUnit\Framework\MockObject\Api;
use \PHPUnit\Framework\MockObject\Method;
use \PHPUnit\Framework\MockObject\MockedCloneMethodWithVoidReturnType;
} This happens because no return value has been configured for the stubbed method. The default return value generator is not aware of However, even if the default return value generator were aware of
Turns out, I only ever implemented support for the To "solve" the Right now, I have no idea what the right thing to do here is. I am open to suggestions. |
@muglug @ondrejmirtes Do you have an idea what the correct behaviour for PHPUnit would be here? Thanks! |
So
It's hard to infer what the mocked method does out of these three options. At the same time, 1) and 2) are impractical in testing environment so I'd always follow 3). PHPUnit already must have some differentiating logic what to do between a type that returns a value (like A quickfix here would be to simply not return anything (to behave same way as After that it's a matter of personal taste what should be done and how the behaviour should be configured in case the method is called: a) Read There might be other options too... |
Thank you, @ondrejmirtes. Unfortunately, "not return anything" does not work as it results in the I think I will change the runtime behaviour to |
To confirm, that |
We would like to assert that a method with the During tests, infinite execution or exiting the process does not seem appropriate. The only thing that remains, is throwing an exception. Hence, during tests, a How about introducing a special exception, that could be overridden with |
Instead, we use anonymous classes for now. Eventually this will be released: sebastianbergmann/phpunit#5048
- fix types where neos depends on it (by correcting types and adding `never`) - adjust unit test as `never` cannot be doubled eventually this will be fixed via: sebastianbergmann/phpunit#5048 - fix ci and style as neos 9 followup for #3218
With #3218 PHPStan level 1 was added to the whole Flow code base and CI for Flow 8. This upmerged change needs some adjustments to pass the CI in Flow 9 - fix types and remove deprecated code - fix types where neos depends on it (by correcting types and adding `never`) - adjust unit test as `never` cannot be doubled (eventually this will be fixed via: sebastianbergmann/phpunit#5048) - fix ci and style as neos 9 followup for #3218
With neos/flow-development-collection#3218 PHPStan level 1 was added to the whole Flow code base and CI for Flow 8. This upmerged change needs some adjustments to pass the CI in Flow 9 - fix types and remove deprecated code - fix types where neos depends on it (by correcting types and adding `never`) - adjust unit test as `never` cannot be doubled (eventually this will be fixed via: sebastianbergmann/phpunit#5048) - fix ci and style as neos 9 followup for neos/flow-development-collection#3218
With neos/flow-development-collection#3218 PHPStan level 1 was added to the whole Flow code base and CI for Flow 8. This upmerged change needs some adjustments to pass the CI in Flow 9 - fix types and remove deprecated code - fix types where neos depends on it (by correcting types and adding `never`) - adjust unit test as `never` cannot be doubled (eventually this will be fixed via: sebastianbergmann/phpunit#5048) - fix ci and style as neos 9 followup for neos/flow-development-collection#3218
With neos/flow-development-collection#3218 PHPStan level 1 was added to the whole Flow code base and CI for Flow 8. This upmerged change needs some adjustments to pass the CI in Flow 9 - fix types and remove deprecated code - fix types where neos depends on it (by correcting types and adding `never`) - adjust unit test as `never` cannot be doubled (eventually this will be fixed via: sebastianbergmann/phpunit#5048) - fix ci and style as neos 9 followup for neos/flow-development-collection#3218
Summary
Mocking a class with a method that returns
never
throws a php fatal error in MockClass::generate().Current behavior
That error seems to be thrown from executing this code:
How to reproduce
Create a mock of a class with a method that returns
never
:Expected behavior
The mock code doesn't generate a class named
never
.The text was updated successfully, but these errors were encountered: