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

docs on data providers don't say provided values can't be anonymous functions #361

Open
joachim-n opened this issue Jul 2, 2024 · 2 comments

Comments

@joachim-n
Copy link
Contributor

Trying to use an anonymous function as a value in a data provider crashes phpunit.

The docs should say what the limitations are on possible data provider values.

@sebastianbergmann
Copy link
Owner

It never occurred to me that one would use anonymous functions as "values" in data providers. That being said, AFAICS it seems to work:

<?php declare(strict_types=1);
namespace PHPUnit\TestFixture;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class DataProviderAnonymousFunctionTest extends TestCase
{
    public static function provider(): array
    {
        return [
            [function () { return true; }],
        ];
    }

    #[DataProvider('provider')]
    public function testOne(callable $a): void
    {
        $this->assertIsCallable($a);
        $this->assertTrue($a());
    }
}

@joachim-n
Copy link
Contributor Author

I get:

Message: Serialization of 'Closure' is not allowed
Location: /var/www/html/vendor/phpunit/phpunit/src/Framework/TestRunner.php:306

But that's on PHPUnit 10.5.20.

Good to know this is supported in 11.

The use case was tests that do nearly everything the same except a few additional steps of setup -- details here https://www.drupal.org/project/drupal/issues/3278083#comment-15666084

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

No branches or pull requests

2 participants