Skip to content

Commit 9baf954

Browse files
committed
Refactor CI workflow to optimize PHPUnit testing
Revised the CI workflow to focus on PHPUnit tests with better dependency management and added support for PHP 8.4 compatibility testing. Introduced dynamic handling of old and current stable PHP versions and included a specific adjustment for aura/sql with lowest dependencies. Enhanced flexibility with manual triggers, input parameters, and proper cleanup of temporary changes.
1 parent 6215351 commit 9baf954

File tree

1 file changed

+169
-6
lines changed

1 file changed

+169
-6
lines changed
Lines changed: 169 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,176 @@
1+
# GitHub Actions workflow specifically designed for running PHPUnit tests
2+
# This workflow is optimized to install only the necessary dependencies for PHPUnit
3+
# and explicitly avoids installing development tools to prevent latest PHP compatibility issues
4+
15
name: Continuous Integration
26

37
on:
48
push:
9+
paths-ignore:
10+
- '**.md' # Skip workflow on documentation changes only
511
pull_request:
6-
workflow_dispatch:
12+
workflow_dispatch: # Enable manual trigger
13+
workflow_call: # Enable calling from other workflows
14+
inputs:
15+
old_stable:
16+
required: false
17+
type: string
18+
default: "[]" # Example: '["8.1", "8.2", "8.3"]'
19+
current_stable:
20+
required: true
21+
type: string # Example: "8.4"
22+
script:
23+
required: false
24+
type: string
25+
26+
env:
27+
COMPOSER_FLAGS: "--ansi --no-interaction --no-progress --prefer-dist --no-plugins"
28+
COMPOSER_UPDATE_FLAGS: ""
729

