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

[Bug]: Unable to use named parameters #1344

Open
victorlap opened this issue Jan 24, 2025 · 2 comments
Open

[Bug]: Unable to use named parameters #1344

victorlap opened this issue Jan 24, 2025 · 2 comments
Labels

Comments

@victorlap
Copy link

victorlap commented Jan 24, 2025

What Happened

When using a dataProvider with named arguments, phpunit throws a deprecation warning and will not use the argument correctly

How to Reproduce

<?php

it('can use named arguments', function (
    string $a = 'foo',
    ?string $b = null,
): void {
    expect(true)->toBeTrue();
})->with([
    'positional arguments' => ['foo', 'bar'],
    'named argument' => ['b' => 'bar'],
]);

Running the above code produces a deprecation warning and error similar to the following:

   DEPR  Tests\Unit\Test
  ! it can use named arguments → Providing invalid named argument $b for method P\Tests\Unit\Test::it_can_use_named_arguments() is deprecated and will not be supported in PHPUnit 11.0. 0.02s
  ✓ it can use named arguments with dataset "positional arguments"                                                                                                                    0.18s

  Tests:    1 deprecated, 1 passed (2 assertions)
  Duration: 0.63s

This should work according to the phpunit functionality, however the generated test class code doesn't include the parameters. Phpunit uses reflection to get the parameters from the function and therefore this functionality is not working.

For completeness sake, this is the generated code:

namespace P\Tests\Unit;

use Pest\Repositories\DatasetsRepository as __PestDatasets;

/**
 * @testdox /var/www/html/tests/Unit/Test.php
 */
#[\AllowDynamicProperties]
final class Test extends \Tests\TestCase implements \Pest\Contracts\HasPrintableTestCaseName
{
    use \Pest\Concerns\Testable, \Pest\Concerns\Expectable, \Pest\Arch\Concerns\Architectable;

    private static $__filename = '/var/www/html/tests/Unit/Test.php';

    /**
     * @test
     * @testdox it can use named arguments
     * @dataProvider __pest_evaluable_it_can_use_named_arguments_dataset
     */
    public function __pest_evaluable_it_can_use_named_arguments()
    {
        $test = \Pest\TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name())->getClosure($this);

        return $this->__runTest(
            $test,
            ...func_get_args(),
        );
    }

    public static function __pest_evaluable_it_can_use_named_arguments_dataset()
    {
        return __PestDatasets::get(self::$__filename, '__pest_evaluable_it_can_use_named_arguments');
    }
}

Sample Repository

No response

Pest Version

v2.36.0

PHP Version

8.4.2

Operation System

Linux

Notes

No response

@victorlap victorlap added the bug label Jan 24, 2025
@victorlap victorlap changed the title [Bug]: [Bug]: Unable to use named parameters Jan 24, 2025
@danielh-official
Copy link

<?php

it('can use named arguments', function (
    string $a,
    ?string $b = null,
): void {
    expect(true)->toBeTrue();
})->with([
    'positional arguments' => ['foo', 'bar'],
    'named argument' => ['a' => 'bar'],
]);

Just to make sure, do you have problems with this version of the reproduction code as well?

I'm certain Pest only allows positional arguments, but if it allowed named arguments, the original reproduction code should still fail because you're providing an argument to $b, which is a nullable string.

Meanwhile $a, a non-nullable string, gets nothing.

The above code should not fail, if Pest allowed named arguments, because it's providing a value to $a and nothing to $b.

@victorlap
Copy link
Author

victorlap commented Jan 26, 2025

In my example code, eventually the parameter is passed to 'a', since phpunit can't figure out at what position it should place the argument.

I first had asserts in there to test, but it the test fails it won't trigger a depreciation warning as well

Edit: I see the my example is missing the default value I had for the 'a' parameter, will add it back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants