Skip to content

Commit 9ce7e07

Browse files
committed
Initial setup of testing framework.
1 parent 77ae3dd commit 9ce7e07

23 files changed

+937
-3
lines changed

.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: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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.4","8.3","8.2", "8.1", "8.0", "7.4"]
28+
wordpress: ["6.8","6.7", "6.6", "6.5", "6.4", "6.3", "6.2"]
29+
include:
30+
- php: "8.2"
31+
wordpress: "6.8"
32+
coverage: 1
33+
exclude:
34+
# New WP versions that dont support older PHP versions
35+
- php: "8.0"
36+
wordpress: "6.8"
37+
- php: "8.0"
38+
wordpress: "6.7"
39+
- php: "8.0"
40+
wordpress: "6.6"
41+
- php: "8.0"
42+
wordpress: "6.5"
43+
- php: "7.4"
44+
wordpress: "6.8"
45+
- php: "7.4"
46+
wordpress: "6.7"
47+
- php: "7.4"
48+
wordpress: "6.6"
49+
- php: "7.4"
50+
wordpress: "6.5"
51+
fail-fast: false
52+
53+
steps:
54+
- name: Checkout
55+
uses: actions/checkout@v4
56+
57+
- name: Install PHP
58+
uses: shivammathur/setup-php@v2
59+
with:
60+
php-version: ${{ matrix.php }}
61+
extensions: json, mbstring
62+
tools: composer:v2
63+
64+
- name: Install Composer dependencies
65+
uses: ramsey/composer-install@v3
66+
with:
67+
composer-options: "--no-progress"
68+
69+
- name: Copy .env file
70+
run: |
71+
cp .docker/.env.ci .env
72+
echo "INCLUDE_EXTENSIONS=${{ matrix.extensions }}" >> .env
73+
echo "WP_VERSION=${{ matrix.wordpress }}" >> .env
74+
echo "PHP_VERSION=${{ matrix.php }}" >> .env
75+
env:
76+
WP_VERSION: ${{ matrix.wordpress }}
77+
PHP_VERSION: ${{ matrix.php }}
78+
INCLUDE_EXTENSIONS: ${{ matrix.extensions }}
79+
80+
- name: Build test environment
81+
run: |
82+
composer run docker:build
83+
env:
84+
WP_VERSION: ${{ matrix.wordpress }}
85+
PHP_VERSION: ${{ matrix.php }}
86+
87+
- name: Start test environment
88+
run: |
89+
docker compose --env-file .env up --detach
90+
91+
CONTAINER_ID=$(docker compose ps -q wordpress)
92+
if [ -n "$CONTAINER_ID" ]; then
93+
docker exec $CONTAINER_ID init-docker.sh
94+
else
95+
echo "Error: WordPress container not found."
96+
exit 1
97+
fi
98+
env:
99+
WP_VERSION: ${{ matrix.wordpress }}
100+
PHP_VERSION: ${{ matrix.php }}
101+
102+
- name: Run Acceptance Tests w/ Docker
103+
run: |
104+
if [ "${{ matrix.extensions }}" = "true" ]; then
105+
docker exec \
106+
--env DEBUG=${{ env.DEBUG }} \
107+
--env SKIP_TESTS_CLEANUP=${{ env.SKIP_TESTS_CLEANUP }} \
108+
--env SUITES=${{ env.SUITES }} \
109+
$(docker compose ps -q wordpress) \
110+
bash -c "cd wp-content/plugins/${{ steps.plugin.outputs.slug }} && bin/run-codeception.sh"
111+
fi
112+
env:
113+
DEBUG: ${{ secrets.ACTIONS_STEP_DEBUG || matrix.debug }}
114+
SKIP_TESTS_CLEANUP: 'true'
115+
SUITES: acceptance
116+
continue-on-error: true
117+
118+
- name: Run Functional Tests w/ Docker
119+
run: |
120+
docker exec \
121+
--env DEBUG=${{ env.DEBUG }} \
122+
--env SKIP_TESTS_CLEANUP=${{ env.SKIP_TESTS_CLEANUP }} \
123+
--env SUITES=${{ env.SUITES }} \
124+
$(docker compose ps -q wordpress) \
125+
bash -c "cd wp-content/plugins/${{ steps.plugin.outputs.slug }} && bin/run-codeception.sh"
126+
env:
127+
DEBUG: ${{ secrets.ACTIONS_STEP_DEBUG || matrix.debug }}
128+
SKIP_TESTS_CLEANUP: 'true'
129+
SUITES: functional
130+
continue-on-error: true
131+
132+
- name: Run WPUnit Tests w/ Docker
133+
run: |
134+
docker exec \
135+
--env COVERAGE=${{ env.COVERAGE }} \
136+
--env USING_XDEBUG=${{ env.USING_XDEBUG }} \
137+
--env DEBUG=${{ env.DEBUG }} \
138+
--env SUITES=${{ env.SUITES }} \
139+
$(docker compose ps -q wordpress) \
140+
bash -c "cd wp-content/plugins/${{ steps.plugin.outputs.slug }} && bin/run-codeception.sh"
141+
env:
142+
COVERAGE: ${{ matrix.coverage }}
143+
USING_XDEBUG: ${{ matrix.coverage }}
144+
DEBUG: ${{ secrets.ACTIONS_STEP_DEBUG || matrix.debug }}
145+
SUITES: wpunit

