Skip to content

Commit 8d4a752

Browse files
authored
Merge pull request #241 from wpengine/chore-add-phpunit-tests
chore: Added test to Previews and setup test workflow for plugins
2 parents d679035 + 1500833 commit 8d4a752

File tree

69 files changed

+12228
-2601
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+12228
-2601
lines changed

.github/actions/code-quality/action.yml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ inputs:
44
working-directory:
55
description: 'Directory to run composer and quality tools in'
66
required: true
7+
php-version:
8+
description: 'PHP version to use'
9+
required: false
10+
default: '7.4'
11+
composer-options:
12+
description: 'Additional composer options'
13+
required: false
14+
default: '--no-progress'
15+
716
runs:
817
using: "composite"
918
steps:
10-
- name: Setup PHP
11-
uses: shivammathur/setup-php@v2
12-
with:
13-
php-version: '7.4'
14-
tools: composer:v2
15-
coverage: none
16-
17-
- name: Install dependencies
18-
uses: ramsey/composer-install@v2
19+
- name: Setup PHP with Cached Composer
20+
uses: ./.github/actions/setup-php-composer
1921
with:
22+
php-version: ${{ inputs.php-version }}
2023
working-directory: ${{ inputs.working-directory }}
21-
composer-options: "--no-progress"
24+
composer-options: ${{ inputs.composer-options }}
2225

2326
- name: Run PHPStan
2427
working-directory: ${{ inputs.working-directory }}
@@ -33,4 +36,4 @@ runs:
3336
- name: Run PHP CodeSniffer
3437
working-directory: ${{ inputs.working-directory }}
3538
run: composer run-script check-cs
36-
shell: bash
39+
shell: bash
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
name: "Run Codeception Tests"
2+
description: "Sets up environment and runs Codeception test suites"
3+
inputs:
4+
working-directory:
5+
description: "Plugin directory to run tests in"
6+
required: true
7+
php:
8+
description: "PHP version"
9+
required: true
10+
extensions:
11+
description: "PHP extensions"
12+
required: true
13+
wordpress:
14+
description: "WordPress version"
15+
required: true
16+
composer-options:
17+
description: "Additional composer options"
18+
required: false
19+
default: "--no-progress"
20+
runs:
21+
using: "composite"
22+
steps:
23+
- name: Setup PHP with Cached Composer
24+
uses: ./.github/actions/setup-php-composer
25+
with:
26+
php-version: ${{ inputs.php }}
27+
working-directory: ${{ inputs.working-directory }}
28+
composer-options: ${{ inputs.composer-options }}
29+
30+
- name: Setup environment
31+
run: |
32+
cp ${{ inputs.working-directory }}/.docker/.env.ci ${{ inputs.working-directory }}/.env
33+
cd ${{ inputs.working-directory }}
34+
echo "INCLUDE_EXTENSIONS=${{ inputs.extensions }}" >> .env
35+
echo "WP_VERSION=${{ inputs.wordpress }}" >> .env
36+
echo "PHP_VERSION=${{ inputs.php }}" >> .env
37+
shell: bash
38+
39+
- name: Build test environment
40+
uses: nick-invision/retry@v2
41+
with:
42+
timeout_minutes: 10
43+
max_attempts: 3
44+
retry_on: error
45+
shell: bash
46+
command: |
47+
cd ${{ inputs.working-directory }}
48+
composer run docker:build
49+
env:
50+
WP_VERSION: ${{ inputs.wordpress }}
51+
PHP_VERSION: ${{ inputs.php }}
52+
53+
- name: Start test environment
54+
working-directory: ${{ inputs.working-directory }}
55+
shell: bash
56+
run: |
57+
docker compose --env-file .env up --detach
58+
59+
CONTAINER_ID=$(docker compose ps -q wordpress)
60+
if [ -n "$CONTAINER_ID" ]; then
61+
docker exec $CONTAINER_ID init-docker.sh
62+
else
63+
echo "Error: WordPress container not found."
64+
exit 1
65+
fi
66+
env:
67+
WP_VERSION: ${{ inputs.wordpress }}
68+
PHP_VERSION: ${{ inputs.php }}
69+
70+
- name: Run Acceptance Tests w/ Docker
71+
working-directory: ${{ inputs.working-directory }}
72+
shell: bash
73+
run: |
74+
docker exec \
75+
--env DEBUG=${{ env.DEBUG }} \
76+
--env SKIP_TESTS_CLEANUP=${{ env.SKIP_TESTS_CLEANUP }} \
77+
--env SUITES=acceptance \
78+
$(docker compose ps -q wordpress) \
79+
bash -c "cd wp-content/plugins/$(basename $(echo ${{ inputs.working-directory }} | sed 's:/*$::')) && bin/run-codeception.sh"
80+
env:
81+
DEBUG: ${{ env.ACTIONS_STEP_DEBUG }}
82+
SKIP_TESTS_CLEANUP: "true"
83+
continue-on-error: true
84+
85+
- name: Run Functional Tests w/ Docker
86+
working-directory: ${{ inputs.working-directory }}
87+
shell: bash
88+
run: |
89+
docker exec \
90+
--env DEBUG=${{ env.DEBUG }} \
91+
--env SKIP_TESTS_CLEANUP=${{ env.SKIP_TESTS_CLEANUP }} \
92+
--env SUITES=functional \
93+
$(docker compose ps -q wordpress) \
94+
bash -c "cd wp-content/plugins/$(basename ${{ inputs.working-directory }}) && bin/run-codeception.sh"
95+
env:
96+
DEBUG: ${{ env.ACTIONS_STEP_DEBUG }}
97+
SKIP_TESTS_CLEANUP: "true"
98+
continue-on-error: true
99+
100+
- name: Run WPUnit Tests w/ Docker
101+
working-directory: ${{ inputs.working-directory }}
102+
shell: bash
103+
run: |
104+
docker exec \
105+
--env COVERAGE=${{ inputs.coverage }} \
106+
--env USING_XDEBUG=${{ inputs.coverage }} \
107+
--env DEBUG=${{ env.DEBUG }} \
108+
--env SUITES=wpunit \
109+
$(docker compose ps -q wordpress) \
110+
bash -c "cd wp-content/plugins/$(basename ${{ inputs.working-directory }}) && bin/run-codeception.sh"
111+
env:
112+
DEBUG: ${{ env.ACTIONS_STEP_DEBUG }}

.github/actions/create-plugin-artifact/action.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@ inputs:
44
slug:
55
description: 'Plugin slug (directory name under plugins/)'
66
required: true
7+
composer-options:
8+
description: 'Additional composer options'
9+
required: false
10+
default: '--no-progress'
711
runs:
812
using: "composite"
913
steps:
10-
- name: Set up PHP
11-
uses: shivammathur/setup-php@v2
14+
- name: Setup PHP with Cached Composer
15+
uses: ./.github/actions/setup-php-composer
1216
with:
13-
php-version: '7.4'
17+
php-version: ${{ inputs.php }}
18+
working-directory: plugins/${{ inputs.slug }}
19+
composer-options: ${{ inputs.composer-options }}
1420

15-
- name: Install dependencies and build
21+
- name: Create plugin artifact
1622
working-directory: plugins/${{ inputs.slug }}
1723
run: |
18-
composer install --no-dev --optimize-autoloader
1924
echo "${GITHUB_SHA}" > build-sha.txt
2025
rm -f plugin-build/${{ inputs.slug }}-*.zip
2126
composer archive -vvv --format=zip --file="plugin-build/${{ inputs.slug }}" --dir="."
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: "Setup PHP with Cached Composer"
2+
description: "Setup PHP and install Composer dependencies with caching"
3+
inputs:
4+
php-version:
5+
description: "PHP version to setup"
6+
required: false
7+
default: "7.4"
8+
working-directory:
9+
description: "Working directory for composer install"
10+
required: true
11+
composer-options:
12+
description: "Additional composer options"
13+
required: false
14+
default: "--no-progress --optimize-autoloader"
15+
tools:
16+
description: "Tools to install with PHP"
17+
required: false
18+
default: "composer:v2"
19+
20+
runs:
21+
using: "composite"
22+
steps:
23+
- name: Setup PHP
24+
uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: ${{ inputs.php-version }}
27+
tools: ${{ inputs.tools }}
28+
coverage: none
29+
30+
- name: Validate Composer File
31+
shell: bash
32+
working-directory: ${{ inputs.working-directory }}
33+
run: |
34+
if [ ! -f "composer.json" ]; then
35+
echo "Error: composer.json missing in ${{ inputs.working-directory }}"
36+
exit 1
37+
fi
38+
39+
- name: Get Composer cache directory
40+
id: composer-cache
41+
shell: bash
42+
working-directory: ${{ inputs.working-directory }}
43+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
44+
45+
- name: Cache Composer dependencies
46+
uses: actions/cache@v4
47+
with:
48+
path: |
49+
${{ steps.composer-cache.outputs.dir }}
50+
${{ inputs.working-directory }}/vendor
51+
key: composer-${{ runner.os }}-php${{ inputs.php-version }}-${{ hashFiles(format('{0}/composer.lock', inputs.working-directory)) }}
52+
restore-keys: |
53+
composer-${{ runner.os }}-php${{ inputs.php-version }}-
54+
composer-${{ runner.os }}-
55+
56+
- name: Install Composer dependencies
57+
uses: ramsey/composer-install@v2
58+
with:
59+
working-directory: ${{ inputs.working-directory }}
60+
composer-options: ${{ inputs.composer-options }}

.github/workflows/code-quality.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
name: Code Quality
22

33
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- 'plugins/**.php'
49
pull_request:
510
paths:
6-
- 'plugins/**'
11+
- 'plugins/**.php'
712

813
jobs:
914
run:

