Skip to content

PHPUnit Test Scaffolding #674

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

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# WP-Browser configuration
# https://wpbrowser.wptestkit.dev/

# The location Composer installed johnpbloch/wordpress to
WP_ROOT_FOLDER="wordpress"

TEST_DB_HOST="127.0.0.1"
# This must match the .wp-env.json config
TEST_DB_PORT="33306"
TEST_DB_USER="root"
TEST_DB_PASSWORD="password"

TEST_DB_NAME="wp-browser-tests"
TEST_TABLE_PREFIX="wp_"

TEST_SITE_WP_DOMAIN="localhost:8888"
TEST_SITE_ADMIN_EMAIL="[email protected]"
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.env.testing export-ignore
/.wp-env.json export-ignore
/codeception.dist.yml export-ignore
/package-lock.json export-ignore
/package.json export-ignore
/tests export-ignore
109 changes: 109 additions & 0 deletions .github/workflows/codecoverage-main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
name: Codecoverage-Main

# Runs PHPUnit unit and Codeception wp-browser wpunit tests, merges the code coverage, commits the html report to
# GitHub Pages, generates a README badge with the coverage percentage.

on:
push:
branches:
- main

jobs:

codecoverage-main:
runs-on: ubuntu-latest

services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: tests-wordpress
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

strategy:
matrix:
php: [ '7.3', '7.4', '8.0', '8.1', '8.2', '8.3' ]

steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: Check does gh-pages branch need to be created
run: |
if [[ $(git branch -l gh-pages) == "" ]]; then
gh_pages_branch_needed=true
else
gh_pages_branch_needed=false
fi
echo "GH_PAGES_BRANCH_NEEDED=$gh_pages_branch_needed" >> $GITHUB_ENV;
mkdir gh-pages

- name: Maybe create gh-pages branch
if: ${{ env.GH_PAGES_BRANCH_NEEDED }}
uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4.0.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./gh-pages
force_orphan: true
allow_empty_commit: true
commit_message: "🤖 Creating gh-pages branch"

- name: Checkout GitHub Pages branch for code coverage report
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
ref: gh-pages
path: gh-pages

- name: Install PHP
uses: shivammathur/setup-php@9e72090525849c5e82e596468b86eb55e9cc5401 # v2.32.0
with:
php-version: ${{ matrix.php }}
coverage: xdebug
tools: composer, jaschilz/php-coverage-badger
extensions: zip

- name: Read .env.testing
uses: c-py/action-dotenv-to-setenv@925b5d99a3f1e4bd7b4e9928be4e2491e29891d9 # v5
with:
env-file: .env.testing

- name: Run composer install
continue-on-error: true
run: composer install -v

- name: Allow writing to wp-content
run: sudo chmod -R a+w wp-content

- name: Clear previous code coverage
run: |
rm -rf gh-pages/phpunit || true;
mkdir gh-pages/phpunit || true;

- name: Run tests
run: XDEBUG_MODE=coverage composer test-coverage

- name: Merge code coverage
run: vendor/bin/phpcov merge --clover gh-pages/phpunit/clover.xml --php gh-pages/phpunit/phpunit.cov --html gh-pages/phpunit/html/ tests/_output/;

# See: https://github.blog/2009-12-29-bypassing-jekyll-on-github-pages/
- name: Add `.nojekyll` file so code coverage report successfully deploys to gh-pages
working-directory: gh-pages/phpunit
run: |
touch .nojekyll
git add -- .nojekyll *

- name: Update README badge
run: php-coverage-badger gh-pages/phpunit/clover.xml gh-pages/phpunit/coverage.svg

