Skip to content

Commit e9629d0

Browse files
authored
Workflow test
1 parent b4259d4 commit e9629d0

File tree

7 files changed

+350
-3
lines changed

7 files changed

+350
-3
lines changed

.github/workflows/phpunit.yml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: PHPUnit Tests
2+
3+
on:
4+
# Run on pushes to main branch and on all pull requests
5+
push:
6+
branches: [ main ]
7+
pull_request:
8+
# Allow manually triggering the workflow
9+
workflow_dispatch:
10+
11+
# Cancels all previous workflow runs for the same branch that have not yet completed
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.ref }}
14+
cancel-in-progress: true
15+
16+
permissions:
17+
contents: read
18+
pull-requests: write
19+
20+
jobs:
21+
test:
22+
name: PHP ${{ matrix.php-version }} | WP ${{ matrix.wp-version }}${{ matrix.multisite && ' (multisite)' || '' }}
23+
runs-on: ubuntu-latest
24+
25+
strategy:
26+
matrix:
27+
include:
28+
- php-version: '7.4'
29+
wp-version: '6.0'
30+
multisite: false
31+
32+
- php-version: '8.0'
33+
wp-version: 'latest'
34+
multisite: false
35+
36+
- php-version: '8.1'
37+
wp-version: 'latest'
38+
multisite: true
39+
40+
- php-version: '8.2'
41+
wp-version: 'latest'
42+
multisite: false
43+
fail-fast: false
44+
45+
services:
46+
mysql:
47+
image: mysql:8.0
48+
env:
49+
MYSQL_ALLOW_EMPTY_PASSWORD: false
50+
MYSQL_ROOT_PASSWORD: root
51+
MYSQL_DATABASE: wordpress_test
52+
ports:
53+
- 3306:3306
54+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
55+
56+
steps:
57+
- name: Checkout code
58+
uses: actions/checkout@v4
59+
60+
- name: Setup PHP
61+
uses: shivammathur/setup-php@v2
62+
with:
63+
php-version: ${{ matrix.php-version }}
64+
extensions: mysqli, curl, zip, intl, gd, mbstring, fileinfo, xml
65+
coverage: none
66+
tools: composer:v2
67+
68+
- name: Remove the PHP platform requirement
69+
run: composer config --unset platform.php
70+
71+
- name: Install Composer dependencies
72+
uses: ramsey/composer-install@v2
73+
with:
74+
dependency-versions: highest
75+
composer-options: "--prefer-dist --no-suggest --no-progress"
76+
77+
- name: Setup WP Tests
78+
run: |
79+
bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 ${{ matrix.wp-version }}
80+
working-directory: ${{ github.workspace }}/tests
81+
82+
- name: Run tests (single site)
83+
if: ${{ !matrix.multisite }}
84+
run: vendor/bin/phpunit --config phpunit.xml
85+
86+
- name: Run tests (multisite)
87+
if: ${{ matrix.multisite }}
88+
run: vendor/bin/phpunit --config phpunit.xml
89+
env:
90+
WP_MULTISITE: 1
91+
92+
- name: Add labels to any PRs
93+
if: github.event_name == 'pull_request'
94+
uses: actions/github-script@v6
95+
with:
96+
github-token: ${{ secrets.GITHUB_TOKEN }}
97+
script: |
98+
github.rest.issues.addLabels({
99+
issue_number: context.issue.number,
100+
owner: context.repo.owner,
101+
repo: context.repo.repo,
102+
labels: ['automated', 'documentation', 'plugin update']
103+
})

CHANGELOG.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
# Changelog
2-
3-
All notable changes to the Simple WP Optimizer plugin will be documented in this file.
1+
# Chang## [Unreleased]
2+
### Added
3+
- Added standardized readme.txt file for WordPress.org repository
4+
- Implemented automated "Tested up to" WordPress version checker using skaut/wordpress-version-checker GitHub Action
5+
- Added full plugin header information including plugin URI and License URI
6+
- Created .wordpress-version-checker.json configuration for WordPress compatibility checking
7+
- Added automated PR creation for WordPress compatibility updates with "automated" and "documentation" labels
8+
- Added PHPUnit test framework and GitHub Actions workflow for automated testing
9+
- Configured cross-version PHP testing (7.4-8.2) with multisite supportll notable changes to the Simple WP Optimizer plugin will be documented in this file.
410

