Skip to content

Commit bb7102d

Browse files
committed
setup for phpunit tests
1 parent 6990cb6 commit bb7102d

File tree

9 files changed

+2381
-59
lines changed

9 files changed

+2381
-59
lines changed

.github/workflows/phpunit-test.yml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: PHPUnit Test Runner
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
php-version:
7+
required: false
8+
type: string
9+
default: '8.1'
10+
description: 'PHP version to test against'
11+
12+
jobs:
13+
phpunit-test:
14+
name: PHPUnit tests (PHP ${{ inputs.php-version }})
15+
runs-on: ubuntu-22.04
16+
17+
services:
18+
mysql:
19+
image: mysql:8.0
20+
env:
21+
MYSQL_ROOT_PASSWORD: root
22+
MYSQL_DATABASE: wordpress_test
23+
ports:
24+
- 3306:3306
25+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
26+
27+
steps:
28+
- name: Checkout source code
29+
uses: actions/checkout@v4
30+
31+
- name: Set up PHP
32+
uses: shivammathur/setup-php@v2
33+
with:
34+
php-version: ${{ inputs.php-version }}
35+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, mysql, mysqli, pdo_mysql
36+
coverage: none
37+
38+
- name: Compute dependency hash
39+
id: deps-hash
40+
run: |
41+
set -euo pipefail
42+
tmpfile=$(mktemp)
43+
if [ -f "src/composer.lock" ]; then
44+
cat "src/composer.lock" >> "$tmpfile"
45+
fi
46+
if [ -s "$tmpfile" ]; then
47+
deps_hash=$(shasum -a 1 "$tmpfile" | awk '{print $1}' | cut -c1-8)
48+
else
49+
deps_hash=$(echo "${GITHUB_SHA:-unknown}" | cut -c1-8)
50+
fi
51+
echo "deps_hash=$deps_hash" >> "$GITHUB_OUTPUT"
52+
53+
- name: Get Composer cache
54+
id: composer-cache
55+
uses: actions/cache/restore@v4
56+
with:
57+
path: src/vendor
58+
key: ${{ runner.os }}-php-${{ inputs.php-version }}-composer-${{ steps.deps-hash.outputs.deps_hash }}
59+
restore-keys: |
60+
${{ runner.os }}-php-${{ inputs.php-version }}-composer-
61+
62+
- name: Install Composer dependencies
63+
if: steps.composer-cache.outputs.cache-hit != 'true'
64+
run: |
65+
cd src
66+
composer install --no-progress --prefer-dist --optimize-autoloader
67+
68+
- name: Save Composer cache
69+
if: steps.composer-cache.outputs.cache-hit != 'true'
70+
uses: actions/cache/save@v4
71+
with:
72+
path: src/vendor
73+
key: ${{ runner.os }}-php-${{ inputs.php-version }}-composer-${{ steps.deps-hash.outputs.deps_hash }}
74+
75+
- name: Install WordPress test suite
76+
run: |
77+
bash tests/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 latest true
78+
79+
- name: Run PHPUnit tests
80+
run: |
81+
cd src
82+
vendor/bin/phpunit -c ../phpunit.xml --testdox
83+
84+
- uses: actions/upload-artifact@v4
85+
if: always()
86+
with:
87+
name: phpunit-test-results-php-${{ inputs.php-version }}
88+
path: |
89+
.phpunit.result.cache
90+
if-no-files-found: ignore
91+
retention-days: 2
92+

