Skip to content

Commit

Permalink
Merge pull request #310 from Yoast/JRF/tests-add-bootstrap-file
Browse files Browse the repository at this point in the history
Tests: add bootstrap file for PHPUnit
  • Loading branch information
jrfnl authored Sep 26, 2023
2 parents 0e33b6b + 885354e commit 8a692fa
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 12 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/quicktest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
83 changes: 83 additions & 0 deletions phpunit-bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* YoastCS: Bootstrap file for running the tests.
*
* - Load the PHPCS PHPUnit bootstrap file providing cross-version PHPUnit support.
* {@link https://github.com/squizlabs/PHP_CodeSniffer/pull/1384}
* - Load the Composer autoload file.
* - Automatically limit the testing to the YoastCS tests.
*
* @package Yoast\YoastCS
* @since 3.0.0
*/

use PHP_CodeSniffer\Util\Standards;

if ( \defined( 'PHP_CODESNIFFER_IN_TESTS' ) === false ) {
\define( 'PHP_CODESNIFFER_IN_TESTS', true );
}

/*
* Load the necessary PHPCS files.
*/
// Get the PHPCS dir from an environment variable.
$phpcs_dir = \getenv( 'PHPCS_DIR' );
$composer_phpcs_path = __DIR__ . '/vendor/squizlabs/php_codesniffer';

if ( $phpcs_dir === false && \is_dir( $composer_phpcs_path ) ) {
// PHPCS installed via Composer.
$phpcs_dir = $composer_phpcs_path;
}
elseif ( $phpcs_dir !== false ) {
/*
* PHPCS in a custom directory.
* For this to work, the `PHPCS_DIR` needs to be set in a custom `phpunit.xml` file.
*/
$phpcs_dir = \realpath( $phpcs_dir );
}

// Try and load the PHPCS autoloader.
if ( $phpcs_dir !== false
&& \file_exists( $phpcs_dir . '/autoload.php' )
&& \file_exists( $phpcs_dir . '/tests/bootstrap.php' )
) {
require_once $phpcs_dir . '/autoload.php';
require_once $phpcs_dir . '/tests/bootstrap.php'; // PHPUnit 6.x+ support.
}
else {
echo 'Uh oh... can\'t find PHPCS.
If you use Composer, please run `composer install`.
Otherwise, make sure you set a `PHPCS_DIR` environment variable in your phpunit.xml file
pointing to the PHPCS directory.
';

die( 1 );
}

/*
* Set the PHPCS_IGNORE_TEST environment variable to ignore tests from other standards.
*/
$yoast_standards = [
'Yoast' => 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 );
5 changes: 1 addition & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -29,8 +30,4 @@
<log type="coverage-text" target="php://stdout" showOnlySummary="true"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>

<php>
<env name="PHPCS_IGNORE_TESTS" value="PHPCompatibility,WordPress"/>
</php>
</phpunit>

0 comments on commit 8a692fa

Please sign in to comment.