.github/workflows/codeception.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Codeception
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- 'plugins/**.php'
9+
pull_request:
10+
paths:
11+
- 'plugins/**.php'
12+
13+
# Cancel previous workflow run groups that have not completed.
14+
concurrency:
15+
# Group workflow runs by workflow name, along with the head branch ref of the pull request
16+
# or otherwise the branch or tag ref.
17+
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.ref }}
18+
cancel-in-progress: true
19+
20+
jobs:
21+
continuous_integration:
22+
runs-on: ubuntu-latest
23+
name: WordPress ${{ matrix.wordpress }} on PHP ${{ matrix.php }}
24+
25+
strategy:
26+
matrix:
27+
php: ["8.3","8.2","8.1"]
28+
wordpress: ["6.8","6.7","6.6","6.5"]
29+
include:
30+
- php: "8.2"
31+
wordpress: "6.8"
32+
coverage: 1
33+
fail-fast: false
34+
35+
steps:
36+
- name: Checkout
37+
uses: actions/checkout@v4
38+
39+
- name: Get changed plugin directory
40+
id: plugin
41+
run: |
42+
git fetch --prune --unshallow
43+
plugin=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '^plugins/' | head -1 | cut -d/ -f2)
44+
echo "slug=$plugin" >> $GITHUB_OUTPUT
45+
46+
- name: Validate composer.json
47+
run: |
48+
if [ ! -f "plugins/${{ steps.plugin.outputs.slug }}/composer.json" ]; then
49+
echo "Warning: composer.json missing in plugins/${{ steps.plugin.outputs.slug }}"
50+
fi
51+
52+
- name: Run Codeception Tests
53+
uses: ./.github/actions/codeception
54+
with:
55+
working-directory: plugins/${{ steps.plugin.outputs.slug }}
56+
php: ${{ matrix.php }}
57+
wordpress: ${{ matrix.wordpress }}
58+
extensions: json,mbstring
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
PLUGIN_SLUG=hwp-previews
2+
3+
# Configure these to match your existing testing environment or the one you want to create with Docker.
4+
## Usually, these values should match the ones in the `wp-config.php` file.
5+
## If using Local by Flywheel, you can `open AdminerEvo` and find the values in the URL: `http://localhost:{DB_PORT}/?username={DB_USER}&db={DB_NAME}`
6+
## NOTE: Codeception may modify or the database during testing. If you want to preserve your local data, create a new database and use that for the `DB_NAME`.
7+
DB_NAME=wordpress
8+
DB_HOST=mysql
9+
DB_USER=root
10+
DB_PASSWORD=password
11+
DB_PORT=3306
12+
13+
# The local path to the WordPress root directory, the one containing the wp-load.php file.
14+
## This can be a relative path from the directory that contains the codeception.yml file, or an absolute path.
15+
## If you are using Local by Flywheel, you can find the path in the Local by Flywheel app under the site's settings.
16+
WORDPRESS_ROOT_DIR="/var/www/html"
17+
18+
# This table prefix used by the WordPress site, and in Acceptance tests.
19+
WORDPRESS_TABLE_PREFIX=wp_
20+
21+
# The URL and domain of the WordPress site, and in Acceptance tests.
22+
## If the port is in use, you can change it to a different port.
23+
WORDPRESS_URL=http://localhost
24+
WORDPRESS_DOMAIN=localhost
25+
WORDPRESS_ADMIN_PATH=/wp-admin
26+
27+
# The username and password of the administrator user of the WordPress site, and in Acceptance tests.
28+
WORDPRESS_ADMIN_USER=admin
29+
WORDPRESS_ADMIN_PASSWORD=password
30+
WORDPRESS_ADMIN_EMAIL=[email protected]
31+
32+
# Tests will require a MySQL database to run.
33+
# Do not use a database that contains important data!
34+
WORDPRESS_DB_HOST=${DB_HOST}
35+
WORDPRESS_DB_USER=${DB_USER}
36+
WORDPRESS_DB_PASSWORD=${DB_PASSWORD}
37+
WORDPRESS_DB_NAME=${DB_NAME}
38+
WORDPRESS_DB_PORT=${DB_PORT}
39+
40+
# WPUnit tests will use these variables instead.
41+
# By default this is the same as WordPress
42+
TEST_DB_HOST=${WORDPRESS_DB_HOST}
43+
TEST_DB_USER=${WORDPRESS_DB_USER}
44+
TEST_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
45+
TEST_DB_NAME=${WORDPRESS_DB_NAME}
46+
TEST_DB_PORT=${WORDPRESS_DB_PORT}
47+
# The Integration suite will use this table prefix for the WordPress tables.
48+
TEST_TABLE_PREFIX=test_
49+
50+
# The DSN used by Acceptance tests.
51+
TEST_DB_DSN="mysql:host=${TEST_DB_HOST};port=${TEST_DB_PORT};dbname=${TEST_DB_NAME}"
52+
53+
# The following variables are used to determine test behavior.
54+
55+
# Include 3rd party plugins (e.g. WooCommerce) in the tests.
56+
# Skips recreating the database before running the tests.
57+
SKIP_DB_CREATE=false
58+
# Skips configuring the WordPress installation
59+
SKIP_WP_SETUP=false

0 commit comments

Comments
 (0)