From 885354e8f368d37dcc1c6bd88075258f0ef7953e Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 4 Apr 2020 16:52:17 +0200 Subject: [PATCH] Tests: add bootstrap file for PHPUnit As, as of YoastCS 3.0, more external standards will be used, manually maintaining the `PHPCS_IGNORE_TESTS` lists becomes cumbersome. This commit adds a bootstrap file which will: * Load the PHPCS autoloader (was previously done from the command-line); * Will automatically set the `PHPCS_IGNORE_TESTS` environment variable to the correct value to prevent running tests from other standards. --- .github/workflows/quicktest.yml | 4 +- .github/workflows/test.yml | 4 +- composer.json | 4 +- phpunit-bootstrap.php | 83 +++++++++++++++++++++++++++++++++ phpunit.xml.dist | 5 +- 5 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 phpunit-bootstrap.php diff --git a/.github/workflows/quicktest.yml b/.github/workflows/quicktest.yml index 3c316d32..7ba036fd 100644 --- a/.github/workflows/quicktest.yml +++ b/.github/workflows/quicktest.yml @@ -100,6 +100,4 @@ jobs: - name: Run the unit tests - PHP 8.1+ if: ${{ matrix.php_version >= '8.1' || matrix.php_version == 'latest'}} - run: composer test -- --no-configuration --dont-report-useless-tests - env: - PHPCS_IGNORE_TESTS: 'PHPCompatibility,WordPress' + run: composer test -- --no-configuration --bootstrap=./phpunit-bootstrap.php --dont-report-useless-tests diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e1f7a315..700d0788 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -155,9 +155,7 @@ jobs: - name: Run the unit tests - PHP 8.1+ if: ${{ matrix.php_version >= '8.1' }} - run: composer test -- --no-configuration --dont-report-useless-tests - env: - PHPCS_IGNORE_TESTS: 'PHPCompatibility,WordPress' + run: composer test -- --no-configuration --bootstrap=./phpunit-bootstrap.php --dont-report-useless-tests #### CODE COVERAGE STAGE #### # N.B.: Coverage is only checked on the lowest and highest stable PHP versions diff --git a/composer.json b/composer.json index 6b017e6b..829fff5f 100644 --- a/composer.json +++ b/composer.json @@ -55,10 +55,10 @@ "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf" ], "test": [ - "@php ./vendor/phpunit/phpunit/phpunit --filter Yoast --bootstrap=\"./vendor/squizlabs/php_codesniffer/tests/bootstrap.php\" ./vendor/squizlabs/php_codesniffer/tests/AllTests.php --no-coverage" + "@php ./vendor/phpunit/phpunit/phpunit --filter Yoast ./vendor/squizlabs/php_codesniffer/tests/AllTests.php --no-coverage" ], "coverage": [ - "@php ./vendor/phpunit/phpunit/phpunit --filter Yoast --bootstrap=\"./vendor/squizlabs/php_codesniffer/tests/bootstrap.php\" ./vendor/squizlabs/php_codesniffer/tests/AllTests.php" + "@php ./vendor/phpunit/phpunit/phpunit --filter Yoast ./vendor/squizlabs/php_codesniffer/tests/AllTests.php" ], "check-complete": [ "@php ./vendor/phpcsstandards/phpcsdevtools/bin/phpcs-check-feature-completeness ./Yoast" diff --git a/phpunit-bootstrap.php b/phpunit-bootstrap.php new file mode 100644 index 00000000..e6a4ecd0 --- /dev/null +++ b/phpunit-bootstrap.php @@ -0,0 +1,83 @@ + true, +]; + +$all_standards = Standards::getInstalledStandards(); +$all_standards[] = 'Generic'; + +$standards_to_ignore = []; +foreach ( $all_standards as $standard ) { + if ( isset( $yoast_standards[ $standard ] ) === true ) { + continue; + } + + $standards_to_ignore[] = $standard; +} + +$standards_to_ignore_string = \implode( ',', $standards_to_ignore ); + +// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_putenv -- This is not production, but test code. +\putenv( "PHPCS_IGNORE_TESTS={$standards_to_ignore_string}" ); + +// Clean up. +unset( $phpcs_dir, $composer_phpcs_path, $all_standards, $standards_to_ignore, $standard, $standards_to_ignore_string ); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b8e34b92..a1962b29 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,6 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd" backupGlobals="true" + bootstrap="./phpunit-bootstrap.php" beStrictAboutTestsThatDoNotTestAnything="false" colors="true" convertErrorsToExceptions="true" @@ -29,8 +30,4 @@ - - - -