Yoast / PHPUnit-Polyfills

Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests
BSD 3-Clause "New" or "Revised" License
173 stars 13 forks source link

Receiving error using autoload/classmap - AssertFileEqualsSpecializations::assertFileEqualsCanonicalizing #153

Closed mszkb closed 9 months ago

mszkb commented 9 months ago

This issue occured when I wanted to make run Wordpress plugin Integrationtests. I used following tutorials: https://make.wordpress.org/cli/handbook/misc/plugin-unit-tests/ and https://sarathlal.com/unit-testing-wordpress-plugin-using-phpunit/

Following those tutorials, running phpunit requires me to install yoast/phpunit-polyfills. Doing so gives me biiig warnings during installing phase. Afterwards when I want to run phpunit ./bin/vendor/phpunit -c phpunit.xml.dist it gives me some weird errors:

[05-Feb-2024 21:48:16 UTC] PHP Fatal error:  Declaration of Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations::assertFileEqualsCanonicalizing($expected, $actual, $message = '') must be compatible with PHPUnit\Framework\Assert::assertFileEqualsCanonicalizing(string $expected, string $actual, string $message = ''): void in /var/www/html/wp-content/plugins/voting/vendor/yoast/phpunit-polyfills/src/TestCases/TestCasePHPUnitGte8.php on line 31

Fatal error: Declaration of Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations::assertFileEqualsCanonicalizing($expected, $actual, $message = '') must be compatible with PHPUnit\Framework\Assert::assertFileEqualsCanonicalizing(string $expected, string $actual, string $message = ''): void in /var/www/html/wp-content/plugins/voting/vendor/yoast/phpunit-polyfills/src/TestCases/TestCasePHPUnitGte8.php on line 31

It turns out, I had to exclude the yoast dependency from the classmapping of composer. Then everything works fine. Like this:

{
    "autoload": {
        "classmap": [
            "vendor/",
            "src/"
        ],
        "exclude-from-classmap": ["vendor/yoast"]
    },
    "require-dev": {
        "phpunit/phpunit": "^8",
        "10up/wp_mock": "*",
      "yoast/phpunit-polyfills": "^2.0"
    }
}

Doing composer update it does not show significant warnings and finally ./bin/vendor/phpunit -c phpunit.xml.dist does work.

Maybe you can address this issue by the next release. Right now it works with the exclusion. I hope this helps somebody.

jrfnl commented 9 months ago

@mszkb Sorry you are experiencing problems, but this is not an issue with the PHPUnit Polyfills. but user error.

You should never include vendor in your classmap and most definitely not in an autoload directive. Each dependency defines their own autoload requirements and Composer puts those all together resulting in a vendor/autoload.php file (with sub-files) which handles it all. This file will differ depending on whether the install was run with --no-dev or without.

The autoload directive in your composer.json should only include your own files. Additionally, anything non-runtime related (like tests), should be handled via a separate autoload-dev directive.

As side-notes:

All in all, I don't know those tutorials, but you may want to educate yourself about the use of Composer a little more. I'd suggest starting with the Composer documentation: https://getcomposer.org/doc/