Error when mocking built-in function with pass-by-reference argument and argument with default value (PHP 8)
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 42/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- php
- Domain
- testing-qa
Research direction
Start at AspectMock/Core/Mocker.php:275 and trace how built-in function signatures are turned into the temporary wrapper; Registry.php:38 and Test.php:248 show the surrounding call path. Reproduce with PHP 8.1 using flock, system, stream_socket_client, or stream_select, then verify that generated declarations parse without duplicate defaults and the mocked calls still work.
Written by the indexing model from the issue text.
Description
This is for AspectMock 4.1.1.
During our migration from PHP 7.4 to PHP 8.1, we've encountered this pattern a few times:
When our PHP-Unit tests mock a PHP built-in function that has at least one argument passed by reference and one argument with a default value, the mock fails with the error ParseError: syntax error, unexpected token "=", expecting ")".
The stack track looks like this:
/tmp/flockoipmSG:4
/petabox/www/common/vendor/codeception/aspect-mock/src/AspectMock/Core/Mocker.php:275
/petabox/www/common/vendor/codeception/aspect-mock/src/AspectMock/Core/Registry.php:38
/petabox/www/common/vendor/codeception/aspect-mock/src/AspectMock/Test.php:248
Examining the temporary file reveals the problem:
$ cat /tmp/flockoipmSG
<?php
namespace Atomic;
if (!function_exists('Atomic\flock')) {
function flock($p0, int $p1, &$p2 = null=NULL) {
$args = [];
switch(count(func_get_args())) {
case 3: $args = [$p0, $p1, &$p2]; break;
case 2: $args = [$p0, $p1]; break;
case 1: $args = [$p0]; break;
}
if (($__am_res = __amock_before_func('Atomic','flock', $args)) !== __AM_CONTINUE__) {
return $__am_res;
}
return call_user_func_array('flock', $args);
}
}
The function declaration has two default values for an argument (function flock($p0, int $p1, &$p2 = null=NULL).
Other functions which have given us this problem are:
system()stream_socket_client()stream_select()
The last one is interesting because, unlike the others, the passed-by-reference arguments don't have default values. Only the final argument does:
stream_select(
?array &$read,
?array &$write,
?array &$except,
?int $seconds,
?int $microseconds = null
): int|false
That's what leads me to believe the problem is the existence of both in the function signature, but not necessarily that a single argument needs to be both pass-by-reference and have a default value for this to fail.
We don't see this under PHP 7.4, so I assume this related to the PHP version.
- Dominant language
- PHP
- Stars
- 784
- Forks
- 130
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Codeception/AspectMock
-
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
Codeception/AspectMock#208 · 2 comments · 5 reactions ·
-
Difficulty 3/5 1-2 days Newbie friendliness 25/100
Codeception/AspectMock#206 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
Codeception/AspectMock#203 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 35/100
Codeception/AspectMock#202 · 5 comments · 2 reactions ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Codeception/AspectMock#196 · 1 comment ·
All issues in Codeception/AspectMock
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
taskforcesh/bullmq#5238 ·
Maintainers usually reply within 1 day
-
[Bug] v-change-database-password reports success but doesn't change the password when MySQL validate_password rejects itPossibly taken @bfs2021 claimed this today. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Automattic/jetpack#55517 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 84/100
-
area/import type/enhancement when/later
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day