PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Incompatibility with PhpUnit 9.5.10

Open Koen1999 opened this issue 4 years ago • 7 comments

Describe the bug PHP Fatal error: Declaration of PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in /home/user/laminas-ldap/vendor/squizlabs/php_codesniffer/tests/Standards/AbstractSniffUnitTest.php on line 55

Custom ruleset https://github.com/laminas/laminas-coding-standard/blob/2.3.0/src/LaminasCodingStandard/ruleset.xml

To reproduce Steps to reproduce the behavior:

  1. php7.4 vendor/bin/phpcs --standard=LaminasCodingStandard --colors -p --extensions=php .

PHPCS output here PHP Fatal error: Declaration of PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in /home/user/laminas-ldap/vendor/squizlabs/php_codesniffer/tests/Standards/AbstractSniffUnitTest.php on line 55

Versions (please complete the following information):

  • OS: Ubuntu 21.10
  • PHP: 7.4, 8.0, 8.1
  • PHPCS: 3.6.1
  • Standard: LaminasCodingStandard

Additional context

phpunit/phpunit                                9.5.10  The PHP Unit Testing framework.
squizlabs/php_codesniffer                      3.6.1   PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.
slevomat/coding-standard                       7.0.16  Slevomat Coding Standard for PHP_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.
laminas/laminas-coding-standard                2.3.0   Laminas Coding Standard

Koen1999 avatar Dec 01 '21 13:12 Koen1999

This is related to the fact that you are clearly using the PHPCS native test framework to run the sniff tests for an external standard.

It is, of course, perfectly fine to do so and quite common practice, but if you do, you need to abide by the PHPUnit version constraints as set by PHPCS, which are currently "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0".

As for getting things working:

  • The tests will run fine on PHP 7.4 using PHPUnit 7.5.20.
  • For PHP 8.0 (and higher), you need to make sure you are using a Composer installed version of PHPUnit 7.5.20. The PHPUnit 7.5.20 PHAR will not work due to it containing outdated dependencies not compatible with PHP 8.0.
  • For PHP 8.1, again, using PHPUnit 7.5.20, you need to use the CLI --no-configuration option and pass any arguments which are normally stored in a phpunit.xml file via the command-line.

Also related to #3395

jrfnl avatar Dec 01 '21 14:12 jrfnl

@jrfnl Thanks for your comment! As for now it then seems that I cannot support both unit tests and PhpCB using PHP 8.1 given compatibility issues with PHP 8.1 and PhpUnit 7.5.* I appreciate your effort

Koen1999 avatar Dec 01 '21 15:12 Koen1999

@Koen1999 What do you mean with PhpCB ?

jrfnl avatar Dec 01 '21 15:12 jrfnl

@Koen1999 What do you mean with PhpCB ?

That's a typo, I meant to say PhpCS. This project, the code sniffer.

Koen1999 avatar Dec 01 '21 16:12 Koen1999

Ah, I understand, but the thing is: you can. See my pointers above on how.

jrfnl avatar Dec 01 '21 17:12 jrfnl

Ah, I understand, but the thing is: you can. See my pointers above on how.

Your pointers suggest using PHPUnit 7, but this does not work with certain features of PHP 8.1 when running tests. (sadly, I did not store the example for reference).

https://github.com/sebastianbergmann/phpunit/blob/master/ChangeLog-9.5.md

According to the changelog, you require PhpUnit ^9.5.8 when using PHP 8.1.

Koen1999 avatar Dec 01 '21 18:12 Koen1999

@Koen1999 Running tests based off the PHPCS test framework will work fine on PHP 8.1 with PHPUnit 7.5 as described above. Seriously, PHPCS itself does so and I'm doing it for a number of external standards too.

The only thing which probably won't work well is running code-coverage. In most cases, you'd only run code coverage for a select number of builds anyway, not all builds, so just don't use your PHP 8.1 builds for your code coverage runs.

jrfnl avatar Dec 01 '21 18:12 jrfnl