Skip to content

Commit 0021014

Browse files
authored
Merge pull request #674 from newfold-labs/PRESS10-97
PHPUnit Test Scaffolding
2 parents 735fc41 + c7c1667 commit 0021014

File tree

14 files changed

+4927
-1443
lines changed

14 files changed

+4927
-1443
lines changed

.env.testing

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# WP-Browser configuration
2+
# https://wpbrowser.wptestkit.dev/
3+
4+
# The location Composer installed johnpbloch/wordpress to
5+
WP_ROOT_FOLDER="wordpress"
6+
7+
TEST_DB_HOST="127.0.0.1"
8+
# This must match the .wp-env.json config
9+
TEST_DB_PORT="33306"
10+
TEST_DB_USER="root"
11+
TEST_DB_PASSWORD="password"
12+
13+
TEST_DB_NAME="wp-browser-tests"
14+
TEST_TABLE_PREFIX="wp_"
15+
16+
TEST_SITE_WP_DOMAIN="localhost:8888"
17+
TEST_SITE_ADMIN_EMAIL="[email protected]"

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/.env.testing export-ignore
2+
/.wp-env.json export-ignore
3+
/codeception.dist.yml export-ignore
4+
/package-lock.json export-ignore
5+
/package.json export-ignore
6+
/tests export-ignore
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
name: Codecoverage-Main
2+
3+
# Runs PHPUnit unit and Codeception wp-browser wpunit tests, merges the code coverage, commits the html report to
4+
# GitHub Pages, generates a README badge with the coverage percentage.
5+
6+
on:
7+
push:
8+
branches:
9+
- trunk
10+
pull_request:
11+
types: [ opened, reopened, ready_for_review, synchronize ]
12+
branches:
13+
- trunk
14+
workflow_dispatch:
15+
16+
jobs:
17+
18+
codecoverage:
19+
runs-on: ubuntu-latest
20+
21+
services:
22+
mysql:
23+
image: mysql:5.7 # Password auth did not work on 8.0 on PHP 7.3, it did seem to work for PHP 7.4+
24+
env: # These are the same username and password as the wp-env container defaults
25+
MYSQL_ROOT_PASSWORD: password
26+
MYSQL_DATABASE: tests-wordpress
27+
ports: # This mapping matches the .wp-env.json configuration
28+
- 33306:3306
29+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
30+
31+
strategy:
32+
matrix: # Supported PHP versions
33+
php: [ '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
34+
35+
steps:
36+
- name: Checkout
37+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
38+
with:
39+
fetch-depth: 0 # attempting to get all branch names.
40+
41+
# TODO: a DEPLOY_KEY is needed to enable gh-pages for the first time (the branch can be created and pushed but
42+
# it will not be reachable as a website).
43+
# Consider @see https://github.com/peaceiris/actions-gh-pages
44+
# - name: Check does gh-pages branch need to be created
45+
# run: |
46+
# git branch -l;
47+
# if [[ $(git branch -l gh-pages) == "" ]]; then
48+
# gh_pages_branch_needed=true;
49+
# echo "gh-pages branch is needed";
50+
# else
51+
# gh_pages_branch_needed=false
52+
# echo "gh-pages branch already exists";
53+
# fi
54+
# echo "GH_PAGES_BRANCH_NEEDED=$gh_pages_branch_needed" >> $GITHUB_ENV;
55+
# mkdir gh-pages
56+
#
57+
# - name: Maybe create gh-pages branch
58+
# if: ${{ env.GH_PAGES_BRANCH_NEEDED }}
59+
# uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4.0.0
60+
# with:
61+
# github_token: ${{ secrets.GITHUB_TOKEN }}
62+
# publish_dir: ./gh-pages
63+
# force_orphan: true
64+
# allow_empty_commit: true
65+
# commit_message: "🤖 Creating gh-pages branch"
66+
67+
- name: Checkout GitHub Pages branch for code coverage report
68+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
69+
with:
70+
ref: gh-pages
71+
path: gh-pages
72+
73+
- name: Install PHP
74+
uses: shivammathur/setup-php@cf4cade2721270509d5b1c766ab3549210a39a2a # v2.33.0
75+
with:
76+
php-version: ${{ matrix.php }}
77+
coverage: xdebug
78+
tools: composer, jaschilz/php-coverage-badger
79+
extensions: zip
80+
81+
- name: Read .env.testing
82+
uses: c-py/action-dotenv-to-setenv@925b5d99a3f1e4bd7b4e9928be4e2491e29891d9 # v5
83+
with:
84+
env-file: .env.testing
85+
86+
- name: Run composer install
87+
continue-on-error: true
88+
run: composer install -v
89+
90+
- name: Allow writing to wp-content
91+
if: ${{ hashFiles('wp-content') != '' }} # TODO: This may be incorrect since it's a directory.
92+
run: sudo chmod -R a+w wp-content
93+
94+
- name: Print refs
95+
run: |
96+
echo "ref: ${{ github.ref }}"
97+
echo "head_ref: ${{ github.head_ref }}"
98+
echo "base_ref: ${{ github.base_ref }}"
99+
100+
# On trunk, there will be a previous coverage report. On PRs we create a new directory using the commit SHA.
101+
- name: Clear previous code coverage
102+
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/trunk') }}
103+
run: |
104+
rm -rf gh-pages/phpunit || true;
105+
mkdir gh-pages/phpunit || true;
106+
107+
# - name: Run unit tests
108+
# if: ${{ hashFiles('tests/phpunit/bootstrap.php') != '' }} # Only run unit tests if they are present.
109+
# run: XDEBUG_MODE=coverage vendor/bin/phpunit --bootstrap tests/phpunit/bootstrap.php --coverage-php tests/_output/unit.cov --debug
110+
111+
- name: Run wpunit tests
112+
run: XDEBUG_MODE=coverage vendor/bin/codecept run wpunit --coverage tests/_output/wpunit.cov --debug
113+
114+
# For PRs, we'll generate the coverage report on each pushed commit
115+
- name: Merge code coverage for PR
116+
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
117+
run: |
118+
vendor/bin/phpcov merge --clover clover.xml tests/_output/;
119+
vendor/bin/phpcov merge --clover gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/clover.xml --php gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/phpunit.cov --html gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/html/ tests/_output/;
120+
121+
# On main, we want it output to a different path
122+
- name: Merge code coverage for main
123+
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/trunk') }}
124+
run: |
125+
vendor/bin/phpcov merge --clover clover.xml tests/_output/;
126+
vendor/bin/phpcov merge --clover gh-pages/phpunit/clover.xml --php gh-pages/phpunit/phpunit.cov --html gh-pages/phpunit/html/ tests/_output/;
127+
128+
# This makes the coverage percentage available in `{{ steps.coverage-percentage.outputs.coverage-rounded }}`.
129+
- name: Check test coverage
130+
if: ${{ matrix.php == '7.3' }}
131+
uses: johanvanhelden/gha-clover-test-coverage-check@2543c79a701f179bd63aa14c16c6938c509b2cec # v1
132+
id: coverage-percentage
133+
with:
134+
percentage: 25
135+
exit: false
136+
filename: clover.xml
137+
rounded-precision: "0"
138+
139+
# See: https://github.blog/2009-12-29-bypassing-jekyll-on-github-pages/
140+
- name: Add `.nojekyll` file so code coverage report successfully deploys to gh-pages
141+
if: ${{ (matrix.php == '7.3') }}
142+
working-directory: gh-pages
143+
run: |
144+
touch .nojekyll
145+
git add -- .nojekyll *
146+
147+
- name: Update README coverage badge
148+
if: ${{ (matrix.php == '7.3') && (github.ref == 'refs/heads/trunk') }} # only commit on trunk, on the PHP version we're using in production.
149+
run: php-coverage-badger clover.xml gh-pages/phpunit/coverage.svg
150+
151+
- name: Generate PR coverage badge
152+
if: ${{ (matrix.php == '7.3') && github.event_name == 'pull_request' }}
153+
run: php-coverage-badger clover.xml gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/coverage.svg
154+
155+
- name: Commit code coverage to gh-pages
156+
if: ${{ matrix.php == '7.3' }}
157+
uses: stefanzweifel/git-auto-commit-action@e348103e9026cc0eee72ae06630dbe30c8bf7a79 # v5.1.0
158+
with:
159+
repository: gh-pages
160+
branch: gh-pages
161+
commit_message: ${{ format('🤖 Save code coverage report to gh-pages {0}%', steps.coverage-percentage.outputs.coverage-rounded) }}
162+
commit_options: ""
163+
env:
164+
GITHUB_TOKEN: "${{ github.token }}"
165+
166+
- name: Add coverage badge to PR comment
167+
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
168+
run: |
169+
echo "[![Code Coverage ](https://newfold-labs.github.io/wp-module-onboarding/${{ github.event.pull_request.head.sha }}/phpunit/coverage.svg)](https://newfold-labs.github.io/wp-module-onboarding/${{ github.event.pull_request.head.sha }}/phpunit/html/)" >> coverage-comment.md
170+
echo "" >> coverage-comment.md
171+
echo "" >> coverage-comment.md
172+
173+
- name: Add coverage report link to PR comment
174+
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
175+
run: |
176+
echo "${{ format('[project coverage report {0}%](https://newfold-labs.github.io/wp-module-onboarding/{1}/phpunit/html/) @ {2}', steps.coverage-percentage.outputs.coverage-rounded, github.event.pull_request.head.sha, github.event.pull_request.head.sha) }}" >> coverage-comment.md
177+
echo "" >> coverage-comment.md
178+
echo "" >> coverage-comment.md
179+
180+
- name: Add phpcov uncovered lines report to PR comment
181+
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
182+
continue-on-error: true # phpcov can fail if there are no uncovered lines
183+
run: |
184+
BRANCHED_COMMIT=$(git rev-list origin..HEAD | tail -n 1);
185+
echo "BRANCHED_COMMIT=$BRANCHED_COMMIT"
186+
git diff $BRANCHED_COMMIT...${{ github.event.pull_request.head.sha }} > branch.diff;
187+
cat branch.diff;
188+
OUTPUT=${vendor/bin/phpcov patch-coverage --path-prefix $(pwd) ./gh-pages/${{ github.event.pull_request.head.sha }}/phpunit/phpunit.cov branch.diff || true}
189+
echo $OUTPUT;
190+
echo "$OUTPUT" >> coverage-comment.md
191+
192+
- name: Add coverage PR comment
193+
uses: mshick/add-pr-comment@b8f338c590a895d50bcbfa6c5859251edc8952fc # v2.8.2
194+
if: ${{ matrix.php == '7.3' && github.event_name == 'pull_request' }}
195+
with:
196+
message-id: coverage-report
197+
message-path: coverage-comment.md

.gitignore

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
node_modules
2-
vendor
31
.DS_Store
42
.vscode
3+
/node_modules
4+
/tests/_output
5+
/tests/_support/_generated
6+
/wordpress
7+
/wp-content
8+
node_modules
9+
vendor

.wp-env.json

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
11
{
2-
"config": {
3-
"WP_DEBUG": true,
4-
"WP_DEBUG_LOG": true,
5-
"WP_DEBUG_DISPLAY": true
6-
},
7-
"plugins": [
8-
"."
9-
],
10-
"port": 8880,
11-
"testsPort": 8881,
12-
"env": {
13-
"tests": {
14-
"config": {
15-
"WP_TESTS_DOMAIN": "localhost:8881",
16-
"WP_TESTS_EMAIL": "[email protected]",
17-
"WP_TESTS_TITLE": "WordPress Tests",
18-
"WP_TESTS_BINARY": "php"
19-
}
20-
}
21-
}
22-
}
2+
"env": {
3+
"tests": {
4+
"mysqlPort": 33306
5+
}
6+
}
7+
}