511
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
612
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ This repository uses the [WordPress Version Checker](https://github.com/skaut/wo
103103

104104
This ensures our plugin stays up-to-date with the latest WordPress versions without manual intervention.
105105

106+
### Automated Testing
107+
108+
The plugin includes a comprehensive PHPUnit test suite that runs automatically on GitHub Actions. Our testing matrix includes:
109+
110+
- PHP versions: 7.4, 8.0, 8.1, 8.2
111+
- Latest WordPress version
112+
- Both single site and multisite installations
113+
114+
This ensures code quality and compatibility across different PHP versions and WordPress configurations.
115+
106116

107117

108118
## Contributing

phpunit.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0"?>
2+
<phpunit
3+
bootstrap="tests/bootstrap.php"
4+
backupGlobals="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
>
10+
<testsuites>
11+
<testsuite name="Simple WP Optimizer Tests">
12+
<directory prefix="test-" suffix=".php">./tests/</directory>
13+
</testsuite>
14+
</testsuites>
15+
<coverage>
16+
<include>
17+
<directory suffix=".php">./</directory>
18+
</include>
19+
<exclude>
20+
<directory>./tests</directory>
21+
<directory>./vendor</directory>
22+
</exclude>
23+
</coverage>
24+
<php>
25+
<env name="WP_PHPUNIT__TESTS_CONFIG" value="tests/wp-config.php" />
26+
</php>
27+
</phpunit>

tests/bin/install-wp-tests.sh

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/usr/bin/env bash
2+
3+
if [ $# -lt 3 ]; then
4+
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
5+
exit 1
6+
fi
7+
8+
DB_NAME=$1
9+
DB_USER=$2
10+
DB_PASS=$3
11+
DB_HOST=${4-localhost}
12+
WP_VERSION=${5-latest}
13+
SKIP_DB_CREATE=${6-false}
14+
15+
TMPDIR=${TMPDIR-/tmp}
16+
TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//")
17+
WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib}
18+
WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress}
19+
20+
download() {
21+
if command -v curl &>/dev/null; then
22+
curl -s "$1" > "$2";
23+
elif command -v wget &>/dev/null; then
24+
wget -nv -O "$2" "$1"
25+
fi
26+
}
27+
28+
if [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+\-(beta|RC)[0-9]+$ ]]; then
29+
WP_BRANCH=${WP_VERSION%\-*}
30+
WP_TESTS_TAG="branches/$WP_BRANCH"
31+
elif [[ $WP_VERSION =~ ^[0-9]+\.[0-9]+$ ]]; then
32+
WP_TESTS_TAG="branches/$WP_VERSION"
33+
elif [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
34+
if [[ $WP_VERSION =~ [0-9]+\.[0-9]+\.[0] ]]; then
35+
# version x.x.0 means the first release of the major version, so strip off the .0 and download version x.x
36+
WP_TESTS_TAG="tags/${WP_VERSION%??}"
37+
else
38+
WP_TESTS_TAG="tags/$WP_VERSION"
39+
fi
40+
elif [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
41+
WP_TESTS_TAG="trunk"
42+
else
43+
# http serves a single offer, whereas https serves multiple. we only want one
44+
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
45+
LATEST_VERSION=$(grep -o '"version":"[^"]*' /tmp/wp-latest.json | sed 's/"version":"//')
46+
if [[ -z "$LATEST_VERSION" ]]; then
47+
echo "Latest WordPress version could not be found"
48+
exit 1
49+
fi
50+
WP_TESTS_TAG="tags/$LATEST_VERSION"
51+
fi
52+
set -ex
53+
54+
install_wp() {
55+
if [ -d $WP_CORE_DIR ]; then
56+
return;
57+
fi
58+
59+
mkdir -p $WP_CORE_DIR
60+
61+
if [[ $WP_VERSION == 'nightly' || $WP_VERSION == 'trunk' ]]; then
62+
mkdir -p $TMPDIR/wordpress-trunk
63+
rm -rf $TMPDIR/wordpress-trunk/*
64+
svn export https://core.svn.wordpress.org/trunk $TMPDIR/wordpress-trunk/wordpress
65+
mv $TMPDIR/wordpress-trunk/wordpress/* $WP_CORE_DIR
66+
else
67+
if [ $WP_VERSION == 'latest' ]; then
68+
local ARCHIVE_NAME='latest'
69+
else
70+
local ARCHIVE_NAME="wordpress-$WP_VERSION"
71+
fi
72+
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
73+
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
74+
fi
75+
76+
download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
77+
}
78+
79+
install_test_suite() {
80+
# portable in-place argument for both GNU sed and Mac OSX sed
81+
if [[ $(uname -s) == 'Darwin' ]]; then
82+
local ioption='-i.bak'
83+
else
84+
local ioption='-i'
85+
fi
86+
87+
# set up testing suite if it doesn't yet exist
88+
if [ ! -d $WP_TESTS_DIR ]; then
89+
# set up testing suite
90+
mkdir -p $WP_TESTS_DIR
91+
rm -rf $WP_TESTS_DIR/{includes,data}
92+
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/includes/ $WP_TESTS_DIR/includes
93+
svn export --quiet --ignore-externals https://develop.svn.wordpress.org/${WP_TESTS_TAG}/tests/phpunit/data/ $WP_TESTS_DIR/data
94+
fi
95+
96+
if [ ! -f wp-tests-config.php ]; then
97+
download https://develop.svn.wordpress.org/${WP_TESTS_TAG}/wp-tests-config-sample.php "$WP_TESTS_DIR"/wp-tests-config.php
98+
# remove all forward slashes in the end
99+
WP_CORE_DIR=$(echo $WP_CORE_DIR | sed "s:/\+$::")
100+
sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
101+
sed $ioption "s:__DIR__ . '/src/':'$WP_CORE_DIR/':" "$WP_TESTS_DIR"/wp-tests-config.php
102+
sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" "$WP_TESTS_DIR"/wp-tests-config.php
103+
sed $ioption "s/yourusernamehere/$DB_USER/" "$WP_TESTS_DIR"/wp-tests-config.php
104+
sed $ioption "s/yourpasswordhere/$DB_PASS/" "$WP_TESTS_DIR"/wp-tests-config.php
105+
sed $ioption "s|localhost|${DB_HOST}|" "$WP_TESTS_DIR"/wp-tests-config.php
106+
fi
107+
108+
}
109+
110+
install_db() {
111+
112+
if [ ${SKIP_DB_CREATE} = "true" ]; then
113+
return 0
114+
fi
115+
116+
# parse DB_HOST for port or socket references
117+
local PARTS=(${DB_HOST//\:/ })
118+
local DB_HOSTNAME=${PARTS[0]};
119+
local DB_SOCK_OR_PORT=${PARTS[1]};
120+
local EXTRA=""
121+
122+
if ! [ -z $DB_HOSTNAME ] ; then
123+
if [ $(echo $DB_SOCK_OR_PORT | grep -e '^[0-9]\{1,\}$') ]; then
124+
EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
125+
elif ! [ -z $DB_SOCK_OR_PORT ] ; then
126+
EXTRA=" --socket=$DB_SOCK_OR_PORT"
127+
elif ! [ -z $DB_HOSTNAME ] ; then
128+
EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
129+
fi
130+
fi
131+
132+
# create database
133+
mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
134+
}
135+
136+
install_wp
137+
install_test_suite
138+
install_db

tests/bootstrap.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* PHPUnit bootstrap file for plugin tests.
4+
*
5+
* @package Simple_WP_Optimizer
6+
*/
7+
8+
// Load the Composer autoloader.
9+
require_once dirname( __DIR__ ) . '/vendor/autoload.php';
10+
11+
// Load the PHPUnit Polyfills for cross-version compatibility.
12+
require_once dirname( __DIR__ ) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
13+
14+
// Use the requested test WordPress instance
15+
$_tests_dir = getenv( 'WP_TESTS_DIR' );
16+
if ( ! $_tests_dir ) {
17+
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
18+
}
19+
20+
// Handle trailing slash in path
21+
if ( substr( $_tests_dir, -1 ) !== '/' ) {
22+
$_tests_dir .= '/';
23+
}
24+
25+
// Make sure the tests directory exists
26+
if ( ! file_exists( $_tests_dir . 'includes/functions.php' ) ) {
27+
echo "Could not find $_tests_dir/includes/functions.php, have you run the test installer script?" . PHP_EOL;
28+
exit( 1 );
29+
}
30+
31+
// Give access to tests_add_filter() function.
32+
require_once $_tests_dir . 'includes/functions.php';
33+
34+
/**
35+
* Manually load the plugin being tested.
36+
*/
37+
function _manually_load_plugin() {
38+
require dirname( __DIR__ ) . '/simple-wp-optimizer.php';
39+
}
40+
41+
// Start up the WP testing environment.
42+
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
43+
44+
require $_tests_dir . 'includes/bootstrap.php';

tests/test-plugin.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Class Test_Simple_WP_Optimizer
4+
*
5+
* @package Simple_WP_Optimizer
6+
*/
7+
8+
/**
9+
* Sample test case.
10+
*/
11+
class Test_Simple_WP_Optimizer extends WP_UnitTestCase {
12+
/**
13+
* Test that the plugin can be loaded correctly.
14+
*/
15+
public function test_plugin_loaded() {
16+
// Simply check that the plugin file has been loaded
17+
$this->assertTrue( function_exists( 'simple_wp_optimizer_init' ) || class_exists( 'Simple_WP_Optimizer' ), 'Plugin functions not found, plugin may not be loaded correctly.' );
18+
}
19+
}

0 commit comments

Comments
 (0)