Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add standalone PHPUnit configs in plugins/ #1013

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
bbcfc87
Move tests to specific plugin directory
thelovekesh Feb 24, 2024
227d0ec
Add plugin specific phpunit config
thelovekesh Feb 24, 2024
4808f24
Add tests helpers.php file
thelovekesh Feb 24, 2024
317e86a
Move tests helpers to class for the sake of autoloading
thelovekesh Feb 24, 2024
54f229f
Update undefined function error
thelovekesh Feb 24, 2024
679d583
Add tests bootstrap file for standalone plugins
thelovekesh Feb 24, 2024
608c89f
Update tests file names for webp-uploads
thelovekesh Feb 24, 2024
f1f2922
Enable single quotes in yml
thelovekesh Feb 24, 2024
66613cb
Add setup-wp-tests composite action
thelovekesh Feb 24, 2024
175dec0
Update main plugin phpunit config
thelovekesh Feb 24, 2024
5cba968
Update phpunit tests workflow for main plugin tests
thelovekesh Feb 24, 2024
1c53502
Update wp tests setup script path
thelovekesh Feb 24, 2024
b4fb039
Fix wp version in matrix
thelovekesh Feb 24, 2024
c767259
Fix env vars and multisite matrix
thelovekesh Feb 24, 2024
8d64d5b
Update matrix and move test run step to main workflow file
thelovekesh Feb 24, 2024
8b4b76a
Fix phpunit version input
thelovekesh Feb 24, 2024
3ebf3eb
Update tests title
thelovekesh Feb 24, 2024
057e5db
Add job to run unit tests for standalone plugins
thelovekesh Feb 24, 2024
c5e5516
Fix standalone plugins path
thelovekesh Feb 24, 2024
5925e7f
Fix testdata path in standalone plugins
thelovekesh Feb 24, 2024
093b7d9
Move testcases abstract classes to respective plugins
thelovekesh Feb 24, 2024
4fc069d
Remove redundant classes
thelovekesh Feb 24, 2024
fb1ce25
Autoload tests files
thelovekesh Feb 24, 2024
9c527f6
Update assertStringContainsString() first param to be string
thelovekesh Feb 25, 2024
6403915
Bump actions/cache to v4
thelovekesh Feb 25, 2024
fd6754b
Move images required for dominant-color-images test cases to it's plu…
thelovekesh Feb 25, 2024
197cf1d
Update dominant-color-images plugin test data images path
thelovekesh Feb 25, 2024
ed569e5
Fix remaining files due to merge conflicts
thelovekesh Feb 26, 2024
bef73f8
Update plugin path in PHPUnit bootstrap file
thelovekesh Feb 26, 2024
ea1f071
Update tests helper class with more genric file called bootstrap-func…
thelovekesh Feb 27, 2024
cc46176
Move tests helper class to file called bootstrap-functions
thelovekesh Feb 27, 2024
debf72d
Update bootstrap init
thelovekesh Feb 27, 2024
068700b
Fix plugin dir in main tests bootstrap file
thelovekesh Feb 27, 2024
9faad87
Update bootstrap init to include plugin directory
thelovekesh Feb 27, 2024
39510ca
Fix plugin dir in tests bootstrap file
thelovekesh Feb 27, 2024
93bda8b
Update CODEOWNERS for standalone plugins
thelovekesh Feb 27, 2024
3e3f04c
Add fixtures in dominant-color-images color images to be run in multi…
thelovekesh Feb 27, 2024
44e0771
MOve common env at workflow level from job level
thelovekesh Mar 2, 2024
ac56c7c
Update pre-run to calculate matrix for PHPUnit tests
thelovekesh Mar 2, 2024
f02b383
Fix multisite tests
thelovekesh Mar 2, 2024
50bb27f
Setup plugin tests for speculation-rules plugin
thelovekesh Mar 2, 2024
25bb171
Update multisite tests order
thelovekesh Mar 2, 2024
ffb640a
Fix bootstrap file for dominant-color-images plugin
thelovekesh Mar 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ indent_size = 2
insert_final_newline = false
indent_style = space
indent_size = 2
quote_type = single