codeception.dist.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
paths:
2+
tests: tests
3+
output: tests/_output
4+
data: tests/_data
5+
support: tests/_support
6+
envs: tests/_envs
7+
actor_suffix: Tester
8+
extensions:
9+
enabled:
10+
- Codeception\Extension\RunFailed
11+
commands:
12+
- Codeception\Command\GenerateWPUnit
13+
params:
14+
- .env.testing
15+
coverage:
16+
enabled: true
17+
include:
18+
- /includes/*
19+
# - /upgrades/*
20+
bootstrap: bootstrap.php

composer.json

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,34 @@
1919
},
2020
"config": {
2121
"allow-plugins": {
22-
"dealerdirect/phpcodesniffer-composer-installer": true
22+
"dealerdirect/phpcodesniffer-composer-installer": true,
23+
"composer/installers": true,
24+
"johnpbloch/wordpress-core-installer": true
2325
},
2426
"platform": {
25-
"php": "7.3.0"
27+
"php": "7.3"
28+
}
29+
},
30+
"repositories": {
31+
"outlandishideas/wpackagist": {
32+
"type": "composer",
33+
"url": "https://wpackagist.org"
34+
},
35+
"0": {
36+
"type": "composer",
37+
"url": "https://newfold-labs.github.io/satis/",
38+
"only": [
39+
"newfold-labs/*"
40+
]
41+
},
42+
"1": {
43+
"type": "vcs",
44+
"url": "[email protected]:InstaWP/connect-helpers.git",
45+
"only": [
46+
"instawp/*"
47+
]
2648
}
2749
},
28-
"repositories": [
29-
{
30-
"type": "composer",
31-
"url": "https://newfold-labs.github.io/satis/",
32-
"only": [
33-
"newfold-labs/*"
34-
]
35-
},
36-
{
37-
"type": "vcs",
38-
"url": "[email protected]:InstaWP/connect-helpers.git",
39-
"only": [
40-
"instawp/*"
41-
]
42-
}
43-
],
4450
"require": {
4551
"mustache/mustache": "^2.14.2",
4652
"wp-cli/wp-config-transformer": "^1.4.1",
@@ -51,16 +57,20 @@
5157
"wp-forge/helpers": "^2.0"
5258
},
5359
"require-dev": {
60+
"php": ">=7.3",
5461
"wp-phpunit/wp-phpunit": "^6.8.0",
5562
"yoast/phpunit-polyfills": "^4.0.0",
5663
"newfold-labs/wp-php-standards": "^1.2.5",
57-
"wp-cli/i18n-command": "^2.6.3"
64+
"wp-cli/i18n-command": "^2.6.3",
65+
"johnpbloch/wordpress": "6.1.7",
66+
"lucatume/wp-browser": "*",
67+
"phpunit/phpcov": "*"
5868
},
5969
"scripts": {
6070
"fix": [
6171
"vendor/bin/phpcbf . --standard=phpcs.xml"
6272
],
63-
"i18n-pot": "vendor/bin/wp i18n make-pot . ./languages/wp-module-onboarding.pot --headers='{\"Report-Msgid-Bugs-To\":\"https://github.com/newfold-labs/wp-module-onboarding/issues\",\"POT-Creation-Date\":\"2024-11-18T07:59:34+00:00\"}' --exclude=assets,tests,src",
73+
"i18n-pot": "vendor/bin/wp i18n make-pot . ./languages/wp-module-onboarding.pot --headers='{\"Report-Msgid-Bugs-To\":\"https://github.com/newfold-labs/wp-module-onboarding/issues\",\"POT-Creation-Date\":\"2024-11-18T07:59:34+00:00\"}' --exclude=assets,tests,src,wordpress",
6474
"i18n-po": "vendor/bin/wp i18n update-po ./languages/wp-module-onboarding.pot ./languages",
6575
"i18n-mo": "vendor/bin/wp i18n make-mo ./languages",
6676
"i18n-php": "vendor/bin/wp i18n make-php ./languages",
@@ -73,15 +83,25 @@
7383
],
7484
"lint": [
7585
"vendor/bin/phpcs . --standard=phpcs.xml"
86+
],
87+
"test": [
88+
"codecept run wpunit"
89+
],
90+
"test-coverage": [
91+
"codecept run wpunit --coverage wpunit.cov",
92+
"phpcov merge --php tests/_output/merged.cov --html tests/_output/html tests/_output;",
93+
"echo \"open tests/_output/html/index.html\" to view the report"
7694
]
7795
},
7896
"scripts-descriptions": {
79-
"lint": "Check files against coding standards.",
8097
"clean": "Automatically fix coding standards issues where possible.",
8198
"i18n": "Generate new language files.",
82-
"i18n-pot": "Generate a .pot file for translation.",
83-
"i18n-po": "Update existing .po files.",
99+
"i18n-json": "Generate new language .json files.",
84100
"i18n-mo": "Generate new language .mo files.",
85-
"i18n-json": "Generate new language .json files."
101+
"i18n-po": "Update existing .po files.",
102+
"i18n-pot": "Generate a .pot file for translation.",
103+
"lint": "Check files against coding standards.",
104+
"test": "Run tests.",
105+
"test-coverage": "Run tests with coverage, merge coverage and create HTML report."
86106
}
87107
}

0 commit comments

Comments
 (0)