Skip to content

Commit 8fbfa2b

Browse files
Merge pull request #10 from peterwilsoncc/better-tests
Improve PHP Unit Tests.
2 parents a8cb129 + e2214c3 commit 8fbfa2b

4 files changed

Lines changed: 133 additions & 45 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Reusable PHPUnit Tests
2+
3+
env:
4+
COMPOSER_VERSION: "2"
5+
COMPOSER_CACHE: "${{ github.workspace }}/.composer-cache"
6+
7+
on:
8+
workflow_call:
9+
inputs:
10+
wp-version:
11+
description: WordPress version to test
12+
required: true
13+
type: string
14+
php-version:
15+
description: PHP version to test
16+
required: true
17+
type: string
18+
19+
jobs:
20+
phpunit:
21+
name: WP ${{ inputs.wp-version }} / PHP ${{ inputs.php-version }}
22+
runs-on: ubuntu-latest
23+
env:
24+
DB_DATABASE: wordpress_test
25+
DB_USER: root
26+
DB_PASSWORD: root
27+
steps:
28+
- name: Checkout
29+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
30+
31+
- name: Set standard Composer cache directories
32+
run: |
33+
composer config -g cache-dir "${{ env.COMPOSER_CACHE }}"
34+
35+
- name: Prepare composer cache
36+
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
37+
with:
38+
path: ${{ env.COMPOSER_CACHE }}
39+
key: composer-${{ env.COMPOSER_VERSION }}-${{ hashFiles('**/composer.lock') }}
40+
restore-keys: |
41+
composer-${{ env.COMPOSER_VERSION }}-
42+
43+
- name: Set PHP version
44+
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # v2.37.2
45+
with:
46+
php-version: ${{ inputs.php-version }}
47+
coverage: none
48+
tools: phpunit-polyfills, composer:v2
49+
50+
- name: Install dependencies
51+
run: composer update -W
52+
53+
- name: Install WordPress
54+
run: |
55+
sudo /etc/init.d/mysql start
56+
bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 ${{ inputs.wp-version }}
57+
58+
- name: PHPUnit
59+
run: './vendor/bin/phpunit'
60+
env:
61+
WP_TESTS_DIR: ${{ runner.temp }}/wordpress-tests-lib
62+
WP_CORE_DIR: ${{ runner.temp }}/wordpress

‎.github/workflows/phpunit.yaml‎

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,61 @@ on:
88
branches:
99
- main
1010

11+
# Cancels all previous workflow runs for pull requests that have not completed.
1112
concurrency:
12-
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
13+
# The concurrency group contains the workflow name and the branch name for pull requests
14+
# or the commit hash for any other events.
15+
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
1316
cancel-in-progress: true
1417

