-
-
Notifications
You must be signed in to change notification settings - Fork 3
235 lines (201 loc) · 7.91 KB
/
Copy pathphpunit.yml
File metadata and controls
235 lines (201 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
name: PHPUnit
on:
pull_request:
# Runs on main too — the badges job needs the coverage + mutation artifacts
# produced by the phpunit/mutation jobs to publish the badge data.
push:
branches: [main]
workflow_dispatch:
concurrency:
# Cancel superseded PR runs; let main pushes finish so the badges job
# (publishes coverage/MSI to the badges branch) is never cut off.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
permissions:
contents: read
jobs:
phpunit:
name: Unit Tests (PHP ${{ matrix.php-version }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: ["8.3", "8.4"]
include:
- php-version: "8.4"
coverage: true
steps:
- name: Checkout code
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Set up PHP
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # v2
with:
php-version: ${{ matrix.php-version }}
tools: composer:v2
coverage: ${{ matrix.coverage && 'pcov' || 'none' }}
- name: Cache Composer packages
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ~/.composer/cache/files
key: ${{ runner.os }}-composer-phpunit-${{ matrix.php-version }}-${{ hashFiles('composer.json') }}
restore-keys: ${{ runner.os }}-composer-phpunit-${{ matrix.php-version }}
- name: Install dev dependencies
run: composer install --no-interaction --no-progress
- name: Run PHPUnit
if: ${{ !matrix.coverage }}
run: vendor/bin/phpunit
- name: Run PHPUnit with coverage
if: ${{ matrix.coverage }}
# mkdir: the text report is written with a bare file_put_contents and
# relies on the Clover/HTML writers having created reports/ first.
run: |
mkdir -p reports
vendor/bin/phpunit \
--coverage-text=reports/coverage.txt \
--coverage-html reports/coverage \
--coverage-clover reports/clover.xml
- name: Enforce minimum line coverage
if: ${{ matrix.coverage }}
# Quality ratchet — raise the threshold as coverage grows.
run: php tests/coverage-checker.php reports/clover.xml 78
- name: Publish coverage summary
if: ${{ matrix.coverage && always() }}
run: |
if [[ -f reports/coverage.txt ]]; then
{
echo '## Code Coverage'
echo '```'
cat reports/coverage.txt
echo '```'
} >> "$GITHUB_STEP_SUMMARY"
fi
- name: Upload coverage report
if: ${{ matrix.coverage && always() }}
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: coverage-report
path: |
reports/coverage
reports/clover.xml
if-no-files-found: ignore
mutation:
name: Mutation Tests (Infection)
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Set up PHP
uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # v2
with:
php-version: "8.4"
tools: composer:v2
coverage: pcov
- name: Cache Composer packages
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ~/.composer/cache/files
key: ${{ runner.os }}-composer-phpunit-8.4-${{ hashFiles('composer.json') }}
restore-keys: ${{ runner.os }}-composer-phpunit-8.4
- name: Install dev dependencies
run: composer install --no-interaction --no-progress
- name: Run Infection
run: vendor/bin/infection --threads=max --logger-github --no-progress
- name: Publish mutation summary
if: always()
run: |
if [[ -f reports/infection/summary.log ]]; then
{
echo '## Mutation Testing'
echo '```'
cat reports/infection/summary.log
echo '```'
} >> "$GITHUB_STEP_SUMMARY"
fi
- name: Upload mutation report
if: always()
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: mutation-report
path: reports/infection
if-no-files-found: ignore
# Publishes shields.io endpoint JSON (coverage %, mutation score) to the
# "badges" branch; the README badges read from there via raw.githubusercontent.
badges:
name: Publish badge data
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
needs: [phpunit, mutation]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Download coverage report
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: coverage-report
path: reports
- name: Download mutation report
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: mutation-report
path: reports/infection
- name: Generate badge JSON
run: |
mkdir -p badge-data
python3 - <<'EOF'
import json
import re
import xml.etree.ElementTree as ET
def color(value, bands):
for limit, name in bands:
if value < limit:
return name
return "brightgreen"
metrics = ET.parse("reports/clover.xml").getroot().find("./project/metrics")
statements = int(metrics.get("statements"))
covered = int(metrics.get("coveredstatements"))
coverage = covered / statements * 100 if statements else 0.0
summary = open("reports/infection/summary.log").read()
def count(label):
return int(re.search(rf"^{label}: (\d+)$", summary, re.M).group(1))
total = count("Total")
detected = (
count("Killed by Test Framework")
+ count("Killed by Static Analysis")
+ count("Errored")
+ count("Timed Out")
)
covered_mutants = total - count("Not Covered")
msi = detected / covered_mutants * 100 if covered_mutants else 0.0
badges = {
"coverage.json": ("coverage", coverage, [(20, "red"), (50, "orange"), (75, "yellow")]),
"msi.json": ("mutation score", msi, [(50, "red"), (70, "orange"), (85, "yellow")]),
}
for filename, (label, value, bands) in badges.items():
with open(f"badge-data/{filename}", "w") as handle:
json.dump(
{
"schemaVersion": 1,
"label": label,
"message": f"{value:.1f}%",
"color": color(value, bands),
},
handle,
)
EOF
- name: Push badge data to badges branch
env:
GH_TOKEN: ${{ github.token }}
run: |
remote="https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
if ! git clone --depth 1 --branch badges "${remote}" badges-branch; then
mkdir badges-branch
git -C badges-branch init -b badges
git -C badges-branch remote add origin "${remote}"
fi
cp badge-data/*.json badges-branch/
cd badges-branch
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add .
git diff --cached --quiet || git commit -m "chore: update badge data"
git push origin badges