Skip to content

Run unit tests only for plugins with changes #2863

Run unit tests only for plugins with changes

Run unit tests only for plugins with changes #2863

name: Unit Testing for Plugins
on:
push:
branches:
- trunk
- 'release/**'
# Only run if PHP-related files changed.
paths:
- '.github/workflows/php-test-plugins.yml'
- 'plugins/**.php'
- '.wp-env.json'
- '**/package.json'
- 'package-lock.json'
- 'phpunit.xml.dist'
- 'composer.json'
- 'composer.lock'
pull_request:
# Only run if PHP-related files changed.
paths:
- '.github/workflows/php-test-plugins.yml'
- 'plugins/**.php'
- '.wp-env.json'
- '**/package.json'
- 'package-lock.json'
- 'phpunit.xml.dist'
- 'composer.json'
- 'composer.lock'
types:
- opened
- reopened
- synchronize
jobs:
php-test-plugins:
name: "PHP ${{ matrix.php }} / WP ${{ matrix.wp }}"
runs-on: ubuntu-latest
timeout-minutes: 20
strategy:
fail-fast: false
matrix:
php: ['8.1', '8.0', '7.4', '7.3', '7.2']
wp: [ 'latest' ]
coverage: [false]
include:
- php: '7.4'
wp: '6.6'
- php: '8.3'
wp: 'trunk'
- php: '8.2'
phpunit: '9.6'
wp: 'latest'
coverage: ${{ !startsWith(github.actor, 'dependabot') }}
env:
WP_ENV_PHP_VERSION: ${{ matrix.php }}
WP_ENV_CORE: ${{ matrix.wp == 'trunk' && 'WordPress/WordPress' || format( 'https://wordpress.org/wordpress-{0}.zip', matrix.wp ) }}
steps:
- uses: styfle/[email protected]
- uses: actions/checkout@v4
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v45
with:
dir_names: true # Output unique changed directories.
dir_names_max_depth: 2
files_yaml: |
plugins:
- 'plugins/**'
config:
- '.github/workflows/php-test-plugins.yml'
- '.wp-env.json'
- '**/package.json'
- 'package-lock.json'
- 'phpunit.xml.dist'
- 'composer.json'
- 'composer.lock'
- name: Get changed plugins
id: changed-plugins
run: |
if [[ "${{ github.event_name }}" == "push" || "${{ steps.changed-files.outputs.config_any_changed }}" == "true" ]]; then
ALL_CHANGED_PLUGINS=($(ls plugins))
echo "all_changed_plugins=${ALL_CHANGED_PLUGINS[*]}" >> $GITHUB_OUTPUT
exit 0
fi
declare -a ALL_CHANGED_PLUGINS=()
for DIR in ${{ steps.changed-files.outputs.plugins_all_changed_files }}; do
PLUGIN_NAME=$(basename "$DIR")
ALL_CHANGED_PLUGINS+=("$PLUGIN_NAME")
done
# Define and add plugin dependencies (e.g., optimization-detective triggers others).
declare -A PLUGIN_DEPENDENCIES=(
["optimization-detective"]="embed-optimizer image-prioritizer"
)
for PLUGIN in "${ALL_CHANGED_PLUGINS[@]}"; do
if [[ -n "${PLUGIN_DEPENDENCIES[$PLUGIN]}" ]]; then
for DEP in ${PLUGIN_DEPENDENCIES[$PLUGIN]}; do
if [[ ! " ${ALL_CHANGED_PLUGINS[@]} " =~ " ${DEP} " ]]; then
ALL_CHANGED_PLUGINS+=("$DEP")
fi
done
fi
done
ALL_CHANGED_PLUGINS=($(echo "${ALL_CHANGED_PLUGINS[@]}" | tr ' ' '\n' | sort | tr '\n' ' '))
echo "all_changed_plugins=${ALL_CHANGED_PLUGINS[*]}" >> $GITHUB_OUTPUT
- name: Setup Node.js (.nvmrc)
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: npm
- name: npm install
run: npm ci
- name: Build assets
run: npm run build
- name: Install WordPress
run: |
if [ "${{ matrix.coverage }}" == "true" ]; then
npm run wp-env start -- --xdebug=coverage
else
npm run wp-env start
fi
- name: Composer Install
run: npm run wp-env run tests-cli -- --env-cwd="wp-content/plugins/$(basename $(pwd))" composer install --no-interaction --no-progress
- name: Update Composer Dependencies
run: composer update --with-all-dependencies --no-interaction --no-progress
- name: Install PHPUnit
run: |
if [ "${{ matrix.php }}" == "8.2" ]; then
composer require phpunit/phpunit:${{ matrix.phpunit }} --with-all-dependencies --ignore-platform-reqs
else
composer require --dev phpunit/phpunit:${{ matrix.phpunit }}
fi
if: matrix.phpunit != ''
- name: Running single site unit tests
run: |
if [ "${{ matrix.coverage }}" == "true" ]; then
for PLUGIN in ${{ steps.changed-plugins.outputs.all_changed_plugins }}; do
npm run test-php:$PLUGIN -- -- -- --coverage-clover=./single-site-reports/coverage-$PLUGIN.xml
done
else
for PLUGIN in ${{ steps.changed-plugins.outputs.all_changed_plugins }}; do
npm run test-php:$PLUGIN
done
fi
- name: Running multisite unit tests
run: |
if [ "${{ matrix.coverage }}" == "true" ]; then
for PLUGIN in ${{ steps.changed-plugins.outputs.all_changed_plugins }}; do
npm run test-php-multisite:$PLUGIN -- -- -- --coverage-clover=./multisite-reports/coverage-multisite-$PLUGIN.xml
done
else
for PLUGIN in ${{ steps.changed-plugins.outputs.all_changed_plugins }}; do
npm run test-php-multisite:$PLUGIN
done
fi
- name: Upload single site coverage reports to Codecov
if: ${{ matrix.coverage == true }}
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./single-site-reports
flags: single
name: ${{ matrix.php }}-single-site-coverage
fail_ci_if_error: true
- name: Upload multisite coverage reports to Codecov
if: ${{ matrix.coverage == true }}
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./multisite-reports
flags: multisite
name: ${{ matrix.php }}-multisite-coverage
fail_ci_if_error: true