Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use static data providers #48283

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

Closed
wants to merge 11 commits into from

Conversation

OskarStark
Copy link
Contributor

Q A
Branch? 4.4
Bug fix? no
New feature? no
Deprecations? no
Tickets Refs #48281
License MIT
Doc PR -

Follows

This is the first PR, starting against 4.4 branch.

I am not sure yet, if I catched all of them, I searched for provide*() and *Provider() methods in the tests directory.

@OskarStark OskarStark force-pushed the static-data-providers branch from 3c3933b to cbb8b64 Compare November 22, 2022 12:55
@GromNaN
Copy link
Member

GromNaN commented Nov 22, 2022

Did you use rector or some other tool to automate the change?
Edit: not available yet. See rectorphp/rector-phpunit#126

@OskarStark
Copy link
Contributor Author

Did you use rector or some other tool to automate the change?

No as I didn't found an already available rector yet. I asked @TomasVotruba via Slack, but no answer until now.

@IonBazan
Copy link
Contributor

IonBazan commented Nov 22, 2022

Unfortunately not all of them start with provide*. I've written this PHP script to find all method names referenced by @dataProvider annotation and make them static:

<?php

function rsearch($folder, $pattern)
{
    $iti = new RecursiveDirectoryIterator($folder);
    foreach(new RecursiveIteratorIterator($iti) as $file){
        if (str_ends_with($file, $pattern) && is_file($file)) {
            yield $file;
        }
    }

    return [];
}

$files = rsearch(__DIR__.'/src', '.php'); // not all tests end with "Test"

/** @var SplFileInfo $file */
foreach ($files as $file) {
    $contents = file_get_contents($file->getRealPath());
    if (preg_match_all('#\@dataProvider (\w+)#', $contents, $matches)) {
        foreach ($matches[1] as $dataProvider) {
            $contents = preg_replace("#public function $dataProvider\(#", "public static function $dataProvider(", $contents);
        }
        file_put_contents($file->getRealPath(), $contents);
    }
}

This produces tons of broken tests, as there are many abstract provider methods implemented by derived classes, which require manual fixing and refactoring. I tried to do that but it was too tedious. I suggest writing a migration tool or a Rector rule to handle this but I don't have enough experience with that.

I'm wondering if 4.4 should be the target here, as there will be no bug fixes provided by the end of this month and PHPUnit 10 will be most likely released next year so perhaps we could target 5.4. This would also reduce the number of conflicts when merging up.

@stof
Copy link
Member

stof commented Nov 22, 2022

I opened rectorphp/rector-phpunit#126 as a feature request for such a rector

@OskarStark
Copy link
Contributor Author

This produces tons of broken tests, as there are many abstract provider methods implemented by derived classes, which require manual fixing and refactoring. I tried to do that but it was too tedious. I suggest writing a migration tool or a Rector rule to handle this but I don't have enough experience with that.

Another problem is, that we have data providers using $this internally to call some helper methods, those need to be static, too.

I'm wondering if 4.4 should be the target here, as there will be no bug fixes provided by the end of this month and PHPUnit 10 will be most likely released next year so perhaps we could target 5.4. This would also reduce the number of conflicts when merging up.

Yes, I think most of the providers are also available in 5.4 so it doesn't really hurt 🤷‍♂️ But we can ignore 4.4 if it gets decided.

@stof
Copy link
Member

stof commented Nov 22, 2022

you should indeed target the branch. PHPUnit 10 is scheduled for February 2023, so Symfony 4.4 will be in security-only mode before this release.

@OskarStark
Copy link
Contributor Author

you should indeed target the branch.

5.4 ?

@stof
Copy link
Member

stof commented Nov 22, 2022

yeah, I meant to say "the 5.4 branch". Not sure where the number got stucked while writing...

@IonBazan
Copy link
Contributor

Another problem is, that we have data providers using $this internally to call some helper methods, those need to be static, too.

Yes, and it escalates quite fast as you try to make these helper methods static 💀. We should be able to refactor most of it though, but there are some providers using mocks ($this->createMock()) which seems like a dead end 🚧 to me.

@OskarStark
Copy link
Contributor Author

Ok before going forward, let's wait for feedback in the rector issue

@OskarStark
Copy link
Contributor Author

but there are some providers using mocks ($this->createMock()) which seems like a dead end 🚧 to me.

Yes

@stof
Copy link
Member

stof commented Nov 22, 2022

The tests using mocks in data providers will indeed need to be refactored manually (and they probably have a weird impact on PHPUnit). The suggested rector should be able to migrate all simple cases. Then, for remaining ones, a manual work will be needed.

@fabpot fabpot modified the milestones: 4.4, 5.4 Nov 22, 2022
@nicolas-grekas
Copy link
Member

Do we have a automated way to do this change? Eg php-cs-fixer?
I'd better close this PR and wait for the cs fixer if not.

@OskarStark
Copy link
Contributor Author

Lets close this, rector has already created a rule, but I think it is first working with PHPUnit 10, not sure

@OskarStark OskarStark closed this Dec 13, 2022
OskarStark added a commit to OskarStark/symfony that referenced this pull request Dec 13, 2022
Extracted from closed PR symfony#48283

This is work, which needs to be done manually anyway.
nicolas-grekas added a commit that referenced this pull request Dec 13, 2022
This PR was merged into the 5.4 branch.

Discussion
----------

Use static methods inside data providers

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | n/a
| License       | MIT
| Doc PR        | n/a

Extracted from closed PR #48283

This is work, which needs to be done manually anyway.

Commits
-------

90c8f73 Use static methods inside data providers
@stof
Copy link
Member

stof commented Dec 14, 2022

The rector rule is considered part of the rector ruleset about migrating to PHPUnit 10. But you can use it separately. PHPUnit has always supported static data providers AFAIK.

@OskarStark OskarStark deleted the static-data-providers branch June 23, 2025 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants