Skip to content

Commit 038ed8b

Browse files
authored
Workflow Updates
1 parent 80679b7 commit 038ed8b

File tree

11 files changed

+546
-31
lines changed

11 files changed

+546
-31
lines changed

.github/workflows/php-code-quality.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ jobs:
4040
echo "Standard install failed, trying with --ignore-platform-reqs"
4141
composer install --prefer-dist --no-progress --no-suggest --ignore-platform-reqs
4242
}
43+
44+
# Verify installation was successful
45+
if [ ! -d "vendor" ]; then
46+
echo "::warning::Composer installation failed, using direct approach"
47+
composer require --dev wp-coding-standards/wpcs dealerdirect/phpcodesniffer-composer-installer phpcompatibility/php-compatibility --no-interaction
48+
fi
4349
4450
- name: Run PHP_CodeSniffer
4551
run: |

.github/workflows/wordpress-tests.yml

Lines changed: 136 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ jobs:
7575
extensions: dom, curl, libxml, mbstring, zip, pdo, mysql, pdo_mysql, bcmath, intl, gd, exif, iconv
7676
coverage: none
7777

78-
- name: Install SVN
78+
- name: Install SVN and jq
7979
run: |
8080
sudo apt-get update
81-
sudo apt-get install -y subversion
81+
sudo apt-get install -y subversion jq
8282
8383
- name: Install Composer dependencies
8484
uses: ramsey/composer-install@v3
@@ -88,27 +88,96 @@ jobs:
8888

8989
- name: Verify Composer installation
9090
run: |
91-
if [ ! -d "vendor" ]; then
92-
echo "::warning::Composer vendor directory not found. Installing dependencies manually."
93-
# For PHP 8.0+, we need to modify requirements or use ignore-platform-reqs
94-
if [[ "${{ matrix.php }}" == "8.0" || "${{ matrix.php }}" == "8.1" || "${{ matrix.php }}" == "8.2" ]]; then
95-
echo "::notice::Using PHP ${{ matrix.php }}, installing with --ignore-platform-reqs"
96-
composer install --no-progress --ignore-platform-reqs
97-
else
98-
composer install --no-progress
99-
fi
91+
# Clean start - remove composer.lock to avoid cached dependency issues
92+
rm -f composer.lock
93+
94+
# Add bc command needed for version comparison
95+
sudo apt-get install -y bc
96+
97+
# Define PHP version variable for use in conditions
98+
PHP_VERSION="${{ matrix.php }}"
99+
100+
# Determine what PHPUnit version to use
101+
if [[ "$PHP_VERSION" == "8.0" || "$PHP_VERSION" == "8.1" || "$PHP_VERSION" == "8.2" ]]; then
102+
echo "::notice::Using PHP $PHP_VERSION, configuring for PHPUnit 9.x"
103+
# Use jq to update phpunit requirement in composer.json
104+
cp composer.json composer.json.bak
105+
jq '.["require-dev"]["phpunit/phpunit"] = "^9.0"' composer.json.bak > composer.json
106+
107+
# Also update yoast/phpunit-polyfills to latest
108+
jq '.["require-dev"]["yoast/phpunit-polyfills"] = "^2.0"' composer.json > composer.json.tmp
109+
mv composer.json.tmp composer.json
110+
else
111+
echo "::notice::Using PHP $PHP_VERSION, keeping PHPUnit 7.5.x"
112+
# No change needed for PHP 7.x
100113
fi
101114
102-
if [ ! -f "vendor/bin/phpunit" ]; then
103-
echo "::warning::PHPUnit not found in vendor/bin. Installing phpunit explicitly."
104-
# For PHP 8.0+, we need a different PHPUnit version
105-
if [[ "${{ matrix.php }}" == "8.0" || "${{ matrix.php }}" == "8.1" || "${{ matrix.php }}" == "8.2" ]]; then
106-
echo "::notice::Using PHP ${{ matrix.php }}, installing PHPUnit 9.x"
107-
composer require --dev phpunit/phpunit:^9.0 --ignore-platform-reqs --no-progress
115+
# Write a custom composer.json for PHP 8.2 to handle specific requirements
116+
if [[ "$PHP_VERSION" == "8.2" ]]; then
117+
echo "::notice::Using PHP 8.2, creating specialized composer configuration"
118+
cat > composer.json.php82 << 'EOL'
119+
{
120+
"name": "enginescript/simple-wp-optimizer",
121+
"description": "Simple WP Optimizer - A lightweight WordPress optimization plugin",
122+
"type": "wordpress-plugin",
123+
"license": "GPL-2.0-or-later",
124+
"authors": [
125+
{
126+
"name": "EngineScript",
127+
"email": "support@enginescript.com"
128+
}
129+
],
130+
"minimum-stability": "stable",
131+
"require": {
132+
"php": ">=7.4"
133+
},
134+
"require-dev": {
135+
"phpunit/phpunit": "^9.5",
136+
"yoast/phpunit-polyfills": "^2.0",
137+
"wp-coding-standards/wpcs": "^2.3",
138+
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1"
139+
},
140+
"config": {
141+
"allow-plugins": {
142+
"dealerdirect/phpcodesniffer-composer-installer": true
143+
},
144+
"platform": {
145+
"php": "8.1.99"
146+
}
147+
},
148+
"scripts": {
149+
"test": "vendor/bin/phpunit",
150+
"phpcs": "vendor/bin/phpcs --standard=WordPress",
151+
"phpcbf": "vendor/bin/phpcbf --standard=WordPress"
152+
}
153+
}
154+
EOL
155+
# Use the PHP 8.2 specific composer file
156+
mv composer.json.php82 composer.json
157+
fi
158+
159+
# Install dependencies based on PHP version
160+
echo "::notice::Installing Composer dependencies for PHP $PHP_VERSION"
161+
if [[ "$PHP_VERSION" == "8.0" || "$PHP_VERSION" == "8.1" || "$PHP_VERSION" == "8.2" ]]; then
162+
composer update --no-progress --ignore-platform-reqs
163+
else
164+
composer update --no-progress
165+
fi
166+
167+
# Check if installation was successful
168+
if [ ! -d "vendor" ] || [ ! -f "vendor/bin/phpunit" ]; then
169+
echo "::error::Failed to install PHPUnit. Trying a direct approach."
170+
rm -rf vendor
171+
if [[ "$PHP_VERSION" == "8.0" || "$PHP_VERSION" == "8.1" || "$PHP_VERSION" == "8.2" ]]; then
172+
composer require --dev yoast/phpunit-polyfills:^2.0 phpunit/phpunit:^9.5 --ignore-platform-reqs --no-progress
108173
else
109-
composer require --dev phpunit/phpunit:^7.5 --no-progress
174+
composer require --dev yoast/phpunit-polyfills:^1.0 phpunit/phpunit:^7.5 --no-progress
110175
fi
111176
fi
177+
178+
# Verify PHPUnit version
179+
echo "Installed PHPUnit version:"
180+
vendor/bin/phpunit --version || echo "PHPUnit seems to be missing or broken"
112181

113182
- name: Check Test Environment
114183
id: check-test-env
@@ -130,22 +199,61 @@ jobs:
130199
echo "Database 'wordpress_test' already exists. Dropping it first."
131200
mysql -u root -h 127.0.0.1 -e "DROP DATABASE wordpress_test;"
132201
fi
133-
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 ${{ matrix.wordpress }}
202+
203+
# Create the database explicitly to avoid potential issues
204+
mysql -u root -h 127.0.0.1 -e "CREATE DATABASE IF NOT EXISTS wordpress_test;"
205+
206+
# Make the install script executable
207+
chmod +x bin/install-wp-tests.sh
208+
209+
# Install WordPress test environment
210+
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 ${{ matrix.wordpress }} || {
211+
echo "::warning::Failed to setup WordPress test environment. Showing detailed error information:"
212+
cat /tmp/wordpress-tests-lib-install-*.log 2>/dev/null || echo "No install logs found."
213+
exit 1
214+
}
134215
135216
- name: Run tests
136217
if: steps.check-test-env.outputs.test_env_ready == 'true'
137218
run: |
138219
if [ -f "vendor/bin/phpunit" ]; then
139-
# Handle WordPress compatibility with PHPUnit for PHP 8.0+
140-
if [[ "${{ matrix.php }}" == "8.0" || "${{ matrix.php }}" == "8.1" || "${{ matrix.php }}" == "8.2" ]]; then
141-
# Run with WordPress test compatibility parameter for newer PHPUnit versions
142-
echo "::notice::Running tests with PHPUnit 9.x compatibility mode for PHP ${{ matrix.php }}"
143-
vendor/bin/phpunit --no-deprecations || {
144-
echo "::warning::Test failed with --no-deprecations, trying with standard execution"
145-
vendor/bin/phpunit
146-
}
220+
# Get the PHPUnit version
221+
PHPUNIT_VERSION=$(vendor/bin/phpunit --version | grep -oP '\d+\.\d+' | head -1)
222+
echo "Detected PHPUnit version: $PHPUNIT_VERSION"
223+
224+
# Define PHP version variable for use in conditions
225+
PHP_VERSION="${{ matrix.php }}"
226+
227+
# Set up the command based on PHP and PHPUnit versions
228+
if [[ "$PHP_VERSION" == "8.0" || "$PHP_VERSION" == "8.1" || "$PHP_VERSION" == "8.2" ]]; then
229+
echo "::notice::Running tests with PHPUnit for PHP $PHP_VERSION"
230+
231+
# For PHP 8.x, check if it's PHPUnit 9.x
232+
if [[ $(echo "$PHPUNIT_VERSION >= 9.0" | bc -l) -eq 1 ]]; then
233+
# PHPUnit 9.x on PHP 8.x - safest approach
234+
echo "::notice::Using PHPUnit 9.x safe execution mode"
235+
236+
# Try running tests directly without any extra flags
237+
vendor/bin/phpunit || {
238+
echo "::warning::Test execution failed. Running individual test files..."
239+
EXIT_CODE=0
240+
# Try to run each test file individually to isolate issues
241+
for TEST_FILE in tests/test-*.php; do
242+
echo "Testing file: $TEST_FILE"
243+
vendor/bin/phpunit "$TEST_FILE" || EXIT_CODE=1
244+
done
245+
# Return the combined exit code
246+
exit $EXIT_CODE
247+
}
248+
else
249+
# PHPUnit < 9.0 on PHP 8.x - potentially problematic
250+
echo "::warning::Running PHPUnit version less than 9.0 on PHP 8.x - using compatibility mode"
251+
# Run with our compatibility layer which should handle the errors
252+
XDEBUG_MODE=coverage vendor/bin/phpunit
253+
fi
147254
else
148-
# PHP 7.x can use PHPUnit 7.x directly
255+
# PHP 7.x with PHPUnit 7.x - standard execution
256+
echo "::notice::Running tests with PHPUnit for PHP $PHP_VERSION"
149257
vendor/bin/phpunit
150258
fi
151259
else

PHPUNIT-COMPATIBILITY.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# PHPUnit Compatibility Guide
2+
3+
This document provides guidance on which PHPUnit version to use based on your PHP version.
4+
5+
## PHP and PHPUnit Version Compatibility Matrix
6+
7+
| PHP Version | PHPUnit Version | Notes |
8+
|-------------|----------------|-------|
9+
| 7.4 | 7.5.x | Original WordPress compatibility version |
10+
| 8.0 | 9.5.x | Use custom runner script (`run-phpunit.php`) |
11+
| 8.1 | 9.5.x | Use custom runner script (`run-phpunit.php`) |
12+
| 8.2 | 9.5.x | Use custom runner script (`run-phpunit.php`) |
13+
14+
## How to Run Tests
15+
16+
### For PHP 7.4
17+
18+
```bash
19+
composer test
20+
```
21+
22+
### For PHP 8.0, 8.1, 8.2
23+
24+
```bash
25+
composer test:php8
26+
```
27+
28+
## Troubleshooting
29+
30+
If you encounter errors when running PHPUnit on PHP 8.x, try the following:
31+
32+
1. Make sure you're using the right PHPUnit version for your PHP version
33+
2. Use the custom runner script: `php run-phpunit.php`
34+
3. Ensure the file `tests/php8-compatibility.php` exists
35+
4. Clear any cached files by running `rm -rf vendor && composer install`
36+
37+
## GitHub Actions
38+
39+
When running tests in GitHub Actions, the workflow will automatically detect the PHP version and use the appropriate PHPUnit version and execution method.
40+
41+
## Common Errors and Solutions
42+
43+
### Error: "Fatal error: Cannot use positional argument after named argument"
44+
45+
This occurs on PHP 8.0+ with older PHPUnit versions. Solution: Use PHPUnit 9.x.
46+
47+
### Error: "Fatal error: Cannot acquire reference to $GLOBALS"
48+
49+
This occurs on PHPUnit 7.x with PHP 8.1+. Solution: Use the custom runner script that includes compatibility fixes.
50+
51+
### Error: "--no-deprecations is not a recognized option"
52+
53+
This option doesn't exist in older PHPUnit versions. The custom runner script will avoid using this flag.

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![GitHub License](https://img.shields.io/github/license/EngineScript/Simple-WP-Optimizer)
44
![WordPress Plugin Version](https://img.shields.io/badge/version-1.5.4-blue)
5-
![WordPress Plugin Required PHP Version](https://img.shields.io/badge/php-%3E%3D7.4-green)
5+
![WordPress Plugin Required PHP Version](https://img.shields.io/badge/php-7.4--8.2-green)
66
![WordPress Plugin: Tested WP Version](https://img.shields.io/badge/wordpress-5.6--6.0-green)
77

88
A lightweight WordPress plugin designed to optimize your website by removing unnecessary scripts, styles, and header elements that can slow down your site.
@@ -77,6 +77,20 @@ Yes, hiding the WordPress version can provide a minor security benefit by making
7777
3. Set up the test environment: `bin/install-wp-tests.sh wordpress_test root '' localhost latest`
7878
4. Run tests: `composer test`
7979

80+
### PHP 8.x Compatibility
81+
82+
This plugin is fully compatible with PHP versions 7.4 through 8.2. For testing with PHP 8.x, we provide a custom PHPUnit runner script that helps avoid common compatibility issues between PHPUnit and newer PHP versions:
83+
84+
```bash
85+
# For PHP 7.4 (standard testing)
86+
composer test
87+
88+
# For PHP 8.0, 8.1, and 8.2 (using the custom runner)
89+
composer test:php8
90+
```
91+
92+
The custom runner automatically detects your PHP version and applies the appropriate compatibility settings.
93+
8094
## Contributing
8195

8296
Contributions are welcome! Please feel free to submit a Pull Request.

0 commit comments

Comments
 (0)