Skip to content

Commit 1cb9221

Browse files
committed
ci: add new workflow to test the unit tests automatically on git push
1 parent ecd1a8b commit 1cb9221

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

.github/workflows/unit-tests.yml

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
name: Unit Tests
2+
3+
on:
4+
# Run on pushes to `master` and on all pull requests.
5+
push:
6+
branches:
7+
- master
8+
paths-ignore:
9+
- "**.md"
10+
pull_request:
11+
# Allow manually triggering the workflow.
12+
workflow_dispatch:
13+
14+
# Cancels all previous workflow runs for the same branch that have not yet completed.
15+
concurrency:
16+
# The concurrency group contains the workflow name and the branch name.
17+
group: ${{ github.workflow }}-${{ github.ref }}
18+
cancel-in-progress: true
19+
20+
jobs:
21+
lint:
22+
if: ${{ github.ref != 'refs/heads/master' }}
23+
runs-on: ubuntu-latest
24+
25+
strategy:
26+
matrix:
27+
php: ["7.2", "7.4", "8.0", "8.2", "8.4", "8.5"]
28+
29+
name: "Lint: PHP ${{ matrix.php }}"
30+
31+
continue-on-error: ${{ matrix.php == '8.5' }}
32+
33+
steps:
34+
- name: Checkout code
35+
uses: actions/checkout@v4
36+
37+
- name: Install PHP
38+
uses: shivammathur/setup-php@v2
39+
with:
40+
php-version: ${{ matrix.php }}
41+
ini-file: "development"
42+
coverage: none
43+
tools: cs2pr
44+
45+
- name: Install Composer dependencies
46+
uses: "ramsey/composer-install@v3"
47+
with:
48+
# For the PHP "8.5", we need to install with ignore platform reqs as not all dependencies may allow it yet.
49+
composer-options: ${{ matrix.php == '8.5' && '--ignore-platform-req=php+' || '' }}
50+
# Bust the cache at least once a month - output format: YYYY-MM.
51+
custom-cache-suffix: $(date -u "+%Y-%m")
52+
53+
- name: "Lint against parse errors"
54+
run: composer lint -- --checkstyle | cs2pr
55+
56+
#### TEST STAGE ####
57+
test:
58+
if: ${{ github.ref != 'refs/heads/master' }}
59+
runs-on: ubuntu-latest
60+
61+
strategy:
62+
matrix:
63+
# The GHA matrix works different from Travis.
64+
# You can define jobs here and then augment them with extra variables in `include`,
65+
# as well as add extra jobs in `include`.
66+
# @link https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix
67+
#
68+
# The matrix is set up so as not to duplicate the builds which are run for code coverage.
69+
php: ["7.2", "7.3", "7.4", "8.0", "8.1", "8.2", "8.3", "8.5"]
70+
phpcs_version: ["lowest", "stable", "4.x-dev"]
71+
72+
include:
73+
# Add some builds with variations of the dependency versions.
74+
# Note: the PHPCS low/stable + Utils stable combi is already run via the above matrix.
75+
# And the PHPCS low + Utils low combi is run in the code coverage builds.
76+
# So these are the only combis missing.
77+
- php: "8.4"
78+
phpcs_version: "stable"
79+
80+
# Test against dev versions of all dependencies with select PHP versions for early detection of issues.
81+
- php: "7.2"
82+
phpcs_version: "dev-master"
83+
- php: "7.2"
84+
phpcs_version: "4.x-dev"
85+
- php: "7.4"
86+
phpcs_version: "4.x-dev"
87+
- php: "8.2"
88+
phpcs_version: "4.x-dev"
89+
90+
# Experimental builds. These are allowed to fail.
91+
- php: "8.5"
92+
phpcs_version: "dev-master"
93+
- php: "8.5"
94+
phpcs_version: "4.x-dev"
95+
96+
name: "Test: PHP ${{ matrix.php }} - PHPCS ${{ matrix.phpcs_version }}"
97+
98+
continue-on-error: ${{ matrix.php == '8.5' }}
99+
100+
steps:
101+
- name: Checkout code
102+
uses: actions/checkout@v4
103+
104+
# On stable PHPCS versions, allow for PHP deprecation notices.
105+
# Unit tests don't need to fail on those for stable releases where those issues won't get fixed anymore.
106+
- name: Setup ini config
107+
id: set_ini
108+
run: |
109+
if [[ "${{ contains( matrix.phpcs_version, 'dev') }}" == "false" ]]; then
110+
echo 'PHP_INI=error_reporting=E_ALL & ~E_DEPRECATED, display_errors=On, display_startup_errors=On' >> "$GITHUB_OUTPUT"
111+
else
112+
echo 'PHP_INI=error_reporting=-1, display_errors=On, display_startup_errors=On' >> "$GITHUB_OUTPUT"
113+
fi
114+
115+
- name: Install PHP
116+
uses: shivammathur/setup-php@v2
117+
with:
118+
php-version: ${{ matrix.php }}
119+
ini-values: ${{ steps.set_ini.outputs.PHP_INI }}
120+
coverage: none
121+
122+
# Remove PHPCSDevCS as it would (for now) prevent the tests from being able to run against PHPCS 4.x.
123+
- name: "Composer: remove PHPCSDevCS"
124+
run: composer remove --dev phpcsstandards/phpcsdevcs --no-update --no-interaction
125+
126+
- name: "Composer: set PHPCS version for tests (dev)"
127+
if: ${{ contains( matrix.phpcs_version, 'dev') }}
128+
run: composer require squizlabs/php_codesniffer:"${{ matrix.phpcs_version }}" --no-update --no-scripts --no-interaction
129+
130+
- name: "Composer: use lock file when necessary"
131+
if: ${{ matrix.phpcs_version == 'lowest' }}
132+
run: composer config --unset lock
133+
134+
# Install dependencies and handle caching in one go.
135+
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
136+
- name: Install Composer dependencies
137+
uses: "ramsey/composer-install@v3"
138+
with:
139+
# For the PHP "8.5", we need to install with ignore platform reqs as not all dependencies may allow it yet.
140+
composer-options: ${{ matrix.php == '8.5' && '--ignore-platform-req=php+' || '' }}
141+
# Bust the cache at least once a month - output format: YYYY-MM.
142+
custom-cache-suffix: $(date -u "+%Y-%m")
143+
144+
- name: "Composer: set PHPCS version for tests (lowest)"
145+
if: ${{ matrix.phpcs_version == 'lowest' }}
146+
run: composer update squizlabs/php_codesniffer --prefer-lowest --no-scripts --no-interaction
147+
148+
- name: Composer info
149+
run: composer info
150+
151+
- name: Grab PHPCS version
152+
id: phpcs_version
153+
run: echo "VERSION=$(vendor/bin/phpcs --version | grep --only-matching --max-count=1 --extended-regexp '\b[0-9]+\.[0-9]+')" >> "$GITHUB_OUTPUT"
154+
155+
- name: Run the unit tests
156+
run: composer test
157+
env:
158+
PHPCS_VERSION: ${{ matrix.phpcs_version }}

0 commit comments

Comments
 (0)