Skip to content

Commit

Permalink
Run PHPUnit jobs across multiple PHP versions (#46510)
Browse files Browse the repository at this point in the history
1. Run PHPUnit tests on all PHP versions (5.6 through 8.2) using the CI jobs.
2. Shows code style errors on the "Files changed" tab (same as Core);
3. Fix an issue with wp-env as it used non-optimal PHPUnit versions on PHP 7.3 and 7.4;

Props @jrfnl , @dmsnell , @anton-vlasenko
  • Loading branch information
anton-vlasenko authored Jan 16, 2023
1 parent 20b9ba0 commit 9ee17a7
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 20 deletions.
Empty file added .cache/.gitkeep
Empty file.
162 changes: 152 additions & 10 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ on:
- trunk
- 'release/**'
- 'wp/**'
# Allow manually triggering the workflow.
workflow_dispatch:

# Cancels all previous workflow runs for pull requests that have not completed.
concurrency:
Expand Down Expand Up @@ -51,39 +53,179 @@ jobs:
- name: Running the date tests
run: npm run test:unit:date -- --ci --maxWorkers=2 --cacheDirectory="$HOME/.jest-cache"

unit-php:
name: PHP
test-php:
name: PHP ${{ matrix.php }}${{ matrix.multisite && ' multisite' || '' }} on ubuntu-latest
runs-on: ubuntu-latest
timeout-minutes: 20
if: ${{ github.repository == 'WordPress/gutenberg' || github.event_name == 'pull_request' }}
strategy:
fail-fast: true
matrix:
php:
- '5.6'
- '7.0'
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
multisite: [false, true]

env:
WP_ENV_PHP_VERSION: ${{ matrix.php }}

steps:
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0

- name: Use desired version of NodeJS
- name: Set up Node.js
uses: actions/setup-node@8c91899e586c5b171469028077307d293428b516 # v3.5.1
with:
node-version-file: '.nvmrc'
cache: npm

- name: Npm install and build
##
# This allows Composer dependencies to be installed using a single step.
#
# Since the tests are currently run within the Docker containers where the PHP version varies,
# the same PHP version needs to be configured for the action runner machine so that the correct
# dependency versions are installed and cached.
##
- name: Set up PHP
uses: shivammathur/setup-php@1a18b2267f80291a81ca1d33e7c851fe09e7dfc4 # v2.22.0
with:
php-version: '${{ matrix.php }}'
ini-file: development
coverage: none

# Ensure that Composer installs the correct versions of packages.
- name: Override PHP version in composer.json
run: composer config platform.php ${{ matrix.php }}

# The spatie/phpunit-watcher package is not compatible with PHP < 7.2.
# It must be removed before running the tests.
- name: Remove incompatible Composer packages
if: ${{ matrix.php < '7.2' }}
run: composer remove spatie/phpunit-watcher --dev --no-update

# Since Composer dependencies are installed using `composer update` and no lock file is in version control,
# passing a custom cache suffix ensures that the cache is flushed at least once per week.
- name: Install Composer dependencies
uses: ramsey/composer-install@83af392bf5f031813d25e6fe4cd626cdba9a2df6 # v2.2.0
with:
custom-cache-suffix: $(/bin/date -u --date='last Mon' "+%F")

- name: Install npm dependencies
run: |
npm ci
npm run build
- name: Install WordPress
- name: Docker debug information
run: |
npm run wp-env start
docker -v
docker-compose -v
- name: Running lint check
run: npm run lint:php
- name: General debug information
run: |
npm --version
node --version
curl --version
git --version
svn --version
locale -a
- name: Start Docker environment
run: npm run wp-env start

- name: Log running Docker containers
run: docker ps -a

- name: Docker container debug information
run: |
npm run wp-env run tests-mysql "mysql --version"
npm run wp-env run tests-wordpress "php --version"
npm run wp-env run tests-wordpress "php -m"
npm run wp-env run tests-wordpress "php -i"
npm run wp-env run tests-wordpress "/var/www/html/wp-content/plugins/gutenberg/vendor/bin/phpunit --version"
npm run wp-env run tests-wordpress "locale -a"
- name: Running single site unit tests
if: ${{ ! matrix.multisite }}
run: npm run test:unit:php
if: ${{ success() || failure() }}

- name: Running multisite unit tests
if: ${{ matrix.multisite }}
run: npm run test:unit:php:multisite
if: ${{ success() || failure() }}

phpcs:
name: PHP coding standards
runs-on: ubuntu-latest
timeout-minutes: 20
if: ${{ github.repository == 'WordPress/gutenberg' || github.event_name == 'pull_request' }}

steps:
- name: Checkout repository
uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0

- name: Set up PHP
uses: shivammathur/setup-php@1a18b2267f80291a81ca1d33e7c851fe09e7dfc4 # v2.22.0
with:
php-version: '7.4'
coverage: none
tools: cs2pr

# This date is used to ensure that the PHPCS cache is cleared at least once every week.
# http://man7.org/linux/man-pages/man1/date.1.html
- name: "Get last Monday's date"
id: get-date
run: echo "date=$(/bin/date -u --date='last Mon' "+%F")" >> $GITHUB_OUTPUT

- name: Cache PHPCS scan cache
uses: actions/cache@9b0c1fce7a93df8e3bb8926b0d6e9d89e92f20a7 # v3.0.11
with:
path: .cache/phpcs.json
key: ${{ runner.os }}-date-${{ steps.get-date.outputs.date }}-phpcs-cache-${{ hashFiles('**/composer.json', 'phpcs.xml.dist') }}

# Since Composer dependencies are installed using `composer update` and no lock file is in version control,
# passing a custom cache suffix ensures that the cache is flushed at least once per week.
- name: Install Composer dependencies
uses: ramsey/composer-install@83af392bf5f031813d25e6fe4cd626cdba9a2df6 # v2.2.0
with:
custom-cache-suffix: ${{ steps.get-date.outputs.date }}

- name: Make Composer packages available globally
run: echo "${PWD}/vendor/bin" >> $GITHUB_PATH

- name: Run PHPCS on all Gutenberg files
id: phpcs-gutenberg
run: phpcs --report-full --report-checkstyle=./.cache/phpcs-report.xml

- name: Show PHPCS results in PR
if: ${{ always() && steps.phpcs-gutenberg.outcome == 'failure' }}
run: cs2pr ./.cache/phpcs-report.xml

- name: Ensure version-controlled files are not modified during the tests
run: git diff --exit-code

# This job is deprecated but be present for compatibility reasons.
unit-php:
name: PHP
runs-on: ubuntu-latest
needs: [test-php, phpcs]
if: ${{ always() }}
steps:
- name: Fail the job if the PHPUnit tests fail
if: ${{ needs.test-php.result != 'success' }}
run: exit 1

- name: "Fail the job if the code doesn't conform to the coding standards"
if: ${{ needs.phpcs.result != 'success' }}
run: exit 1

- name: Mark the job as passed if all the checks pass
if: ${{ needs.test-php.result == 'success' && needs.phpcs.result == 'success' }}
run: exit 0

mobile-unit-js:
name: Mobile
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ yarn.lock
/perf-envs
/composer.lock

# The /.cache folder is needed for phpcs to cache results between runs, while other .cache folders must be ignored
# It is not possible to re-include a file if a parent directory of that file is excluded
# So, both /.cache and /.cache./.gitkeep must be re-included
.cache
!/.cache/
/.cache/**
!/.cache/.gitkeep

.eslintcache
*.tsbuildinfo

Expand Down
6 changes: 6 additions & 0 deletions packages/env/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

### Bug fix
- PHP 7.3 and 7.4 must use PHPUnit 9.

### Enhancement
- It's now possible to run PHPUnit tests on PHP 8.1 and 8.2.

## 5.9.0 (2023-01-02)

## 5.8.0 (2022-12-14)
Expand Down
6 changes: 4 additions & 2 deletions packages/env/lib/build-docker-compose-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,11 @@ module.exports = function buildDockerComposeConfig( config ) {
phpunitTag = '6' + phpunitPhpVersion;
} else if ( testsPhpVersion === '7.1' ) {
phpunitTag = '7' + phpunitPhpVersion;
} else if ( [ '7.2', '7.3', '7.4' ].indexOf( testsPhpVersion ) >= 0 ) {
} else if ( testsPhpVersion === '7.2' ) {
phpunitTag = '8' + phpunitPhpVersion;
} else if ( testsPhpVersion === '8.0' ) {
} else if (
[ '7.3', '7.4', '8.0', '8.1', '8.2' ].indexOf( testsPhpVersion ) >= 0
) {
phpunitTag = '9' + phpunitPhpVersion;
}
const phpunitImage = `wordpressdevelop/phpunit:${ phpunitTag }`;
Expand Down
3 changes: 3 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<arg value="ps"/>
<arg name="extensions" value="php"/>

<!-- Cache the scan results and re-use those for unchanged files on the next scan. -->
<arg name="cache" value=".cache/phpcs.json"/>

<file>./bin</file>
<file>./gutenberg.php</file>
<file>./lib</file>
Expand Down
15 changes: 15 additions & 0 deletions phpunit/block-supports/layout-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
*/

class WP_Block_Supports_Layout_Test extends WP_UnitTestCase {
/**
* @var string|null
*/
private $theme_root;

/**
* @var array|null
*/
private $orig_theme_dir;

/**
* @var array|null
*/
private $queries;

public function set_up() {
parent::set_up();
$this->theme_root = realpath( __DIR__ . '/../data/themedir1' );
Expand Down
27 changes: 19 additions & 8 deletions phpunit/class-wp-rest-block-pattern-categories-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class WP_REST_Block_Pattern_Categories_Controller_Test extends WP_Test_REST_Controller_Testcase {
protected static $admin_id;
protected static $orig_registry;
protected static $original_instance_value;

public function set_up() {
parent::set_up();
Expand All @@ -26,11 +26,13 @@ public static function wpSetupBeforeClass( $factory ) {
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );

// Setup an empty testing instance of `WP_Block_Pattern_Categories_Registry` and save the original.
$reflection = new ReflectionClass( 'WP_Block_Pattern_Categories_Registry' );
$reflection->getProperty( 'instance' )->setAccessible( true );
self::$orig_registry = $reflection->getStaticPropertyValue( 'instance' );
$test_registry = new WP_Block_Pattern_Categories_Registry();
$reflection->setStaticPropertyValue( 'instance', $test_registry );
$reflection = new ReflectionClass( 'WP_Block_Pattern_Categories_Registry' );
$instance_property = $reflection->getProperty( 'instance' );
$instance_property->setAccessible( true );
self::$original_instance_value = $instance_property->getValue( null );

$test_registry = new WP_Block_Pattern_Categories_Registry();
$instance_property->setValue( $test_registry );

// Register some categories in the test registry.
$test_registry->register( 'test', array( 'label' => 'Test' ) );
Expand All @@ -42,8 +44,11 @@ public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );

// Restore the original registry instance.
$reflection = new ReflectionClass( 'WP_Block_Pattern_Categories_Registry' );
$reflection->setStaticPropertyValue( 'instance', self::$orig_registry );
$reflection = new ReflectionClass( 'WP_Block_Pattern_Categories_Registry' );
$instance_property = $reflection->getProperty( 'instance' );
$instance_property->setAccessible( true );
$instance_property->setValue( self::$original_instance_value );
$instance_property->setAccessible( false );
}

public function test_register_routes() {
Expand Down Expand Up @@ -79,21 +84,27 @@ public function test_get_items() {
public function test_context_param() {
$this->markTestIncomplete();
}

public function test_get_item() {
$this->markTestIncomplete();
}

public function test_create_item() {
$this->markTestIncomplete();
}

public function test_update_item() {
$this->markTestIncomplete();
}

public function test_delete_item() {
$this->markTestIncomplete();
}

public function test_prepare_item() {
$this->markTestIncomplete();
}

public function test_get_item_schema() {
$this->markTestIncomplete();
}
Expand Down
15 changes: 15 additions & 0 deletions phpunit/class-wp-theme-json-resolver-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ class WP_Theme_JSON_Resolver_Gutenberg_Test extends WP_UnitTestCase {
*/
private static $property_core_orig_value;

/**
* @var string|null
*/
private $theme_root;

/**
* @var array|null
*/
private $orig_theme_dir;

/**
* @var array|null
*/
private $queries;

public static function set_up_before_class() {
parent::set_up_before_class();

Expand Down
14 changes: 14 additions & 0 deletions phpunit/wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
*/

class WP_Theme_Json_Test extends WP_UnitTestCase {
/**
* @var string|null
*/
private $theme_root;

/**
* @var array|null
*/
private $orig_theme_dir;

/**
* @var array|null
*/
private $queries;

public function set_up() {
parent::set_up();
Expand Down

1 comment on commit 9ee17a7

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 9ee17a7.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3930531251
📝 Reported issues:

Please sign in to comment.