.github/workflows/plugin-artifact-for-pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Plugin Artifact for PR
33
on:
44
pull_request:
55
paths:
6-
- 'plugins/**'
6+
- 'plugins/**.php'
77

88
jobs:
99
create-plugin-artifact:
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
###############################################################################
2+
# Pre-configured WordPress Installation w/ HWP Previews Plugin
3+
# For testing only, use in production not recommended. #
4+
###############################################################################
5+
6+
# Use build args to get the right wordpress + php image
7+
ARG WP_VERSION
8+
ARG PHP_VERSION
9+
10+
FROM wordpress:${WP_VERSION:-6.8}-php${PHP_VERSION:-7.4}
11+
12+
# Needed to specify the build args again after the FROM command.
13+
ARG WP_VERSION
14+
ARG PHP_VERSION
15+
16+
# Save the build args for use by the runtime environment
17+
ENV WP_VERSION=${WP_VERSION}
18+
ENV PHP_VERSION=${PHP_VERSION}
19+
20+
LABEL author=axepress
21+
LABEL author_uri=https://github.com/AxeWP
22+
23+
SHELL [ "/bin/bash", "-c" ]
24+
25+
# Install required packages
26+
RUN apt-get update && \
27+
apt-get -y install \
28+
git \
29+
ssh \
30+
tar \
31+
gzip \
32+
mariadb-client \
33+
net-tools
34+
35+
# Needed for Codeception WPDB test integration.
36+
RUN docker-php-ext-install pdo pdo_mysql
37+
38+
# Install XDebug 3
39+
RUN if [[ $PHP_VERSION == 7* ]]; then pecl install xdebug-3.1.5; else pecl install xdebug; fi \
40+
&& mkdir -p /usr/local/etc/php/conf.d/disabled \
41+
&& echo "zend_extension=xdebug" > /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini \
42+
&& echo "xdebug.mode=develop,debug,coverage" >> /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini \
43+
&& echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini \
44+
&& echo "xdebug.client_host=host.docker.internal" >> /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini \
45+
&& echo "xdebug.client_port=9003" >> /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini \
46+
&& echo "xdebug.max_nesting_level=512" >> /usr/local/etc/php/conf.d/disabled/docker-php-ext-xdebug.ini \
47+
;
48+
49+
# Set xdebug configuration off by default. Set USING_XDEBUG=1 in the runtime environment to enable it.
50+
ENV USING_XDEBUG=0
51+
52+
# Install PCOV
53+
# This is needed for Codeception / PHPUnit to track code coverage
54+
RUN apt-get install zip unzip -y \
55+
&& pecl install pcov
56+
57+
# Install Dockerize
58+
ENV DOCKERIZE_VERSION=v0.7.0
59+
RUN curl -L -O https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
60+
&& tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
61+
&& rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
62+
63+
# Install composer
64+
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
65+
RUN chmod +x /usr/local/bin/composer
66+
67+
# Install WP-CLI
68+
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \
69+
&& chmod +x wp-cli.phar \
70+
&& mv wp-cli.phar /usr/local/bin/wp
71+
72+
# Install nvm, Node.js, and npm
73+
ENV NVM_DIR=/usr/local/nvm
74+
ENV NODE_VERSION=20
75+
76+
RUN mkdir -p $NVM_DIR
77+
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash \
78+
&& . $NVM_DIR/nvm.sh \
79+
&& nvm install $NODE_VERSION \
80+
&& nvm use $NODE_VERSION \
81+
&& nvm alias default $NODE_VERSION \
82+
&& npm install -g npm
83+
84+
# Setup the container for testing
85+
COPY init-docker.sh /usr/local/bin/
86+
RUN chmod +x /usr/local/bin/init-docker.sh
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# Exit if any command fails.
4+
set -e
5+
6+
# Wait for the database
7+
dockerize -wait tcp://"${WORDPRESS_DB_HOST}":3306 -timeout 1m
8+
9+
# Get the current user
10+
11+
cd "$WORDPRESS_ROOT_DIR/wp-content/plugins/$PLUGIN_SLUG"
12+
13+
# Load NVM
14+
source $NVM_DIR/nvm.sh
15+
nvm use $NODE_VERSION
16+
17+
# Setup the test environment
18+
chmod +x ./bin/install-test-env.sh
19+
20+
bash -c "./bin/install-test-env.sh"
21+
22+
echo "Setting permissions"
23+
chmod -R 777 "$WORDPRESS_ROOT_DIR/wp-content/plugins/$PLUGIN_SLUG"
24+
25+
# Go back to the root directory
26+
cd "$WORDPRESS_ROOT_DIR"