[.github/CODEOWNERS]
indent_style = space
5 changes: 1 addition & 4 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@

# Plugin: Auto-sizes for Lazy-Loaded Images
/plugins/auto-sizes @joemcgill
/tests/plugins/auto-sizes @joemcgill

# Plugin: Speculation Rules
/plugins/speculation-rules @felixarntz
/tests/plugins/speculation-rules @felixarntz

# Plugin: WebP Uploads
/plugins/webp-uploads @adamsilverstein @felixarntz
/tests/plugins/webp-uploads @adamsilverstein @felixarntz

# Plugin: Dominant Color Images
/plugins/dominant-color-images @pbearne @spacedmonkey
/tests/plugins/dominant-color-images @pbearne @spacedmonkey
/plugins/dominant-color-images @pbearne @spacedmonkey
75 changes: 75 additions & 0 deletions .github/actions/setup-wp-tests/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
name: Setup WP Tests

description: 'Setup WordPress tests for GitHub Actions'

inputs:
wp:
description: 'WordPress version'
required: false
default: 'latest'
php:
description: 'PHP version'
required: false
default: '7.4'
mysql-port:
description: 'MySQL port'
required: false
default: '3306'
coverage:
description: 'Code coverage driver'
required: false
default: 'none'
phpunit:
description: 'PHPUnit version'
required: false
default: '9.3'
plugin-path:
description: 'Path to the plugin'
required: false
default: ${{ github.workspace }}

runs:
using: 'composite'
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.php }}
tools: 'composer,phpunit:${{ inputs.phpunit }}'
coverage: ${{ inputs.coverage }}

- name: Setup composer cache
uses: actions/cache@v4
id: php-composer-cache
env:
SEGMENT_DOWNLOAD_TIMEOUT_MINS: '5'
with:
path: vendor
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}-${{ hashFiles('.github/actions/setup-wp-tests/action.yml') }}

- name: Install composer dependencies
if: ${{ steps.php-composer-cache.outputs.cache-hit != 'true' }}
shell: bash
run: composer install --ansi --no-interaction --prefer-dist --ignore-platform-reqs

- name: Remove default PHPUnit
shell: bash
run: |
rm -rf vendor/phpunit
composer dump-autoload

# Scan the logs for failing tests and surface that information by creating annotations and log file decorations.
- name: Setup problem matcher to provide annotations for PHPUnit
shell: bash
# The JSON file is provided by the `shivammathur/setup-php` action. See https://github.com/shivammathur/setup-php#problem-matchers.
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Install WP Tests
shell: bash
run: |
bash ./.github/actions/setup-wp-tests/install-wp-tests.sh wordpress_test root '' 127.0.0.1:${{ inputs.mysql-port }} ${{ inputs.wp }} true

- name: Copy plugin to WP plugins directory
shell: bash
run: cp -r ${{ inputs.plugin-path }} ${{ env.WP_CORE_DIR }}/src/wp-content/plugins/performance
142 changes: 142 additions & 0 deletions .github/actions/setup-wp-tests/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env bash

set -e

if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
exit 1
fi

DB_NAME=$1
DB_USER=$2
DB_PASS=$3
DB_HOST=${4-localhost}
WP_VERSION=${5-latest}
SKIP_DB_CREATE=${6-true}

TMPDIR=${TMPDIR-/tmp}
TMPDIR=$(echo "$TMPDIR" | sed -e "s/\/$//")
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress}

download() {
if [ $(which curl) ]; then
curl -s "$1" > "$2";
elif [ $(which wget) ]; then
wget -nv -O "$2" "$1"
fi
}

if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then
WP_BRANCH=${WP_VERSION%\-*}
WP_TESTS_TAG="branches/$WP_BRANCH"

elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
WP_TESTS_TAG="branches/$WP_VERSION"
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
WP_TESTS_TAG="tags/${WP_VERSION%??}"
else
WP_TESTS_TAG="tags/$WP_VERSION"
fi
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
WP_TESTS_TAG="trunk"
else
# http serves a single offer, whereas https serves multiple. we only want one
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
grep -E '[0-9]+\.[0-9]+(\.[0-9]+)?' /tmp/wp-latest.json
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
if [[ -z "$LATEST_VERSION" ]]; then
echo "Latest WordPress version could not be found"
exit 1
fi
WP_TESTS_TAG="tags/$LATEST_VERSION"
fi

install_wp() {

if [ -d "$WP_CORE_DIR" ]; then
return;
fi

if grep -isqE 'trunk|alpha|beta|rc' <<< "$WP_VERSION"; then
local SVN_URL=https://develop.svn.wordpress.org/trunk/
elif [ "$WP_VERSION" == 'latest' ]; then
local TAG=$( svn ls https://develop.svn.wordpress.org/tags | tail -n 1 | sed 's:/$::' )
local SVN_URL="https://develop.svn.wordpress.org/tags/$TAG/"
elif [[ "$WP_VERSION" =~ ^[0-9]+\.[0-9]+$ ]]; then
# Use the release branch if no patch version supplied. This is useful to keep testing the latest minor version.
local SVN_URL="https://develop.svn.wordpress.org/branches/$WP_VERSION/"
else
local SVN_URL="https://develop.svn.wordpress.org/tags/$WP_VERSION/"
fi

echo "Installing WP from $SVN_URL to $WP_CORE_DIR"

svn export -q "$SVN_URL" "$WP_CORE_DIR"

# Download `wp-includes` folder from the WordPress Core SVN repo to include built internal dependencies.
local SVN_CORE_URL=${SVN_URL/develop/core}
svn export -q --force "${SVN_CORE_URL}wp-includes" "$WP_CORE_DIR/src/wp-includes"
}

install_test_suite() {
# portable in-place argument for both GNU sed and Mac OSX sed
if [[ $(uname -s) == 'Darwin' ]]; then
local ioption='-i.bak'
else
local ioption='-i'
fi

# set up testing suite if it doesn't yet exist
if [ ! -d "$WP_TESTS_DIR" ]; then
# set up testing suite
mkdir -p "$WP_TESTS_DIR"
svn co --quiet --ignore-externals https://develop.svn.wordpress.org/"${WP_TESTS_TAG}"/tests/phpunit/includes/ "$WP_TESTS_DIR/includes"
svn co --quiet --ignore-externals https://develop.svn.wordpress.org/"${WP_TESTS_TAG}"/tests/phpunit/data/ "$WP_TESTS_DIR/data"
fi

if [ ! -f wp-tests-config.php ]; then
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
# remove all forward slashes in the end
WP_CORE_DIR="$(echo $WP_CORE_DIR | sed "s:/\+$::")"
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/src/':" "$WP_TESTS_DIR/wp-tests-config.php"
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR/wp-tests-config.php"
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR/wp-tests-config.php"
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR/wp-tests-config.php"
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR/wp-tests-config.php"
fi

}

install_db() {

if [ "${SKIP_DB_CREATE}" = "true" ]; then
return 0
fi

# parse DB_HOST for port or socket references
local PARTS=("${DB_HOST//\:/ }")
local DB_HOSTNAME=${PARTS[0]};
local DB_SOCK_OR_PORT=${PARTS[1]};
local EXTRA=""

if [ -n "$DB_HOSTNAME" ] ; then
if [ "$(echo "$DB_SOCK_OR_PORT" | grep -e '^[0-9]\{1,\}$')" ]; then
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
elif [ -n "$DB_SOCK_OR_PORT" ] ; then
EXTRA=" --socket=$DB_SOCK_OR_PORT"
elif [ -n "$DB_HOSTNAME" ] ; then
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
fi
fi

# create database
mariadb-admin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA || \
mysqladmin create "$DB_NAME" --user="$DB_USER" --password="$DB_PASS"$EXTRA
}

install_wp
install_test_suite
install_db
Loading
Loading