Skip to content

(test): add live TCP server integration tests for full coverage #46

(test): add live TCP server integration tests for full coverage

(test): add live TCP server integration tests for full coverage #46

Workflow file for this run

name: Tests
on:
push:
branches: [main]
pull_request:
jobs:
unit:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
extensions: swoole, redis, sockets, pcov
tools: composer:v2
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run tests with coverage
run: ./vendor/bin/phpunit --testsuite Unit --coverage-clover=coverage/unit.xml
- name: Upload coverage
uses: actions/upload-artifact@v4
with:
name: coverage-unit
path: coverage/unit.xml
integration:
name: Integration Tests
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
extensions: swoole, redis, sockets, pcov
tools: composer:v2
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run tests with coverage
run: ./vendor/bin/phpunit --testsuite Integration --coverage-clover=coverage/integration.xml
- name: Upload coverage
uses: actions/upload-artifact@v4
with:
name: coverage-integration
path: coverage/integration.xml
coverage:
name: Coverage Check
runs-on: ubuntu-latest
needs: [unit, integration]
steps:
- name: Download unit coverage
uses: actions/download-artifact@v4
with:
name: coverage-unit
path: coverage
- name: Download integration coverage
uses: actions/download-artifact@v4
with:
name: coverage-integration
path: coverage
- name: Merge and check coverage
run: |
merge_clover() {
local total_stmts=0 covered_stmts=0
for f in "$@"; do
[ -f "$f" ] || continue
s=$(grep -oP 'statements="\K\d+' "$f" | paste -sd+ | bc)
c=$(grep -oP 'coveredstatements="\K\d+' "$f" | paste -sd+ | bc)
total_stmts=$((total_stmts + s))
covered_stmts=$((covered_stmts + c))
done
echo "$covered_stmts $total_stmts"
}
read COVERED TOTAL <<< $(merge_clover coverage/unit.xml coverage/integration.xml)
PCT=$(echo "scale=2; $COVERED * 100 / $TOTAL" | bc)
echo "Coverage: ${PCT}% ($COVERED / $TOTAL statements)"
- name: Upload merged baseline
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
run: |
echo "$COVERED $TOTAL" > coverage/baseline.txt
env:
COVERED: ${{ env.COVERED }}
TOTAL: ${{ env.TOTAL }}
- name: Save baseline artifact
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
uses: actions/upload-artifact@v4
with:
name: coverage-baseline
path: |
coverage/unit.xml
coverage/integration.xml
retention-days: 90
- name: Download baseline (PR only)
if: github.event_name == 'pull_request'
uses: dawidd6/action-download-artifact@v6
with:
name: coverage-baseline
path: coverage/baseline
branch: main
workflow: tests.yml
if_no_artifact_found: warn
- name: Check coverage diff (PR only)
if: github.event_name == 'pull_request'
run: |
if [ ! -f coverage/baseline/unit.xml ]; then
echo "No baseline found, skipping diff check"
exit 0
fi
merge_clover() {
local total_stmts=0 covered_stmts=0
for f in "$@"; do
[ -f "$f" ] || continue
s=$(grep -oP 'statements="\K\d+' "$f" | paste -sd+ | bc)
c=$(grep -oP 'coveredstatements="\K\d+' "$f" | paste -sd+ | bc)
total_stmts=$((total_stmts + s))
covered_stmts=$((covered_stmts + c))
done
echo "$covered_stmts $total_stmts"
}
read BASE_COV BASE_TOTAL <<< $(merge_clover coverage/baseline/unit.xml coverage/baseline/integration.xml)
BASE_PCT=$(echo "scale=2; $BASE_COV * 100 / $BASE_TOTAL" | bc)
read PR_COV PR_TOTAL <<< $(merge_clover coverage/unit.xml coverage/integration.xml)
PR_PCT=$(echo "scale=2; $PR_COV * 100 / $PR_TOTAL" | bc)
DIFF=$(echo "$PR_PCT - $BASE_PCT" | bc -l)
echo "Baseline: ${BASE_PCT}% → PR: ${PR_PCT}% (${DIFF}%)"
if [ "$(echo "$DIFF < -1" | bc -l)" -eq 1 ]; then
echo "::error::Coverage dropped ${DIFF}% (max allowed: -1%)"
exit 1
fi