1518
jobs:
16-
phpunit:
19+
build-matrix:
20+
name: Build WP/PHP Matrix
1721
runs-on: ubuntu-latest
18-
env:
19-
DB_DATABASE: wordpress_test
20-
DB_USER: root
21-
DB_PASSWORD: root
22-
strategy:
23-
fail-fast: false
24-
matrix:
25-
php-versions: ['8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
26-
wp-versions: [ '6.6', '6.7', '6.8', '6.9', '7.0', 'latest', 'trunk' ]
27-
name: WP ${{ matrix.wp-versions }} / PHP ${{ matrix.php-versions }}
28-
22+
outputs:
23+
matrix: ${{ steps.set-matrix.outputs.matrix }}
2924
steps:
30-
- name: Checkout
31-
uses: actions/checkout@v4
32-
33-
- name: Setup PHP
34-
uses: shivammathur/setup-php@v2
35-
with:
36-
php-version: ${{ matrix.php-versions }}
37-
38-
- name: Install Subversion.
39-
run: |
40-
sudo apt-get update -y
41-
sudo apt-get install -y subversion
25+
- id: set-matrix
26+
name: Generate matrix JSON
27+
shell: bash
28+
run: |
29+
declare -A wp_php
30+
wp_php["trunk"]="7.4 8.0 8.1 8.2 8.3 8.4 8.5"
31+
wp_php["7.1"]="7.4 8.0 8.1 8.2 8.3 8.4 8.5"
32+
wp_php["7.0"]="7.4 8.0 8.1 8.2 8.3 8.4 8.5"
33+
wp_php["6.9"]="7.4 8.0 8.1 8.2 8.3 8.4 8.5"
34+
wp_php["6.8"]="7.4 8.0 8.1 8.2 8.3 8.4"
4235
43-
- name: Check PHP Version
44-
run: php -v
36+
matrix='{"include":[]}'
37+
mapfile -t wp_versions < <(
38+
{
39+
printf '%s\n' "${!wp_php[@]}" | grep -x 'trunk' || true
40+
printf '%s\n' "${!wp_php[@]}" | grep -vx 'trunk' | sort -rV
41+
}
42+
)
4543
46-
- name: Composer install
47-
run: composer install --optimize-autoloader --prefer-dist
44+
for wp_version in "${wp_versions[@]}"; do
45+
for php_version in ${wp_php[$wp_version]}; do
46+
matrix=$(jq \
47+
--arg wp "$wp_version" \
48+
--arg php "$php_version" \
49+
'.include += [{"wp-version": $wp, "php-version": $php}]' \
50+
<<< "$matrix")
51+
done
52+
done
4853
49-
- name: Install WP Tests
50-
run: |
51-
sudo /etc/init.d/mysql start
52-
bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 ${{ matrix.wp-versions }}
54+
echo "matrix=$(jq -c . <<< "$matrix")" >> "$GITHUB_OUTPUT"
5355
54-
- name: PHPUnit tests
55-
run: ./vendor/bin/phpunit
56-
env:
57-
WP_TESTS_DIR: ${{ runner.temp }}/wordpress-tests-lib
58-
WP_CORE_DIR: ${{ runner.temp }}/wordpress
56+
phpunit:
57+
needs: build-matrix
58+
name: WP ${{ matrix.wp-version }} / PHP ${{ matrix.php-version }}
59+
strategy:
60+
matrix: ${{ fromJSON(needs.build-matrix.outputs.matrix) }}
61+
fail-fast: false
62+
uses: ./.github/workflows/phpunit-reusable.yml
63+
with:
64+
wp-version: ${{ matrix.wp-version }}
65+
php-version: ${{ matrix.php-version }}
5966

6067
required-status-checks:
6168
# This should be configured as the required status check in branch protection rules
@@ -64,7 +71,7 @@ jobs:
6471
# required status check will not need to be updated when matrix items change.
6572
name: PHPUnit Required Status Checks
6673
runs-on: 'ubuntu-24.04'
67-
needs: [ 'phpunit' ]
74+
needs: [ 'build-matrix', 'phpunit' ]
6875
if: ${{ always() }}
6976
steps:
7077
- name: Detect any failing status checks

‎bin/install-wp-tests.sh‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ install_wp() {
6464
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
6565
mkdir -p $TMPDIR/wordpress-trunk
6666
rm -rf $TMPDIR/wordpress-trunk/*
67-
svn export --quiet https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress
67+
git clone --depth=1 --single-branch https://github.com/wordpress/wordpress $TMPDIR/wordpress-trunk/wordpress
6868
mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR
6969
else
7070
if [ $WP_VERSION == 'latest' ]; then
@@ -103,13 +103,32 @@ install_test_suite() {
103103
local ioption='-i'
104104
fi
105105

106+
# GitHub's wordpress-develop mirror tags x.x.0 releases as "x.x.0", whereas
107+
# SVN's tag for that same release is just "x.x" (see WP_TESTS_TAG above).
108+
local WP_DEVELOP_REF=${WP_TESTS_TAG#tags/}
109+
WP_DEVELOP_REF=${WP_DEVELOP_REF#branches/}
110+
if [[ $WP_TESTS_TAG == tags/* && $WP_DEVELOP_REF =~ ^[0-9]+\.[0-9]+$ ]]; then
111+
WP_DEVELOP_REF="$WP_DEVELOP_REF.0"
112+
fi
113+
106114
# set up testing suite if it doesn't yet exist
107115
if [ ! -d $WP_TESTS_DIR ]; then
108116
# set up testing suite
109117
mkdir -p $WP_TESTS_DIR
110118
rm -rf $WP_TESTS_DIR/{includes,data}
111-
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
112-
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
119+
120+
local WP_DEVELOP_DIR=$TMPDIR/wordpress-develop
121+
rm -rf $WP_DEVELOP_DIR
122+
# shallow clone + partial blob filter + sparse-checkout: only fetch the
123+
# single ref's commit/tree objects, then only the blobs for the two
124+
# directories we actually need, instead of the whole wordpress-develop repo
125+
git clone --quiet --no-checkout --depth=1 --filter=blob:none --branch="$WP_DEVELOP_REF" --single-branch https://github.com/WordPress/wordpress-develop.git $WP_DEVELOP_DIR
126+
git -C $WP_DEVELOP_DIR sparse-checkout set --cone tests/phpunit/includes tests/phpunit/data
127+
git -C $WP_DEVELOP_DIR checkout --quiet "$WP_DEVELOP_REF"
128+
129+
mv $WP_DEVELOP_DIR/tests/phpunit/includes $WP_TESTS_DIR/includes
130+
mv $WP_DEVELOP_DIR/tests/phpunit/data $WP_TESTS_DIR/data
131+
rm -rf $WP_DEVELOP_DIR
113132
fi
114133

115134
if [ ! -f wp-tests-config.php ]; then

‎bin/rename.sh‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ echo "Renaming plugin to: $CAMEL_CASE_NAME"
2121
echo "Using kebab case: $KEBAB_CASE_NAME"
2222

2323
# Search all files for the old plugin name and replace it with the new one
24-
sed -i '' "s/${OLD_NAME}/${NEW_NAME}/g" *.* inc/*.* tests/tests/*.*
24+
sed -i '' "s/${OLD_NAME}/${NEW_NAME}/g" *.* inc/*.* tests/tests/*.* tests/*.*
2525
# Camel case replacements
26-
sed -i '' "s/${OLD_CAMEL_CASE_NAME}/${CAMEL_CASE_NAME}/g" *.* inc/*.* tests/tests/*.*
26+
sed -i '' "s/${OLD_CAMEL_CASE_NAME}/${CAMEL_CASE_NAME}/g" *.* inc/*.* tests/tests/*.* tests/*.*
2727
# Kebab case replacements
28-
sed -i '' "s/${OLD_KEBAB_CASE_NAME}/${KEBAB_CASE_NAME}/g" *.* inc/*.* tests/tests/*.*
28+
sed -i '' "s/${OLD_KEBAB_CASE_NAME}/${KEBAB_CASE_NAME}/g" *.* inc/*.* tests/tests/*.* tests/*.*
2929

3030
# Replace the plugin description in the main file with the new name
3131
# Rename the main plugin file

0 commit comments

Comments
 (0)