Fatal error when mocking methods with nullable parameters in HHVM 3.21.0 running in PHP7 mode
fpoirotte opened this issue · 2 comments
Hello,
Trying to mock a class with a method that contains a nullable parameter results in a fatal error under HHVM 3.21.0 when running in PHP7 mode.
This is triggered for example when mocking the built-in "DateTime" class, because its "createFromFormat" method contains a nullable third argument (named $object in regular PHP and $timezone in HHVM).
Tested with:
Debian 9 (Stretch)
HHVM 3.21.0 (from Debian's sid repository)
phpunit/phpunit-mock-objects 4.0.4
Reproduce code:
$ cat test.php
<?php
include('/home/.../vendor/autoload.php');
$gen = new PHPUnit_Framework_MockObject_Generator();
$gen->getMock('DateTime');
$ hhvm -d hhvm.php7.all=1 test.php
Fatal error: syntax error, unexpected ??, expecting '&' or T_VARIABLE or ... in /home/.../vendor/phpunit/phpunit-mock-objects/src/Generator.php(263)(3437554ce2705780dc4f854e0f07d066) : eval()'d code on line 34
Fatal error: Uncaught Error: Class undefined: Mock_DateTime_6d3e4f3d in /home/.../vendor/phpunit/phpunit-mock-objects/src/Generator.php:216
Stack trace:
#0 /home/.../vendor/phpunit/phpunit-mock-objects/src/Generator.php(191): PHPUnit_Framework_MockObject_Generator->getObject()
#1 /tmp/test.php(6): PHPUnit_Framework_MockObject_Generator->getMock()
#2 {main}
Expected result:
No error
Actual result:
See above.
This error is due to the fact that under HHVM 3.21, when running in PHP 7 mode, the type-hints returned by ReflectionParameter::getType()
already include the "?" prefix for nullable arguments.
This does not happen with the official PHP interpreter or with HHVM when running in PHP5 mode because the "?" prefix is omitted from the type-hints.
Changing the code in https://github.com/sebastianbergmann/phpunit-mock-objects/blob/master/src/Generator.php#L1080 & https://github.com/sebastianbergmann/phpunit-mock-objects/blob/master/src/Generator.php#L1085 to strip the "?" prefix when it's already present (using ltrim for example) fixes the issue for me.
Best regards,
François
Sorry, but I cannot help you. PHPUnit is not supported on HHVM. Please open a ticket with the vendor of that alternative runtime for PHP.
Thank you for taking the time to comment on this issue.
I understand that your resources are limited and thus you cannot support every PHP implementation out there. Still, I was wondering if you would accept pull requests trying to improve support for HHVM (in this particular case, the patch only impacts 2 lines of code and I'd be more than happy to submit a PR if time is an issue on your end)
Best regards,
François.