.github/workflows/phpunit.yml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: "(Test): PHPUnit"
2+
3+
on:
4+
pull_request:
5+
types: [labeled, synchronize, opened, reopened]
6+
push:
7+
branches:
8+
- 'core'
9+
- 'pro'
10+
paths-ignore:
11+
- '**.md'
12+
- '**.txt'
13+
- '.gitignore'
14+
- 'docs/**'
15+
workflow_dispatch:
16+
17+
permissions:
18+
contents: read
19+
pull-requests: read
20+
actions: read
21+
22+
concurrency:
23+
group: phpunit-${{ github.event_name }}-${{ github.event_name == 'pull_request' && github.event.pull_request.number || github.ref }}
24+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
25+
26+
jobs:
27+
phpunit-php-7-4:
28+
if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'run-tests')
29+
uses: ./.github/workflows/phpunit-test.yml
30+
with:
31+
php-version: '7.4'
32+
33+
phpunit-php-8-0:
34+
if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'run-tests')
35+
uses: ./.github/workflows/phpunit-test.yml
36+
with:
37+
php-version: '8.0'
38+
39+
phpunit-php-8-1:
40+
if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'run-tests')
41+
uses: ./.github/workflows/phpunit-test.yml
42+
with:
43+
php-version: '8.1'
44+
45+
phpunit-php-8-2:
46+
if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'run-tests')
47+
uses: ./.github/workflows/phpunit-test.yml
48+
with:
49+
php-version: '8.2'
50+
51+
phpunit-php-8-3:
52+
if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'run-tests')
53+
uses: ./.github/workflows/phpunit-test.yml
54+
with:
55+
php-version: '8.3'
56+
57+
test-result:
58+
needs: [phpunit-php-7-4, phpunit-php-8-0, phpunit-php-8-1, phpunit-php-8-2, phpunit-php-8-3]
59+
if: always() && (needs.phpunit-php-7-4.result != 'skipped' || needs.phpunit-php-8-0.result != 'skipped' || needs.phpunit-php-8-1.result != 'skipped' || needs.phpunit-php-8-2.result != 'skipped' || needs.phpunit-php-8-3.result != 'skipped')
60+
runs-on: ubuntu-22.04
61+
name: PHPUnit - Test Results Summary
62+
steps:
63+
- name: Test status summary
64+
run: |
65+
echo "PHP 7.4: ${{ needs.phpunit-php-7-4.result }}"
66+
echo "PHP 8.0: ${{ needs.phpunit-php-8-0.result }}"
67+
echo "PHP 8.1: ${{ needs.phpunit-php-8-1.result }}"
68+
echo "PHP 8.2: ${{ needs.phpunit-php-8-2.result }}"
69+
echo "PHP 8.3: ${{ needs.phpunit-php-8-3.result }}"
70+
71+
- name: Check overall status
72+
if: |
73+
(needs.phpunit-php-7-4.result != 'success' && needs.phpunit-php-7-4.result != 'skipped') ||
74+
(needs.phpunit-php-8-0.result != 'success' && needs.phpunit-php-8-0.result != 'skipped') ||
75+
(needs.phpunit-php-8-1.result != 'success' && needs.phpunit-php-8-1.result != 'skipped') ||
76+
(needs.phpunit-php-8-2.result != 'success' && needs.phpunit-php-8-2.result != 'skipped') ||
77+
(needs.phpunit-php-8-3.result != 'success' && needs.phpunit-php-8-3.result != 'skipped')
78+
run: exit 1
79+

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ node_modules/
1212
npm-debug.log
1313
.sass-cache/
1414

15+
# PHPUnit
16+
.phpunit.result.cache
17+
/coverage/
18+
1519
# Playwright
1620
playwright-report/
1721
test-results/

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"test": "tests"
99
},
1010
"scripts": {
11+
"test:php": "cd src && vendor/bin/phpunit -c ../phpunit.xml",
12+
"test:php:watch": "npm run test:php -- --testdox",
1113
"test:playwright": "playwright test -c tests/playwright/playwright.config.ts",
1214
"test:playwright:debug": "npm run test:playwright -- --debug",
1315
"test:playwright:ui": "npm run test:playwright -- --ui",

src/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
"require-dev": {
3838
"wp-coding-standards/wpcs": "^3.1",
3939
"phpcompatibility/phpcompatibility-wp": "^2.1",
40-
"dealerdirect/phpcodesniffer-composer-installer": "^1.0"
40+
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
41+
"phpunit/phpunit": "^9.6",
42+
"yoast/phpunit-polyfills": "^2.0"
4143
},
4244
"config": {
4345
"platform": {

0 commit comments

Comments
 (0)