plugins/hwp-previews/.env.dist

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
# localhost creates issues with wp config create command
9+
DB_HOST=127.0.0.1
10+
DB_USER=root
11+
DB_PASSWORD=password
12+
DB_PORT=3306
13+
14+
# The local path to the WordPress root directory, the one containing the wp-load.php file.
15+
## This can be a relative path from the directory that contains the codeception.yml file, or an absolute path.
16+
## If you are using Local by Flywheel, you can find the path in the Local by Flywheel app under the site's settings.
17+
WORDPRESS_ROOT_DIR="/tmp/wordpress"
18+
19+
# This table prefix used by the WordPress site, and in Acceptance tests.
20+
WORDPRESS_TABLE_PREFIX=wp_
21+
22+
# The URL and domain of the WordPress site, and in Acceptance tests.
23+
## If the port is in use, you can change it to a different port.
24+
WORDPRESS_URL=http://localhost
25+
WORDPRESS_DOMAIN=localhost
26+
WORDPRESS_ADMIN_PATH=/wp-admin
27+
28+
# The username and password of the administrator user of the WordPress site, and in Acceptance tests.
29+
WORDPRESS_ADMIN_USER=admin
30+
WORDPRESS_ADMIN_PASSWORD=password
31+
32+
33+
# Tests will require a MySQL database to run.
34+
# Do not use a database that contains important data!
35+
WORDPRESS_DB_HOST=${DB_HOST}
36+
WORDPRESS_DB_USER=${DB_USER}
37+
WORDPRESS_DB_PASSWORD=${DB_PASSWORD}
38+
WORDPRESS_DB_NAME=${DB_NAME}
39+
WORDPRESS_DB_PORT=${DB_PORT}
40+
41+
# WPUnit tests will use these variables instead.
42+
# By default this is the same as WordPress
43+
TEST_DB_HOST=${WORDPRESS_DB_HOST}
44+
TEST_DB_USER=${WORDPRESS_DB_USER}
45+
TEST_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
46+
TEST_DB_NAME=${WORDPRESS_DB_NAME}
47+
TEST_DB_PORT=${WORDPRESS_DB_PORT}
48+
# The Integration suite will use this table prefix for the WordPress tables.
49+
TEST_TABLE_PREFIX=test_
50+
51+
# The DSN used by Acceptance tests.
52+
TEST_DB_DSN="mysql:host=${TEST_DB_HOST};port=${TEST_DB_PORT};dbname=${TEST_DB_NAME}"
53+
54+
# The following variables are used to determine test behavior.
55+
56+
# Include 3rd party plugins (e.g. WooCommerce) in the tests.
57+
INCLUDE_EXTENSIONS=true
58+
# Skips recreating the database before running the tests.
59+
SKIP_DB_CREATE=false
60+
# Skips configuring the WordPress installation
61+
SKIP_WP_SETUP=false
62+
# Skips cleanup after the test suite run.
63+
SKIP_TESTS_CLEANUP=true
64+
# The default Codeception suite to run.
65+
SUITES=wpunit

plugins/hwp-previews/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ tests/*.suite.yml
4444
coverage/*
4545
build/
4646
.log/
47+
c3.php
4748

4849
# Cache
4950
phpcs-cache.json

plugins/hwp-previews/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ To implement your own approach from scratch you can refer to the appropriate doc
8686

8787

8888
---
89+
90+
## Testing
91+
92+
See [Testing.md](TESTING.md) for details on how to test the plugin.
93+

plugins/hwp-previews/TESTING.md

Whitespace-only changes.

0 commit comments

Comments
 (0)