Skip to content

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Apr 15, 2025

Description

As things were, the test framework used a setup with custom test suites, which was based on PHPUnit 2/3 and only still worked by accident, not by design. This type of setup was deprecated in PHPUnit 4 and support has been removed completely in PHPUnit 10.

This commit updates the test framework to start using the test suite setup via the PHPUnit config file instead and removes all classes related to the custom test suite setup.

Notes:

  • As all test suites are now declared in the PHPUnit config file, the tests can now be run by just calling phpunit.
    No need to explicitly pass the tests/AllTests.php file anymore.
  • As part of the test suite setup, all files needed by the tests were preloaded and the autoloader was removed once the test suites had been set up. This looks to have been designed like this originally to allow for the tests to be run in a PEAR setup.
    Various global variables were used in this process and referred to in the test cases.
    Those global variables are now no longer declared and all uses of these have been removed.
  • The aforementioned preloading previously could cause issues when running tests for external standards and/or when running the tests for PHPCS itself when various external standards were registered in installed_paths.
    To work around this, PR Play nice with sniff libraries with alternative unit test setups. squizlabs/PHP_CodeSniffer#1146 introduced a PHPCS_IGNORE_TESTS environment variable to explicitly ignore other standards than the one(s) being tested.
    With the changes now made, it is no longer needed to set this environment variable (once a test suite for an external standard has been made compatible with the PHPCS 4.0 test framework).
  • As there is now no place from which to call the printPHPCodeSnifferTestOutput() method anymore, the method has been removed and the "# sniff test files generated # unique error codes; # were fixable (#%)" summary line will no longer be printed at the end of a test run.
    A number of global variables were declared and written to by the tests to allow for that output previously.
    Those global variables are now no longer declared and assignments to these has been removed.
  • The previously used global variables containing an instance of the Config class and an array of instances of the Ruleset class have also been removed. This information is now stored in private static properties in the AbstractSniffUnitTest class.
  • To ensure autoloading of the tests and other files also works for tests for external standards using the PHPCS native test framework and loading the PHPCS native test bootstrap from their test bootstrap, all standards registered in the applicable CodeSniffer.conf file will be registered with the autoloader.

Information on how to upgrade test suites for external standards which are based on the PHPCS native test framework will be included in the PHPCS 4.0 upgrade guide.

Refs:

Suggested changelog entry

Changed:
The test framework has been refactored and no longer creates a custom test suite.
- If tests for an external standard extend the PHPCS native test suite, be sure to read the upgrade guide for more detail.

Related issues/external references

Fixes #25

As things were, the test framework used a setup with custom test suites, which was based on PHPUnit 2/3 and only still worked by accident, not by design. This type of setup was deprecated in PHPUnit 4 and support has been removed completely in PHPUnit 10.

This commit updates the test framework to start using the test suite setup via the PHPUnit config file instead and removes all classes related to the custom test suite setup.

Notes:
* As all test suites are now declared in the PHPUnit config file, the tests can now be run by just calling `phpunit`.
    No need to explicitly pass the `tests/AllTests.php` file anymore.
* As part of the test suite setup, all files needed by the tests were preloaded and the autoloader was removed once the test suites had been set up. This looks to have been designed like this originally to allow for the tests to be run in a PEAR setup.
    Various global variables were used in this process and referred to in the test cases.
    Those global variables are now no longer declared and all uses of these have been removed.
* The aforementioned preloading previously could cause issues when running tests for external standards and/or when running the tests for PHPCS itself when various external standards were registered in `installed_paths`.
    To work around this, PR squizlabs/PHP_CodeSniffer 1146 introduced a `PHPCS_IGNORE_TESTS` environment variable to explicitly ignore other standards than the one(s) being tested.
    With the changes now made, it is no longer needed to set this environment variable (once a test suite for an external standard has been made compatible with the PHPCS 4.0 test framework).
* As there is now no place from which to call the `printPHPCodeSnifferTestOutput()` method anymore, the method has been removed and the "# sniff test files generated # unique error codes; # were fixable (#%)" summary line will no longer be printed at the end of a test run.
    A number of global variables were declared and written to by the tests to allow for that output previously.
    Those global variables are now no longer declared and assignments to these has been removed.
* The previously used global variables containing an instance of the `Config` class and an array of instances of the `Ruleset` class have also been removed. This information is now stored in `private static` properties in the `AbstractSniffUnitTest` class.
* To ensure autoloading of the tests and other files also works for tests for external standards using the PHPCS native test framework and loading the PHPCS native test bootstrap from _their_ test bootstrap, all standards registered in the applicable `CodeSniffer.conf` file will be registered with the autoloader.

Information on how to upgrade test suites for external standards which are based on the PHPCS native test framework will be included in the PHPCS 4.0 upgrade guide.

Fixes 25

Refs:
* sebastianbergmann/phpunit 3213
* sebastianbergmann/phpunit 4460
@jrfnl jrfnl merged commit 7bc2ff3 into 4.x Apr 15, 2025
54 checks passed
@jrfnl jrfnl deleted the phpcs-4.0/feature/25-refactor-testsuite branch April 15, 2025 12:33
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.

3 participants