Skip to content

Commit 883d44b

Browse files
authored
Merge pull request #309 from codesnippetspro/phpunit
internal: add phpunit tests
2 parents 6990cb6 + 72b25e8 commit 883d44b

File tree

10 files changed

+2682
-61
lines changed

10 files changed

+2682
-61
lines changed

.github/workflows/phpunit-test.yml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.2'
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: codesnippetspro/setup-php@v2
33+
with:
34+
php-version: ${{ inputs.php-version }}
35+
36+
- name: Compute dependency hash
37+
id: deps-hash
38+
run: |
39+
set -euo pipefail
40+
tmpfile=$(mktemp)
41+
if [ -f "src/composer.lock" ]; then
42+
cat "src/composer.lock" >> "$tmpfile"
43+
fi
44+
if [ -s "$tmpfile" ]; then
45+
deps_hash=$(shasum -a 1 "$tmpfile" | awk '{print $1}' | cut -c1-8)
46+
else
47+
deps_hash=$(echo "${GITHUB_SHA:-unknown}" | cut -c1-8)
48+
fi
49+
echo "deps_hash=$deps_hash" >> "$GITHUB_OUTPUT"
50+
51+
- name: Get Composer cache
52+
id: composer-cache
53+
uses: actions/cache/restore@v4
54+
with:
55+
path: src/vendor
56+
key: ${{ runner.os }}-php-${{ inputs.php-version }}-composer-${{ steps.deps-hash.outputs.deps_hash }}
57+
restore-keys: |
58+
${{ runner.os }}-php-${{ inputs.php-version }}-composer-
59+
60+
- name: Install Composer dependencies
61+
if: steps.composer-cache.outputs.cache-hit != 'true'
62+
run: |
63+
cd src
64+
composer install --no-progress --prefer-dist --optimize-autoloader
65+
66+
- name: Save Composer cache
67+
if: steps.composer-cache.outputs.cache-hit != 'true'
68+
uses: actions/cache/save@v4
69+
with:
70+
path: src/vendor
71+
key: ${{ runner.os }}-php-${{ inputs.php-version }}-composer-${{ steps.deps-hash.outputs.deps_hash }}
72+
73+
- name: Install WordPress test suite
74+
run: |
75+
bash tests/install-wp-tests.sh wordpress_test root root 127.0.0.1:3306 latest true
76+
77+
- name: Run PHPUnit tests
78+
run: |
79+
cd src
80+
vendor/bin/phpunit -c ../phpunit.xml --testdox
81+
82+
- uses: actions/upload-artifact@v4
83+
if: always()
84+
with:
85+
name: phpunit-test-results-php-${{ inputs.php-version }}
86+
path: |
87+
.phpunit.result.cache
88+
if-no-files-found: ignore
89+
retention-days: 2
90+

.github/workflows/phpunit.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
phpunit-php-8-4:
58+
if: github.event_name != 'pull_request' || contains(github.event.pull_request.labels.*.name, 'run-tests')
59+
uses: ./.github/workflows/phpunit-test.yml
60+
with:
61+
php-version: '8.4'
62+
63+
test-result:
64+
needs: [phpunit-php-7-4, phpunit-php-8-0, phpunit-php-8-1, phpunit-php-8-2, phpunit-php-8-3, phpunit-php-8-4]
65+
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' || needs.phpunit-php-8-4.result != 'skipped')
66+
runs-on: ubuntu-22.04
67+
name: PHPUnit - Test Results Summary
68+
steps:
69+
- name: Test status summary
70+
run: |
71+
echo "PHP 7.4: ${{ needs.phpunit-php-7-4.result }}"
72+
echo "PHP 8.0: ${{ needs.phpunit-php-8-0.result }}"
73+
echo "PHP 8.1: ${{ needs.phpunit-php-8-1.result }}"
74+
echo "PHP 8.2: ${{ needs.phpunit-php-8-2.result }}"
75+
echo "PHP 8.3: ${{ needs.phpunit-php-8-3.result }}"
76+
echo "PHP 8.4: ${{ needs.phpunit-php-8-4.result }}"
77+
78+
- name: Check overall status
79+
if: |
80+
(needs.phpunit-php-7-4.result != 'success' && needs.phpunit-php-7-4.result != 'skipped') ||
81+
(needs.phpunit-php-8-0.result != 'success' && needs.phpunit-php-8-0.result != 'skipped') ||
82+
(needs.phpunit-php-8-1.result != 'success' && needs.phpunit-php-8-1.result != 'skipped') ||
83+
(needs.phpunit-php-8-2.result != 'success' && needs.phpunit-php-8-2.result != 'skipped') ||
84+
(needs.phpunit-php-8-3.result != 'success' && needs.phpunit-php-8-3.result != 'skipped') ||
85+
(needs.phpunit-php-8-4.result != 'success' && needs.phpunit-php-8-4.result != 'skipped')
86+
run: exit 1
87+

.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)