Skip to content

Commit 939c314

Browse files
authored
Testing: Add PHP code coverage collection for e2e tests (#343)
1 parent d86d1ff commit 939c314

File tree

9 files changed

+625
-24
lines changed

9 files changed

+625
-24
lines changed

.codecov.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
# Reference: https://docs.codecov.com/docs/codecovyml-reference
33

44
flags:
5-
e2e:
6-
carryforward: true # Preserve coverage data across uploads to avoid losing history
5+
e2e-js:
6+
carryforward: true
77
paths:
88
- 'assets/src/'
9+
e2e-php:
10+
carryforward: true
11+
paths:
12+
- 'includes/'
913
phpunit:
10-
carryforward: true # Preserve coverage data across uploads to avoid losing history
14+
carryforward: true
1115
paths:
1216
- 'includes/'
1317

.github/workflows/e2e.yml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,28 +64,46 @@ jobs:
6464
- name: Install wp-env
6565
run: npm install -g @wordpress/env
6666

67-
- name: Start wp-env
67+
- name: Start wp-env and install PCOV
68+
env:
69+
PHP_COVERAGE_ENABLED: true
6870
run: wp-env start
6971

7072
- name: Install Playwright browsers
7173
run: npx playwright install --with-deps
7274

7375
- name: Run e2e tests with coverage
76+
env:
77+
PHP_COVERAGE_ENABLED: true
7478
run: npm run test:e2e:coverage
7579

7680
- name: Generate LCOV report from Istanbul coverage
7781
if: always()
7882
run: npm run test:e2e:coverage:report
7983

80-
- name: Upload E2E Coverage to Codecov
84+
- name: Generate PHP coverage report
85+
if: always()
86+
run: npm run test:e2e:php-coverage:report
87+
88+
- name: Upload E2E JS Coverage to Codecov
8189
if: always()
8290
uses: codecov/codecov-action@v5
8391
with:
8492
token: ${{ secrets.CODECOV_TOKEN }}
8593
files: ./coverage/e2e/lcov.info
86-
flags: e2e
94+
flags: e2e-js
95+
slug: WordPress/secure-custom-fields
96+
name: e2e-js-coverage
97+
98+
- name: Upload E2E PHP Coverage to Codecov
99+
if: always()
100+
uses: codecov/codecov-action@v5
101+
with:
102+
token: ${{ secrets.CODECOV_TOKEN }}
103+
files: ./coverage/php-e2e/lcov.info
104+
flags: e2e-php
87105
slug: WordPress/secure-custom-fields
88-
name: e2e-coverage
106+
name: e2e-php-coverage
89107

90108
- name: Stop wp-env
91109
if: always()

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ assets/build/**/*.php
6262
# Code Coverage
6363
/coverage/
6464
/.nyc_output/
65+
/.php-coverage/
6566

6667
# Claude settings
6768
**/.claude/settings.local.json

.wp-env.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
{
22
"$schema": "./schemas/json/wp-env.json",
33
"plugins": ["."],
4+
"lifecycleScripts": {
5+
"afterStart": "bash ./scripts/install-pcov.sh"
6+
},
47
"env": {
58
"tests": {
69
"mappings": {
710
"wp-content/plugins/secure-custom-fields": ".",
8-
"wp-content/plugins/scf-test-plugins": "./tests/e2e/plugins"
11+
"wp-content/plugins/scf-test-plugins": "./tests/e2e/plugins",
12+
"wp-content/mu-plugins/php-coverage-collector.php": "./tests/e2e/mu-plugins/php-coverage-collector.php"
913
}
1014
}
1115
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"test:e2e:coverage": "rm -rf .nyc_output && COVERAGE_ENABLED=true wp-scripts test-playwright",
1111
"test:e2e:coverage:report": "nyc report --reporter=html --reporter=lcov",
1212
"test:e2e:debug": "wp-scripts test-playwright --debug --ui",
13+
"test:e2e:php-coverage": "rm -rf .php-coverage && PHP_COVERAGE_ENABLED=true wp-scripts test-playwright",
14+
"test:e2e:php-coverage:report": "node scripts/merge-php-coverage.js",
15+
"test:e2e:all-coverage": "rm -rf .nyc_output .php-coverage && COVERAGE_ENABLED=true PHP_COVERAGE_ENABLED=true wp-scripts test-playwright",
1316
"test:unit": "wp-scripts test-unit-js",
1417
"test:unit:watch": "wp-scripts test-unit-js --watch",
1518
"watch": "webpack --watch"

scripts/install-pcov.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
#
3+
# Install PCOV extension in wp-env Docker container for faster PHP coverage.
4+
# This script is called by wp-env lifecycleScripts.afterStart.
5+
#
6+
# PCOV is ~5-10x faster than Xdebug for coverage collection.
7+
8+
set -e
9+
10+
# Only install if PHP_COVERAGE_ENABLED is set
11+
if [ -z "$PHP_COVERAGE_ENABLED" ]; then
12+
echo "PHP_COVERAGE_ENABLED not set, skipping PCOV installation."
13+
exit 0
14+
fi
15+
16+
echo "Installing PCOV for PHP coverage..."
17+
18+
# Get the wp-env install path
19+
WP_ENV_PATH=$(wp-env install-path)
20+
cd "$WP_ENV_PATH"
21+
22+
# Check if PCOV is already installed
23+
if docker compose exec -T tests-wordpress php -m 2>/dev/null | grep -q "^pcov$"; then
24+
echo "PCOV already installed."
25+
exit 0
26+
fi
27+
28+
echo "Installing PCOV extension..."
29+
30+
# Install PCOV using pecl (run as root)
31+
docker compose exec -T -u root tests-wordpress bash -c '
32+
# Install dependencies for pecl
33+
apt-get update -qq && apt-get install -qq -y autoconf gcc make > /dev/null 2>&1 || true
34+
35+
# Install PCOV
36+
pecl install pcov > /dev/null 2>&1
37+
38+
# Enable PCOV
39+
docker-php-ext-enable pcov
40+
41+
# Configure PCOV
42+
echo "pcov.enabled=1" >> /usr/local/etc/php/conf.d/docker-php-ext-pcov.ini
43+
echo "pcov.directory=/var/www/html/wp-content/plugins/secure-custom-fields" >> /usr/local/etc/php/conf.d/docker-php-ext-pcov.ini
44+
45+
# Disable Xdebug if enabled (they conflict)
46+
if [ -f /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini ]; then
47+
mv /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini.disabled || true
48+
fi
49+
'
50+
51+
# Restart Apache to load the new extension
52+
docker compose exec -T -u root tests-wordpress service apache2 reload > /dev/null 2>&1 || true
53+
54+
echo "PCOV installed successfully."

0 commit comments

Comments
 (0)