- name: Commit code coverage to gh-pages
uses: stefanzweifel/git-auto-commit-action@e348103e9026cc0eee72ae06630dbe30c8bf7a79 # v5.1.0
with:
repository: gh-pages
branch: gh-pages
commit_message: "🤖 Save code coverage report to gh-pages" # TODO: include the percentage change in the message.
commit_options: ""
env:
GITHUB_TOKEN: "${{ github.token }}"
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
node_modules
vendor
.DS_Store
.vscode
/node_modules
/tests/_output
/tests/_support/_generated
/wordpress
/wp-content
node_modules
vendor
39 changes: 19 additions & 20 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
{
"config": {
"WP_DEBUG": true,
"WP_DEBUG_LOG": true,
"WP_DEBUG_DISPLAY": true
},
"plugins": [
"."
],
"port": 8880,
"testsPort": 8881,
"env": {
"tests": {
"config": {
"WP_TESTS_DOMAIN": "localhost:8881",
"WP_TESTS_EMAIL": "[email protected]",
"WP_TESTS_TITLE": "WordPress Tests",
"WP_TESTS_BINARY": "php"
}
}
"config": {
"WP_DEBUG": true,
"WP_DEBUG_LOG": true,
"WP_DEBUG_DISPLAY": true
},
"port": 8880,
"testsPort": 8881,
"env": {
"tests": {
"config": {
"WP_TESTS_DOMAIN": "localhost:8881",
"WP_TESTS_EMAIL": "[email protected]",
"WP_TESTS_TITLE": "WordPress Tests",
"WP_TESTS_BINARY": "php"
},
"mysqlPort": 33306
}
}
},
"core": "https://wordpress.org/wordpress-6.1.7.zip"
}
20 changes: 20 additions & 0 deletions codeception.dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
commands:
- Codeception\Command\GenerateWPUnit
params:
- .env.testing
coverage:
enabled: true
include:
- /includes/*
# - /upgrades/*
bootstrap: bootstrap.php
176 changes: 98 additions & 78 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,87 +1,107 @@
{
"name": "newfold-labs/wp-module-onboarding",
"description": "Next-generation WordPress Onboarding for WordPress sites at Newfold Digital.",
"type": "library",
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Dave Ryan",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"NewfoldLabs\\WP\\Module\\Onboarding\\": "includes/"
},
"files": [
"bootstrap.php"
]
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
},
"platform": {
"php": "7.3.0"
}
},
"repositories": [
"name": "newfold-labs/wp-module-onboarding",
"description": "Next-generation WordPress Onboarding for WordPress sites at Newfold Digital.",
"type": "library",
"license": "GPL-2.0-or-later",
"authors": [
{
"type": "composer",
"url": "https://newfold-labs.github.io/satis/",
"only": [
"newfold-labs/*"
]
"name": "Dave Ryan",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"NewfoldLabs\\WP\\Module\\Onboarding\\": "includes/"
},
{
"files": [
"bootstrap.php"
]
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/installers": true,
"johnpbloch/wordpress-core-installer": true
},
"platform": {
"php": "7.3"
}
},
"repositories": {
"outlandishideas/wpackagist": {
"type": "composer",
"url": "https://wpackagist.org"
},
"0": {
"type": "composer",
"url": "https://newfold-labs.github.io/satis/",
"only": [
"newfold-labs/*"
]
},
"1": {
"type": "vcs",
"url": "[email protected]:InstaWP/connect-helpers.git",
"only": [
"instawp/*"
"instawp/*"
]
}
],
"require": {
"mustache/mustache": "^2.14.2",
"wp-cli/wp-config-transformer": "^1.4.1",
"newfold-labs/wp-module-onboarding-data": "^1.2.6",
"newfold-labs/wp-module-patterns": "^2.6",
"newfold-labs/wp-module-facebook": "^1.0.9",
"newfold-labs/wp-module-migration": "^1.0.12",
"wp-forge/helpers": "^2.0"
},
"require-dev": {
"wp-phpunit/wp-phpunit": "^6.6.2",
"yoast/phpunit-polyfills": "^2.0.2",
"newfold-labs/wp-php-standards": "^1.2.4",
"wp-cli/i18n-command": "^2.6.3"
},
"scripts": {
"fix": [
"vendor/bin/phpcbf . --standard=phpcs.xml"
],
"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",
"i18n-po": "vendor/bin/wp i18n update-po ./languages/wp-module-onboarding.pot ./languages",
"i18n-mo": "vendor/bin/wp i18n make-mo ./languages",
"i18n-php": "vendor/bin/wp i18n make-php ./languages",
"i18n-json": "find ./languages -type f -name \"*.po\" -print0 | xargs -0 -I {} sh -c 'name=$(basename \"$1\" .po); npx po2json languages/\"$name\".po languages/\"$name\"-nfd-onboarding.json -f jed1.x' -- {}",
"i18n": [
"@i18n-pot",
"@i18n-po",
"@i18n-php",
"@i18n-json"
],
"lint": [
"vendor/bin/phpcs . --standard=phpcs.xml"
]
},
"scripts-descriptions": {
"lint": "Check files against coding standards.",
"clean": "Automatically fix coding standards issues where possible.",
"i18n": "Generate new language files.",
"i18n-pot": "Generate a .pot file for translation.",
"i18n-po": "Update existing .po files.",
"i18n-mo": "Generate new language .mo files.",
"i18n-json": "Generate new language .json files."
}
},
"require": {
"mustache/mustache": "^2.14.2",
"wp-cli/wp-config-transformer": "^1.4.1",
"newfold-labs/wp-module-onboarding-data": "^1.2.6",
"newfold-labs/wp-module-patterns": "^2.6",
"newfold-labs/wp-module-facebook": "^1.0.9",
"newfold-labs/wp-module-migration": "^1.0.12",
"wp-forge/helpers": "^2.0"
},
"require-dev": {
"php": ">=7.3",
"wp-phpunit/wp-phpunit": "^6.6.2",
"yoast/phpunit-polyfills": "^2.0.2",
"newfold-labs/wp-php-standards": "^1.2.4",
"wp-cli/i18n-command": "^2.6.3",
"johnpbloch/wordpress": "6.1.7",
"lucatume/wp-browser": "*",
"phpunit/phpcov": "*"
},
"scripts": {
"fix": [
"vendor/bin/phpcbf . --standard=phpcs.xml"
],
"i18n": [
"@i18n-pot",
"@i18n-po",
"@i18n-php",
"@i18n-json"
],
"i18n-json": "find ./languages -type f -name \"*.po\" -print0 | xargs -0 -I {} sh -c 'name=$(basename \"$1\" .po); npx po2json languages/\"$name\".po languages/\"$name\"-nfd-onboarding.json -f jed1.x' -- {}",
"i18n-mo": "vendor/bin/wp i18n make-mo ./languages",
"i18n-php": "vendor/bin/wp i18n make-php ./languages",
"i18n-po": "vendor/bin/wp i18n update-po ./languages/wp-module-onboarding.pot ./languages",
"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",
"lint": [
"vendor/bin/phpcs . --standard=phpcs.xml"
],
"test": [
"codecept run wpunit"
],
"test-coverage": [
"codecept run wpunit --coverage wpunit.cov",
"phpcov merge --php tests/_output/merged.cov --html tests/_output/html tests/_output;",
"echo \"open tests/_output/html/index.html\" to view the report"
]
},
"scripts-descriptions": {
"clean": "Automatically fix coding standards issues where possible.",
"i18n": "Generate new language files.",
"i18n-json": "Generate new language .json files.",
"i18n-mo": "Generate new language .mo files.",
"i18n-po": "Update existing .po files.",
"i18n-pot": "Generate a .pot file for translation.",
"lint": "Check files against coding standards.",
"test": "Run tests.",
"test-coverage": "Run tests with coverage, merge coverage and create HTML report."
}
}
Loading