830
jobs:
9-
ci:
10-
uses: ray-di/.github/.github/workflows/continuous-integration.yml@v1
11-
with:
12-
old_stable: '["8.1", "8.2", "8.3"]'
13-
current_stable: 8.4
31+
phpunit:
32+
name: PHPUnit
33+
runs-on: ${{ matrix.os }}
34+
strategy:
35+
fail-fast: false
36+
matrix:
37+
php-version: ${{ fromJson(inputs.old_stable) }}
38+
dependencies: [highest, lowest]
39+
os: [ubuntu-latest]
40+
experimental: [false]
41+
include:
42+
- php-version: ${{ inputs.current_stable }} # Should be 8.4 for the special handling
43+
os: windows-latest
44+
dependencies: highest
45+
experimental: false
46+
- php-version: ${{ inputs.current_stable }} # Should be 8.4 for the special handling
47+
os: ubuntu-latest
48+
dependencies: highest
49+
experimental: false
50+
- php-version: ${{ inputs.current_stable }} # Should be 8.4 for the special handling
51+
os: ubuntu-latest
52+
dependencies: lowest # This is the target for aura/sql modification
53+
experimental: false
54+
55+
continue-on-error: ${{ matrix.experimental }}
56+
57+
steps:
58+
- name: Disable autocrlf on Windows
59+
if: contains(matrix.os, 'windows')
60+
run: git config --global core.autocrlf false
61+
62+
- name: Checkout
63+
uses: actions/checkout@v4
64+
with:
65+
fetch-depth: 0 # Not strictly necessary for this change but good practice
66+
67+
- name: Setup PHP ${{ matrix.php-version }}
68+
uses: shivammathur/setup-php@v2
69+
with:
70+
php-version: ${{ matrix.php-version }}
71+
coverage: pcov
72+
ini-values: |
73+
zend.assertions=1
74+
memory_limit=512M
75+
opcache.jit=0
76+
opcache.jit_buffer_size=0
77+
opcache.enable=0
78+
opcache.enable_cli=0
79+
extensions: pdo,pdo_mysql,pdo_sqlite
80+
81+
- name: Get composer cache directory
82+
id: composer-cache
83+
shell: bash
84+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
85+
86+
- name: Cache dependencies
87+
uses: actions/cache@v4
88+
with:
89+
path: ${{ steps.composer-cache.outputs.dir }}
90+
key: ${{ runner.os }}-php${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('**/composer.json', '**/composer.lock') }}
91+
restore-keys: |
92+
${{ runner.os }}-php${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-
93+
${{ runner.os }}-php${{ matrix.php-version }}-composer-
94+
${{ runner.os }}-composer-
95+
96+
- name: Set platform requirements
97+
shell: bash
98+
run: |
99+
composer config platform.php ${{ matrix.php-version }}
100+
101+
- name: Handle lowest dependencies update
102+
if: matrix.dependencies == 'lowest'
103+
shell: bash
104+
run: echo COMPOSER_UPDATE_FLAGS="$COMPOSER_UPDATE_FLAGS --prefer-lowest" >> $GITHUB_ENV
105+
106+
# --- Start of aura/sql specific modification ---
107+
- name: Temporarily modify composer.json for PHP 8.4 lowest dependencies
108+
id: prepare_php84_lowest_aura_sql
109+
# This step runs only for PHP 8.4 with lowest dependencies.
110+
# Assumes inputs.current_stable will be '8.4' when testing PHP 8.4
111+
if: matrix.php-version == inputs.current_stable && inputs.current_stable == '8.4' && matrix.dependencies == 'lowest'
112+
shell: bash
113+
run: |
114+
echo "PHP version is ${{ matrix.php-version }} and dependencies are 'lowest'. Modifying composer.json for aura/sql ^6.0."
115+
if [ ! -f composer.json ]; then
116+
echo "Error: composer.json not found!"
117+
exit 1
118+
fi
119+
cp composer.json composer.json.bak-aura-sql-patch
120+
# Use PHP to modify composer.json, ensuring JSON validity and handling potential errors.
121+
php -r '$composerJsonPath = "composer.json"; $config = json_decode(file_get_contents($composerJsonPath), true); if (json_last_error() !== JSON_ERROR_NONE) { fwrite(STDERR, "Error decoding composer.json: " . json_last_error_msg() . "\n"); exit(1); } if (isset($config["require"]["aura/sql"])) { $config["require"]["aura/sql"] = "^6.0"; $jsonOutput = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); if (json_last_error() !== JSON_ERROR_NONE) { fwrite(STDERR, "Error encoding composer.json: " . json_last_error_msg() . "\n"); exit(1); } file_put_contents($composerJsonPath, $jsonOutput); echo "composer.json modified: aura/sql constraint changed to ^6.0.\n"; echo "modified=true" >> $GITHUB_OUTPUT; } else { echo "Warning: aura/sql not found in require section of composer.json. No modification made.\n"; echo "modified=false" >> $GITHUB_OUTPUT; }'
122+
# Check if PHP script itself failed (e.g., syntax error, or explicit exit(1))
123+
if [ $? -ne 0 ]; then
124+
echo "Error: PHP script to modify composer.json failed."
125+
# Restore immediately if PHP script indicated failure, to ensure clean state for next steps or retry
126+
if [ -f composer.json.bak-aura-sql-patch ]; then
127+
mv composer.json.bak-aura-sql-patch composer.json
128+
echo "Restored composer.json from backup due to script failure."
129+
fi
130+
exit 1 # Fail the step
131+
fi
132+
# --- End of aura/sql specific modification ---
133+
134+
- name: Remove platform config for dependency resolution
135+
if: matrix.dependencies == 'highest'
136+
shell: bash
137+
run: composer config platform --unset
138+
139+
- name: Update dependencies
140+
shell: bash
141+
run: |
142+
composer validate --no-check-all --strict
143+
composer update ${{ env.COMPOSER_UPDATE_FLAGS }} ${{ env.COMPOSER_FLAGS }}
144+
145+
- name: Run test suite
146+
shell: bash
147+
run: vendor/bin/phpunit --coverage-clover=coverage.xml
148+
env:
149+
XDEBUG_MODE: coverage # pcov uses XDEBUG_MODE=coverage for activation
150+
151+
# --- Start of composer.json reversion ---
152+
- name: Revert composer.json modification
153+
# This step always runs to ensure composer.json is restored if it was modified.
154+
# It checks if the modification step was successful and if it actually set the 'modified' output to true.
155+
if: always() && steps.prepare_php84_lowest_aura_sql.outcome == 'success' && steps.prepare_php84_lowest_aura_sql.outputs.modified == 'true'
156+
shell: bash
157+
run: |
158+
echo "Attempting to revert composer.json modification."
159+
if [ -f composer.json.bak-aura-sql-patch ]; then
160+
mv composer.json.bak-aura-sql-patch composer.json
161+
echo "composer.json has been successfully reverted."
162+
else
163+
echo "Warning: Backup file composer.json.bak-aura-sql-patch not found. No reversion performed or backup was not created."
164+
fi
165+
# --- End of composer.json reversion ---
166+
167+
- name: Upload coverage report
168+
uses: codecov/codecov-action@v4
169+
with:
170+
token: ${{ secrets.CODECOV_TOKEN }}
171+
fail_ci_if_error: false
172+
173+
- name: Run additional script
174+
if: inputs.script != ''
175+
shell: bash
176+
run: php ${{ inputs.script }}

0 commit comments

Comments
 (0)