Skip to content

Commit 7c03f36

Browse files
authored
Merge pull request #1251 from cloudinary/feature/wp-unit-testing
Add WordPress unit testing with PHPUnit via wp-env
2 parents d573094 + d09494f commit 7c03f36

10 files changed

Lines changed: 2473 additions & 54 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,54 @@ jobs:
8080
- name: Run PHPStan
8181
run: composer phpstan
8282

83+
unit:
84+
name: Unit (PHPUnit)
85+
runs-on: ubuntu-latest
86+
steps:
87+
- name: Checkout repository
88+
uses: actions/checkout@v4
89+
90+
- name: Setup Node.js
91+
uses: actions/setup-node@v4
92+
with:
93+
node-version: '22'
94+
cache: 'npm'
95+
96+
- name: Cache Composer
97+
uses: actions/cache@v4
98+
with:
99+
path: ~/.composer/cache
100+
key: ${{ runner.os }}-composer-unit-${{ hashFiles('**/composer.lock') }}
101+
restore-keys: |
102+
${{ runner.os }}-composer-unit-
103+
104+
# wp-env downloads WordPress and the core PHPUnit suite into this
105+
# directory, which is roughly a gigabyte of git clones. Caching it keeps
106+
# `env:start` from re-cloning them on every run. The key covers the
107+
# wp-env config and version, because a change to either means different
108+
# sources have to be downloaded.
109+
- name: Cache wp-env sources
110+
uses: actions/cache@v4
111+
with:
112+
path: ~/.wp-env
113+
key: ${{ runner.os }}-wp-env-${{ hashFiles('.wp-env.json', 'package-lock.json') }}
114+
restore-keys: |
115+
${{ runner.os }}-wp-env-
116+
117+
# Also installs the Composer dependencies through the postinstall script.
118+
- name: Install dependencies
119+
run: npm ci
120+
121+
- name: Start wp-env
122+
run: npm run env:start
123+
124+
- name: Run unit tests
125+
run: npm run test:unit
126+
127+
- name: Stop wp-env
128+
if: always()
129+
run: npm run env:stop
130+
83131
e2e:
84132
name: E2E (Playwright)
85133
runs-on: ubuntu-latest

‎.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,9 @@ package/dist
6262
# PHPStan result cache
6363
/.phpstan-cache/
6464

65+
# PHPUnit
66+
/.phpunit.result.cache
67+
/phpunit.xml
68+
6569
# Generated docs (published to gh-pages by workflow)
6670
/docs/

‎composer.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"phpstan/phpstan": "^2.0",
1616
"szepeviktor/phpstan-wordpress": "^2.0",
1717
"php-stubs/wp-cli-stubs": "^2.10",
18-
"php-stubs/woocommerce-stubs": "^9.0"
18+
"php-stubs/woocommerce-stubs": "^9.0",
19+
"phpunit/phpunit": "^9.6@stable",
20+
"yoast/phpunit-polyfills": "^1.1@stable"
1921
},
2022
"config": {
2123
"platform": {
@@ -34,6 +36,9 @@
3436
],
3537
"phpstan": [
3638
"phpstan analyse --memory-limit=-1"
39+
],
40+
"test": [
41+
"phpunit"
3742
]
3843
},
3944
"minimum-stability": "dev"

0 commit comments

Comments
 (0)